src/Subscriber/SitemapSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Entity\City;
  4. use App\Entity\Need;
  5. use App\Entity\Partner;
  6. use App\Entity\Speciality;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  11. use Presta\SitemapBundle\Service\UrlContainerInterface;
  12. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  13. class SitemapSubscriber implements EventSubscriberInterface
  14. {
  15. private $em;
  16. private $router;
  17. /**
  18. * @param EntityManagerInterface $em
  19. * @param UrlGeneratorInterface $router
  20. */
  21. public function __construct(EntityManagerInterface $em, UrlGeneratorInterface $router)
  22. {
  23. $this->em = $em;
  24. $this->router = $router;
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. public static function getSubscribedEvents()
  30. {
  31. return [
  32. SitemapPopulateEvent::class => 'populate',
  33. ];
  34. }
  35. /**
  36. * @param SitemapPopulateEvent $event
  37. */
  38. public function populate(SitemapPopulateEvent $event): void
  39. {
  40. $this->registerBlogPostsUrls($event->getUrlContainer());
  41. }
  42. /**
  43. * @param UrlContainerInterface $urls
  44. * @param UrlGeneratorInterface $router
  45. */
  46. public function registerBlogPostsUrls(UrlContainerInterface $urls): void
  47. {
  48. $needs = $this->em->getRepository(Need::class)->findAll();
  49. foreach ($needs as $need) {
  50. $urls->addUrl(
  51. new UrlConcrete(
  52. $this->router->generate(
  53. 'default_besoins_fiche',
  54. ['slug' => $need->getSlug()],
  55. UrlGeneratorInterface::ABSOLUTE_URL
  56. )
  57. ),
  58. 'default'
  59. );
  60. }
  61. $specialities = $this->em->getRepository(Speciality::class)->findAll();
  62. $cities = $this->em->getRepository(City::class)->findAll();
  63. foreach ($specialities as $speciality) {
  64. $urls->addUrl(
  65. new UrlConcrete(
  66. $this->router->generate(
  67. 'praticiens_fiche',
  68. ['slugJob' => $speciality->getSlugJob()],
  69. UrlGeneratorInterface::ABSOLUTE_URL
  70. )
  71. ),
  72. 'default'
  73. );
  74. $urls->addUrl(
  75. new UrlConcrete(
  76. $this->router->generate(
  77. 'praticiens_description',
  78. ['slugJob' => $speciality->getSlugJob()],
  79. UrlGeneratorInterface::ABSOLUTE_URL
  80. )
  81. ),
  82. 'default'
  83. );
  84. foreach ($cities as $city) {
  85. $urls->addUrl(
  86. new UrlConcrete(
  87. $this->router->generate(
  88. 'praticiens_fiche_ville',
  89. ['slugJob' => $speciality->getSlugJob(), 'slugCity' => $city->getSlug()],
  90. UrlGeneratorInterface::ABSOLUTE_URL
  91. )
  92. ),
  93. 'default'
  94. );
  95. }
  96. }
  97. $partners = $this->em->getRepository(Partner::class)->findBy(["ispractitioner" => true]);
  98. foreach ($partners as $partner) {
  99. if ($partner->getIsActivePractitioner()) {
  100. $urls->addUrl(
  101. new UrlConcrete(
  102. $this->router->generate(
  103. 'default_praticien_fiche',
  104. ['slug' => $partner->getSlug()],
  105. UrlGeneratorInterface::ABSOLUTE_URL
  106. )
  107. ),
  108. 'default'
  109. );
  110. }
  111. }
  112. }
  113. }