<?php
namespace App\EventListener;
use App\Dto\SpeakerSearchDto;
use App\Repository\CategoryRepository;
use App\Repository\PageRepository;
use App\Repository\SpeakerRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
class SitemapSubscriber implements EventSubscriberInterface
{
private SpeakerRepository $speakerPageRepository;
private PageRepository $pageRepository;
private CategoryRepository $categoryRepository;
public function __construct(
SpeakerRepository $speakerPageRepository,
PageRepository $pageRepository,
CategoryRepository $categoryRepository
)
{
$this->speakerPageRepository = $speakerPageRepository;
$this->pageRepository = $pageRepository;
$this->categoryRepository = $categoryRepository;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents(): array
{
return [
SitemapPopulateEvent::class => 'populate',
];
}
/**
* @param SitemapPopulateEvent $event
*/
public function populate(SitemapPopulateEvent $event): void
{
$this->registerSpeakers($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerStaticPages($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerNews($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerSpeakerCategories($event->getUrlContainer(), $event->getUrlGenerator());
}
/**
* @param UrlContainerInterface $urls
* @param UrlGeneratorInterface $router
*/
public function registerSpeakers(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
{
$searchDto = new SpeakerSearchDto('');
$speakers = $this->speakerPageRepository->search($searchDto);
foreach ($speakers as $speaker) {
$urls->addUrl(
new UrlConcrete(
$router->generate(
'speaker.detail',
['uri' => $speaker->getUri()],
UrlGeneratorInterface::ABSOLUTE_URL
)
), 'speakers'
);
}
$urls->addUrl(
new UrlConcrete(
$router->generate('speaker.list', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'speakers'
);
}
/**
* @param UrlContainerInterface $urls
* @param UrlGeneratorInterface $router
*/
public function registerStaticPages(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
{
$urls->addUrl(
new UrlConcrete(
$router->generate('home', [], UrlGeneratorInterface::ABSOLUTE_URL), null, null, 1), 'default',
);
$urls->addUrl(
new UrlConcrete(
$router->generate('about', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'default'
);
$urls->addUrl(
new UrlConcrete(
$router->generate('partnership.request', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'default'
);
$urls->addUrl(
new UrlConcrete(
$router->generate('search', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'default'
);
$urls->addUrl(
new UrlConcrete(
$router->generate('app_login', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'default'
);
$urls->addUrl(
new UrlConcrete(
$router->generate('app_registration', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'default'
);
$urls->addUrl(
new UrlConcrete(
$router->generate('docs', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'default'
);
}
public function registerNews(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
{
$urls->addUrl(
new UrlConcrete(
$router->generate('news.index', [], UrlGeneratorInterface::ABSOLUTE_URL)
), 'news'
);
$news = $this->pageRepository->findAll();
foreach ($news as $item) {
$urls->addUrl(
new UrlConcrete(
$router->generate('page', ['uri' => $item->getUri()], UrlGeneratorInterface::ABSOLUTE_URL)
), 'news'
);
}
}
public function registerSpeakerCategories(UrlContainerInterface $urls, UrlGeneratorInterface $router)
{
$categories = $this->categoryRepository->findAll();
foreach ($categories as $category) {
$urls->addUrl(
new UrlConcrete(
$router->generate('speaker.list', ['uri' => $category->getUri()], UrlGeneratorInterface::ABSOLUTE_URL)
), 'category'
);
}
}
}