<?php
namespace App\EventListener;
use App\Repository\AttributeGroupRepository;
use App\Repository\CourseRepository;
use App\Repository\CourseTopicRepository;
use App\Repository\ManualRepository;
use App\Repository\NewsRepository;
use App\Repository\OrganizerRepository;
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 CourseRepository $courseRepository;
private OrganizerRepository $organizerRepository;
private NewsRepository $newsRepository;
private ManualRepository $manualRepository;
private AttributeGroupRepository $attributeGroupRepository;
private CourseTopicRepository $courseTopicRepository;
public function __construct(
CourseRepository $courseRepository,
OrganizerRepository $organizerRepository,
NewsRepository $newsRepository,
ManualRepository $manualRepository,
AttributeGroupRepository $attributeGroupRepository,
CourseTopicRepository $courseTopicRepository
)
{
$this->courseRepository = $courseRepository;
$this->organizerRepository = $organizerRepository;
$this->newsRepository = $newsRepository;
$this->manualRepository = $manualRepository;
$this->attributeGroupRepository = $attributeGroupRepository;
$this->courseTopicRepository = $courseTopicRepository;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::class => 'populate',
];
}
/**
* @param SitemapPopulateEvent $event
*/
public function populate(SitemapPopulateEvent $event): void
{
$this->registerNewsUrls($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerManualUrls($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerCourseUrls($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerCourseTopicsUrls($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerAttributeGroupsUrls($event->getUrlContainer(), $event->getUrlGenerator());
$this->registerOrganizerUrls($event->getUrlContainer(), $event->getUrlGenerator());
}
public function registerUrls($routeName, $items, $sectionName, UrlContainerInterface $urls, UrlGeneratorInterface $router, $slugParamName = 'slug'): void
{
foreach ($items as $item) {
if ($slugParamName == 'identifier') {
$params = ['slug' => $item->getIdentifier()];
} else {
$params = [$slugParamName => $item->getId()];
}
$urls->addUrl(
new UrlConcrete(
$router->generate(
$routeName,
$params,
UrlGeneratorInterface::ABSOLUTE_URL
)
),
$sectionName
);
}
}
/**
* @param UrlContainerInterface $urls
* @param UrlGeneratorInterface $router
*/
public function registerNewsUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
{
$items = $this->newsRepository->findAll();
$this->registerUrls('news_detail', $items, 'news', $urls, $router);
}
/**
* @param UrlContainerInterface $urls
* @param UrlGeneratorInterface $router
*/
public function registerManualUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
{
$items = $this->manualRepository->findAll();
$this->registerUrls('manual_detail', $items, 'manuals', $urls, $router);
}
private function registerCourseUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router)
{
$items = $this->courseRepository->getLatestCourses();
$this->registerUrls('course_show', $items, 'courses', $urls, $router, 'id');
}
private function registerCourseTopicsUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router)
{
$items = $this->courseTopicRepository->findAll();
$this->registerUrls('course_topic_index', $items, 'main_topics', $urls, $router, 'identifier');
}
private function registerAttributeGroupsUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router)
{
$items = $this->attributeGroupRepository->findAll();
$this->registerUrls('attribute_group_index', $items, 'attribute_groups', $urls, $router, 'identifier');
}
private function registerOrganizerUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router)
{
$items = $this->organizerRepository->findAll();
$this->registerUrls('organizer_detail', $items, 'organizers', $urls, $router, 'id');
}
}