src/EventListener/SitemapSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Dto\SpeakerSearchDto;
  4. use App\Repository\CategoryRepository;
  5. use App\Repository\PageRepository;
  6. use App\Repository\SpeakerRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  10. use Presta\SitemapBundle\Service\UrlContainerInterface;
  11. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  12. class SitemapSubscriber implements EventSubscriberInterface
  13. {
  14.     private SpeakerRepository $speakerPageRepository;
  15.     private PageRepository $pageRepository;
  16.     private CategoryRepository $categoryRepository;
  17.     public function __construct(
  18.         SpeakerRepository  $speakerPageRepository,
  19.         PageRepository     $pageRepository,
  20.         CategoryRepository $categoryRepository
  21.     )
  22.     {
  23.         $this->speakerPageRepository $speakerPageRepository;
  24.         $this->pageRepository $pageRepository;
  25.         $this->categoryRepository $categoryRepository;
  26.     }
  27.     /**
  28.      * @inheritdoc
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             SitemapPopulateEvent::class => 'populate',
  34.         ];
  35.     }
  36.     /**
  37.      * @param SitemapPopulateEvent $event
  38.      */
  39.     public function populate(SitemapPopulateEvent $event): void
  40.     {
  41.         $this->registerSpeakers($event->getUrlContainer(), $event->getUrlGenerator());
  42.         $this->registerStaticPages($event->getUrlContainer(), $event->getUrlGenerator());
  43.         $this->registerNews($event->getUrlContainer(), $event->getUrlGenerator());
  44.         $this->registerSpeakerCategories($event->getUrlContainer(), $event->getUrlGenerator());
  45.     }
  46.     /**
  47.      * @param UrlContainerInterface $urls
  48.      * @param UrlGeneratorInterface $router
  49.      */
  50.     public function registerSpeakers(UrlContainerInterface $urlsUrlGeneratorInterface $router): void
  51.     {
  52.         $searchDto = new SpeakerSearchDto('');
  53.         $speakers $this->speakerPageRepository->search($searchDto);
  54.         foreach ($speakers as $speaker) {
  55.             $urls->addUrl(
  56.                 new UrlConcrete(
  57.                     $router->generate(
  58.                         'speaker.detail',
  59.                         ['uri' => $speaker->getUri()],
  60.                         UrlGeneratorInterface::ABSOLUTE_URL
  61.                     )
  62.                 ), 'speakers'
  63.             );
  64.         }
  65.         $urls->addUrl(
  66.             new UrlConcrete(
  67.                 $router->generate('speaker.list', [], UrlGeneratorInterface::ABSOLUTE_URL)
  68.             ), 'speakers'
  69.         );
  70.     }
  71.     /**
  72.      * @param UrlContainerInterface $urls
  73.      * @param UrlGeneratorInterface $router
  74.      */
  75.     public function registerStaticPages(UrlContainerInterface $urlsUrlGeneratorInterface $router): void
  76.     {
  77.         $urls->addUrl(
  78.             new UrlConcrete(
  79.                 $router->generate('home', [], UrlGeneratorInterface::ABSOLUTE_URL), nullnull1), 'default',
  80.         );
  81.         $urls->addUrl(
  82.             new UrlConcrete(
  83.                 $router->generate('about', [], UrlGeneratorInterface::ABSOLUTE_URL)
  84.             ), 'default'
  85.         );
  86.         $urls->addUrl(
  87.             new UrlConcrete(
  88.                 $router->generate('partnership.request', [], UrlGeneratorInterface::ABSOLUTE_URL)
  89.             ), 'default'
  90.         );
  91.         $urls->addUrl(
  92.             new UrlConcrete(
  93.                 $router->generate('search', [], UrlGeneratorInterface::ABSOLUTE_URL)
  94.             ), 'default'
  95.         );
  96.         $urls->addUrl(
  97.             new UrlConcrete(
  98.                 $router->generate('app_login', [], UrlGeneratorInterface::ABSOLUTE_URL)
  99.             ), 'default'
  100.         );
  101.         $urls->addUrl(
  102.             new UrlConcrete(
  103.                 $router->generate('app_registration', [], UrlGeneratorInterface::ABSOLUTE_URL)
  104.             ), 'default'
  105.         );
  106.         $urls->addUrl(
  107.             new UrlConcrete(
  108.                 $router->generate('docs', [], UrlGeneratorInterface::ABSOLUTE_URL)
  109.             ), 'default'
  110.         );
  111.     }
  112.     public function registerNews(UrlContainerInterface $urlsUrlGeneratorInterface $router): void
  113.     {
  114.         $urls->addUrl(
  115.             new UrlConcrete(
  116.                 $router->generate('news.index', [], UrlGeneratorInterface::ABSOLUTE_URL)
  117.             ), 'news'
  118.         );
  119.         $news $this->pageRepository->findAll();
  120.         foreach ($news as $item) {
  121.             $urls->addUrl(
  122.                 new UrlConcrete(
  123.                     $router->generate('page', ['uri' => $item->getUri()], UrlGeneratorInterface::ABSOLUTE_URL)
  124.                 ), 'news'
  125.             );
  126.         }
  127.     }
  128.     public function registerSpeakerCategories(UrlContainerInterface $urlsUrlGeneratorInterface $router)
  129.     {
  130.         $categories $this->categoryRepository->findAll();
  131.         foreach ($categories as $category) {
  132.             $urls->addUrl(
  133.                 new UrlConcrete(
  134.                     $router->generate('speaker.list', ['uri' => $category->getUri()], UrlGeneratorInterface::ABSOLUTE_URL)
  135.                 ), 'category'
  136.             );
  137.         }
  138.     }
  139. }