<?php
namespace App\Subscriber;
use App\Entity\City;
use App\Entity\Need;
use App\Entity\Partner;
use App\Entity\Speciality;
use Doctrine\ORM\EntityManagerInterface;
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 $em;
private $router;
/**
* @param EntityManagerInterface $em
* @param UrlGeneratorInterface $router
*/
public function __construct(EntityManagerInterface $em, UrlGeneratorInterface $router)
{
$this->em = $em;
$this->router = $router;
}
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
SitemapPopulateEvent::class => 'populate',
];
}
/**
* @param SitemapPopulateEvent $event
*/
public function populate(SitemapPopulateEvent $event): void
{
$this->registerBlogPostsUrls($event->getUrlContainer());
}
/**
* @param UrlContainerInterface $urls
* @param UrlGeneratorInterface $router
*/
public function registerBlogPostsUrls(UrlContainerInterface $urls): void
{
$needs = $this->em->getRepository(Need::class)->findAll();
foreach ($needs as $need) {
$urls->addUrl(
new UrlConcrete(
$this->router->generate(
'default_besoins_fiche',
['slug' => $need->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'default'
);
}
$specialities = $this->em->getRepository(Speciality::class)->findAll();
$cities = $this->em->getRepository(City::class)->findAll();
foreach ($specialities as $speciality) {
$urls->addUrl(
new UrlConcrete(
$this->router->generate(
'praticiens_fiche',
['slugJob' => $speciality->getSlugJob()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'default'
);
$urls->addUrl(
new UrlConcrete(
$this->router->generate(
'praticiens_description',
['slugJob' => $speciality->getSlugJob()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'default'
);
foreach ($cities as $city) {
$urls->addUrl(
new UrlConcrete(
$this->router->generate(
'praticiens_fiche_ville',
['slugJob' => $speciality->getSlugJob(), 'slugCity' => $city->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'default'
);
}
}
$partners = $this->em->getRepository(Partner::class)->findBy(["ispractitioner" => true]);
foreach ($partners as $partner) {
if ($partner->getIsActivePractitioner()) {
$urls->addUrl(
new UrlConcrete(
$this->router->generate(
'default_praticien_fiche',
['slug' => $partner->getSlug()],
UrlGeneratorInterface::ABSOLUTE_URL
)
),
'default'
);
}
}
}
}