src/EventSubscriber/SitemapSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-18
  5.  * Time: 11:17
  6.  */
  7. namespace App\EventSubscriber;
  8. use App\Entity\Location\City;
  9. use App\Entity\Profile\BodyTypes;
  10. use App\Entity\Profile\HairColors;
  11. use App\Entity\Profile\Nationalities;
  12. use App\Entity\Profile\PrivateHaircuts;
  13. use App\Entity\Profile\Profile;
  14. use App\Entity\Service;
  15. use App\Entity\TakeOutLocations;
  16. use App\Entity\Saloon\Saloon;
  17. use App\Repository\CityRepository;
  18. use App\Repository\ProfileRepository;
  19. use App\Repository\SaloonRepository;
  20. use App\Repository\ServiceRepository;
  21. use App\Service\Features;
  22. use Carbon\Carbon;
  23. use Doctrine\Persistence\ManagerRegistry;
  24. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  25. use Presta\SitemapBundle\Service\UrlContainerInterface;
  26. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30. class SitemapSubscriber implements EventSubscriberInterface
  31. {
  32.     protected CityRepository $cityRepository;
  33.     protected ProfileRepository $profileRepository;
  34.     protected SaloonRepository $saloonRepository;
  35.     protected ServiceRepository $serviceRepository;
  36.     protected string $defaultCity;
  37.     private ?array $routeLocales;
  38.     public function __construct(
  39.         protected UrlGeneratorInterface $urlGenerator,
  40.         private Features $features,
  41.         ManagerRegistry $registry,
  42.         ParameterBagInterface $parameterBag
  43.     )
  44.     {
  45.         $this->defaultCity $parameterBag->get('default_city');
  46.         $this->cityRepository $registry->getManagerForClass(City::class)->getRepository(City::class);
  47.         $this->profileRepository $registry->getManagerForClass(Profile::class)->getRepository(Profile::class);
  48.         $this->saloonRepository $registry->getManagerForClass(Saloon::class)->getRepository(Saloon::class);
  49.         $this->serviceRepository $registry->getManagerForClass(Service::class)->getRepository(Service::class);
  50.         if ($this->features->has_translations()) {
  51.             $this->routeLocales $this->features->sitemap_multiple_locales()
  52.                 ? ['ru''en']
  53.                 : ['ru'];
  54.         } else {
  55.             $this->routeLocales null;
  56.         }
  57.     }
  58.     /**
  59.      * @inheritDoc
  60.      */
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
  65.         ];
  66.     }
  67.     public function populate(SitemapPopulateEvent $event): void
  68.     {
  69.         $urlContainer $event->getUrlContainer();
  70.         $this->registerCityUrls($urlContainerUrlConcrete::CHANGEFREQ_DAILY0.8);
  71.         $this->registerProfileUrls($urlContainerUrlConcrete::CHANGEFREQ_DAILY0.6);
  72.         if ($this->features->has_saloons()) {
  73.             $this->registerSaloonUrls($urlContainerUrlConcrete::CHANGEFREQ_DAILY0.2);
  74.         }
  75.     }
  76.     private function dateMutable(?\DateTimeImmutable $dateImmutable): ?\DateTime
  77.     {
  78.         if (null === $dateImmutable) {
  79.             return Carbon::now();
  80.         }
  81.         return Carbon::createFromTimestampUTC($dateImmutable->getTimestamp());
  82.     }
  83.     private function normalizeUriIdentity(string $value): string
  84.     {
  85.         $normalized strtolower(str_replace('_''-'$value));
  86.         return $normalized;
  87.     }
  88.     private function generateLocalizedUrls(string $canonicalRoute, array $routeParameters)
  89.     {
  90.         if (null === $this->routeLocales) {
  91.             yield $this->urlGenerator->generate($canonicalRoute$routeParametersUrlGeneratorInterface::ABSOLUTE_URL);
  92.         } else {
  93.             foreach ($this->routeLocales as $routeLocale) {
  94.                 yield $this->urlGenerator->generate("$canonicalRoute.$routeLocale"$routeParametersUrlGeneratorInterface::ABSOLUTE_URL);
  95.             }
  96.         }
  97.     }
  98.     protected function registerCityUrls(UrlContainerInterface $urlContainerstring $changeFrequency$priority)
  99.     {
  100.         $lastModified Carbon::now();
  101.         $homepageAsCityList $this->features->homepage_as_city_list();
  102.         foreach ($this->cityRepository->iterateAll() as $city) {
  103.             /** @var City $city */
  104.             // Если включена фича вывода списка городов на главной странице, добавляем в sitemap для всех городов (в том числе и для дефолтного) страницу фильтра по городу;
  105.             // Если фича выключена, то для дефолтного города не добавляем страницу фильтра анкет по городу - она уже будет добавлена как роут "homepage".
  106.             if ($homepageAsCityList || !$city->equals($this->defaultCity)) {
  107.                 foreach ($this->generateLocalizedUrls('profile_list.list_by_city', ['city' => $city->getUriIdentity()]) as $url) {
  108.                     $urlContainer->addUrl(new UrlConcrete(
  109.                         $url$lastModified$changeFrequency$priority
  110.                     ), 'cities');
  111.                 }
  112.             }
  113.             $this->registerCityLocationUrls($urlContainer$city$changeFrequency0.6);
  114.             $this->registerCityStaticUrls($urlContainer$city$changeFrequency0.4);
  115.         }
  116.     }
  117.     protected function registerCityLocationUrls(UrlContainerInterface $urlContainerCity $citystring $changeFrequency$priority)
  118.     {
  119.         $lastModified Carbon::now();
  120.         foreach ($city->getCounties() as $county) {
  121.             foreach ($this->generateLocalizedUrls('profile_list.list_by_county', ['city' => $city->getUriIdentity(), 'county' => $county->getUriIdentity()]) as $url) {
  122.                 $urlContainer->addUrl(new UrlConcrete(
  123.                     $url$lastModified$changeFrequency$priority
  124.                 ), 'cities');
  125.             }
  126.         }
  127.         foreach ($city->getDistricts() as $district) {
  128.             foreach ($this->generateLocalizedUrls('profile_list.list_by_district', ['city' => $city->getUriIdentity(), 'district' => $district->getUriIdentity()]) as $url) {
  129.                 $urlContainer->addUrl(new UrlConcrete(
  130.                     $url$lastModified$changeFrequency$priority
  131.                 ), 'cities');
  132.             }
  133.         }
  134.         foreach ($city->getStations() as $station) {
  135.             foreach ($this->generateLocalizedUrls('profile_list.list_by_station', ['city' => $city->getUriIdentity(), 'station' => $station->getUriIdentity()]) as $url) {
  136.                 $urlContainer->addUrl(new UrlConcrete(
  137.                     $url$lastModified$changeFrequency$priority
  138.                 ), 'cities');
  139.             }
  140.         }
  141.         foreach ($this->generateLocalizedUrls('map.page', ['city' => $city->getUriIdentity()]) as $url) {
  142.             $urlContainer->addUrl(new UrlConcrete(
  143.                 $url$lastModified$changeFrequency$priority
  144.             ), 'cities');
  145.         }
  146.     }
  147.     protected function registerCityStaticUrls(UrlContainerInterface $urlContainerCity $citystring $changeFrequency$priority)
  148.     {
  149.         $lastModified Carbon::now();
  150.         if ($this->features->has_masseurs()) {
  151.             foreach ($this->generateLocalizedUrls('masseur_list.page', ['city' => $city->getUriIdentity()]) as $url) {
  152.                 $urlContainer->addUrl(new UrlConcrete(
  153.                     $url$lastModified$changeFrequency$priority
  154.                 ), 'profiles');
  155.             }
  156.         }
  157.         if ($this->features->has_saloons()) {
  158.             foreach ($this->generateLocalizedUrls('saloon_list.list_by_city', ['city' => $city->getUriIdentity()]) as $url) {
  159.                 $urlContainer->addUrl(new UrlConcrete(
  160.                     $url$lastModified$changeFrequency$priority
  161.                 ), 'profiles');
  162.             }
  163.         }
  164.         if ($this->features->has_archive_page()) {
  165.             foreach ($this->generateLocalizedUrls('profile_list.list_archived', ['city' => $city->getUriIdentity()]) as $url) {
  166.                 $urlContainer->addUrl(new UrlConcrete(
  167.                     $url$lastModified$changeFrequency$priority
  168.                 ), 'profiles');
  169.             }
  170.         }
  171.         foreach ($this->serviceRepository->iterateAll() as $service) {
  172.             /** @var \App\Entity\Service $service */
  173.             foreach ($this->generateLocalizedUrls('profile_list.list_by_provided_service', ['city' => $city->getUriIdentity(), 'service' => $service->getUriIdentity()]) as $url) {
  174.                 $urlContainer->addUrl(new UrlConcrete(
  175.                     $url$lastModified$changeFrequency$priority
  176.                 ), 'profiles');
  177.             }
  178.         }
  179.     }
  180.     protected function registerProfileUrls(UrlContainerInterface $urlContainerstring $changeFrequency$priority)
  181.     {
  182.         foreach ($this->profileRepository->sitemapItemsIterator() as $profile) {
  183.             foreach ($this->generateLocalizedUrls('profile_preview.page', ['city' => $profile['city_uri'], 'profile' => $profile['uri']]) as $url) {
  184.                 $urlContainer->addUrl(new UrlConcrete(
  185.                     $url$this->dateMutable($profile['updatedAt']), $changeFrequency$priority
  186.                 ), 'profiles');
  187.             }
  188.         }
  189.     }
  190.     protected function registerSaloonUrls(UrlContainerInterface $urlContainerstring $changeFrequency$priority)
  191.     {
  192.         foreach ($this->saloonRepository->sitemapItemsIterator() as $saloon) {
  193.             foreach ($this->generateLocalizedUrls('saloon_preview.page', ['city' => $saloon['city_uri'], 'saloon' => $saloon['uri']]) as $url) {
  194.                 $urlContainer->addUrl(new UrlConcrete(
  195.                     $url$this->dateMutable($saloon['updatedAt']), $changeFrequency$priority
  196.                 ), 'saloons');
  197.             }
  198.         }
  199.     }
  200. }