src/Aqarmap/Bundle/MainBundle/EventListener/SiteMapListener.php line 60

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\MainBundle\EventListener;
  3. use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionStatus;
  4. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  5. use Aqarmap\Bundle\MainBundle\Service\Setting;
  6. use Aqarmap\Bundle\MainBundle\Service\SiteMapService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  9. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. /**
  16.  * Sitemap Listener.
  17.  */
  18. class SiteMapListener implements EventSubscriberInterface
  19. {
  20.     /** @var RouterInterface */
  21.     private $router;
  22.     /** @var EntityManagerInterface */
  23.     private $em;
  24.     /** @var array */
  25.     private $locales;
  26.     /** @var SiteMapService */
  27.     private $siteMapService;
  28.     /**
  29.      * @throws \Exception
  30.      * @throws \Throwable
  31.      */
  32.     public function __construct(
  33.         EntityManagerInterface $em,
  34.         RouterInterface $router,
  35.         TranslatorInterface $translator,
  36.         SiteMapService $siteMapService,
  37.         Setting $settingService,
  38.         ParameterBagInterface $parameterBag
  39.     ) {
  40.         $this->router $router;
  41.         $this->em $em;
  42.         $this->siteMapService $siteMapService;
  43.         $this->locales $parameterBag->get('locales');
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             SitemapPopulateEvent::class => 'populate',
  49.         ];
  50.     }
  51.     public function populate(SitemapPopulateEvent $event): void
  52.     {
  53.         $section $event->getSection() ?: 'default';
  54.         if (\in_array($section, ['default''neighborhood'])) {
  55.             echo 'Generating Neighborhoods Links'.\PHP_EOL;
  56.             $this->generateNeighborhoodsLinks($event);
  57.         }
  58.         if (\in_array($section, ['default''latest-listings'])) {
  59.             echo 'Generating Latest Listings Links'.\PHP_EOL;
  60.             $this->generateLatestListingsLinks($event);
  61.         }
  62.         if (\in_array($section, ['default''search-results''search-results-listing-only'])) {
  63.             echo 'Generating Search Results Links without locations'.\PHP_EOL;
  64.             $this->generateSearchResultLinks($event);
  65.         }
  66.         if (\in_array($section, ['default''search-results-locations''search-results-location-listing-only'])) {
  67.             echo 'Generating Search Results Links with locations'.\PHP_EOL;
  68.             $this->generateSearchResultLocationLinks($event);
  69.         }
  70.         if (\in_array($section, ['default''projects-luxury'])) {
  71.             echo 'Generating Luxury Projects Links'.\PHP_EOL;
  72.             $this->generateProjectsLuxuryLinks($event);
  73.         }
  74.         if (\in_array($section, ['default''compounds'])) {
  75.             echo 'Generating Compounds Links'.\PHP_EOL;
  76.             $this->generateCompoundsLinks($event);
  77.         }
  78.         if (\in_array($section, ['default''ask-neighbors'])) {
  79.             echo 'Generating Discussions Links'.\PHP_EOL;
  80.             $this->generateDiscussionsLinks($event);
  81.         }
  82.     }
  83.     public function generateNeighborhoodsLinks(SitemapPopulateEvent $event): void
  84.     {
  85.         $neighborhoods $this->em
  86.             ->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Location::class)->getNeighbourhoodLocations();
  87.         foreach ($this->locales as $locale) {
  88.             foreach ($neighborhoods as $location) {
  89.                 $url $this->router->generate(
  90.                     'neighborhood_main_page',
  91.                     ['location_slug' => $location->getSlug(), '_locale' => $locale],
  92.                     UrlGeneratorInterface::ABSOLUTE_URL
  93.                 );
  94.                 // add homepage url to the urlset named default
  95.                 $event->getUrlContainer()->addUrl(
  96.                     new UrlConcrete(
  97.                         $url,
  98.                         new \DateTime(),
  99.                         UrlConcrete::CHANGEFREQ_HOURLY,
  100.                         0.8
  101.                     ),
  102.                     'neighborhood'
  103.                 );
  104.             }
  105.         }
  106.     }
  107.     public function generateCompoundsLinks(SitemapPopulateEvent $event): void
  108.     {
  109.         $compounds $this
  110.             ->em
  111.             ->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\CompoundLocation::class)
  112.             ->getAllEnabled()
  113.         ;
  114.         foreach ($this->locales as $locale) {
  115.             foreach ($compounds as $location) {
  116.                 $url $this->router->generate(
  117.                     'compound_search_with_location',
  118.                     ['location' => $location->getSlug(), '_locale' => $locale],
  119.                     UrlGeneratorInterface::ABSOLUTE_URL
  120.                 );
  121.                 // add homepage url to the urlset named default
  122.                 $event->getUrlContainer()->addUrl(
  123.                     new UrlConcrete(
  124.                         $url,
  125.                         new \DateTime(),
  126.                         UrlConcrete::CHANGEFREQ_HOURLY,
  127.                         0.4
  128.                     ),
  129.                     'compounds'
  130.                 );
  131.             }
  132.         }
  133.     }
  134.     public function generateLatestListingsLinks(SitemapPopulateEvent $event): void
  135.     {
  136.         $listings $this->em
  137.             ->getRepository(Listing::class)->getLatestListings(100)->getQuery()->getResult();
  138.         foreach ($this->locales as $locale) {
  139.             foreach ($listings as $listing) {
  140.                 if (!$listing->getSlug() || empty($listing->getSlug())) {
  141.                     continue;
  142.                 }
  143.                 $url $this->router->generate(
  144.                     'listing_slug',
  145.                     ['id' => $listing->getId(), 'slug' => $listing->getSlug(), '_locale' => $locale],
  146.                     UrlGeneratorInterface::ABSOLUTE_URL
  147.                 );
  148.                 // add homepage url to the urlset named default
  149.                 $event->getUrlContainer()->addUrl(
  150.                     new UrlConcrete(
  151.                         $url,
  152.                         new \DateTime(),
  153.                         UrlConcrete::CHANGEFREQ_DAILY,
  154.                         0.5
  155.                     ),
  156.                     'latest-listings'
  157.                 );
  158.             }
  159.         }
  160.     }
  161.     public function generateSearchResultLocationLinks(SitemapPopulateEvent $event): void
  162.     {
  163.         foreach ($this->siteMapService->generateSearchResultLocationLinks()->iterate() as $searchResults) {
  164.             $searchResults current($searchResults);
  165.             foreach ($this->locales as $locale) {
  166.                 $url $this->router->generate(
  167.                     'search',
  168.                     ['section_slug' => $searchResults['sectionSlug'],
  169.                         'property_type_slug' => $searchResults['propertyTypeSlug'],
  170.                         'location_slug' => $searchResults['locationSlug'],
  171.                         '_locale' => $locale,
  172.                     ],
  173.                     UrlGeneratorInterface::ABSOLUTE_URL
  174.                 );
  175.                 // Set priority based on location level field
  176.                 $locationPriority = [
  177.                     /* Cities */
  178.                     => 1,
  179.                     /* Neighborhoods */
  180.                     => 0.9,
  181.                     /* Sub Locations */
  182.                     => 0.8,
  183.                     /* Sub-sub Locations */
  184.                     => 0.7,
  185.                     /* Sub-sub-sub Locations */
  186.                     => 0.6,
  187.                     /* Sub-sub-sub-sub Locations */
  188.                     => 0.5,
  189.                 ];
  190.                 $level $searchResults['locationLevel'];
  191.                 // add homepage url to the urlset named default
  192.                 $event->getUrlContainer()->addUrl(
  193.                     new UrlConcrete(
  194.                         $url,
  195.                         new \DateTime(),
  196.                         UrlConcrete::CHANGEFREQ_HOURLY,
  197.                         $locationPriority[$level] ?: 1
  198.                     ),
  199.                     'search-results-locations'
  200.                 );
  201.             }
  202.         }
  203.     }
  204.     public function generateSearchResultLinks(SitemapPopulateEvent $event): void
  205.     {
  206.         $sections $this->em
  207.             ->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Section::class)->getSearchableSection();
  208.         $propertyTypes $this->em
  209.             ->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\PropertyType::class)->getPropertyTypesList();
  210.         foreach ($this->locales as $locale) {
  211.             foreach ($sections as $section) {
  212.                 foreach ($propertyTypes as $propertyType) {
  213.                     $url $this->router->generate(
  214.                         'property_type_search',
  215.                         ['section_slug' => $section->getSlug(),
  216.                             'property_type_slug' => $propertyType->getSlug(),
  217.                             '_locale' => $locale,
  218.                         ],
  219.                         UrlGeneratorInterface::ABSOLUTE_URL
  220.                     );
  221.                     // add homepage url to the urlset named default
  222.                     $event->getUrlContainer()->addUrl(
  223.                         new UrlConcrete(
  224.                             $url,
  225.                             new \DateTime(),
  226.                             UrlConcrete::CHANGEFREQ_HOURLY,
  227.                             1
  228.                         ),
  229.                         'search-results'
  230.                     );
  231.                 }
  232.                 $url $this->router->generate(
  233.                     'homepage',
  234.                     ['_locale' => $locale],
  235.                     UrlGeneratorInterface::ABSOLUTE_URL
  236.                 );
  237.                 // add homepage url to the urlset named default
  238.                 $event->getUrlContainer()->addUrl(
  239.                     new UrlConcrete(
  240.                         $url,
  241.                         new \DateTime(),
  242.                         UrlConcrete::CHANGEFREQ_HOURLY,
  243.                         1
  244.                     ),
  245.                     'search-results'
  246.                 );
  247.                 $url $this->router->generate(
  248.                     'homepage',
  249.                     ['_locale' => $locale],
  250.                     UrlGeneratorInterface::ABSOLUTE_URL
  251.                 );
  252.                 // add homepage url to the urlset named default
  253.                 $event->getUrlContainer()->addUrl(
  254.                     new UrlConcrete(
  255.                         $url,
  256.                         new \DateTime(),
  257.                         UrlConcrete::CHANGEFREQ_HOURLY,
  258.                         1
  259.                     ),
  260.                     'search-results'
  261.                 );
  262.             }
  263.         }
  264.     }
  265.     public function generateProjectsLuxuryLinks(SitemapPopulateEvent $event): void
  266.     {
  267.         $sections $this->em
  268.             ->getRepository(\Aqarmap\Bundle\ListingBundle\Entity\Section::class)->getNonSearchableSection();
  269.         foreach ($this->locales as $locale) {
  270.             foreach ($sections as $section) {
  271.                 $listings $this->em
  272.                     ->getRepository(Listing::class)->search(['section' => $section])->getQuery()->getResult();
  273.                 $listings array_column($listings'listing');
  274.                 foreach ($listings as $listing) {
  275.                     if ($listing->getSlug()) {
  276.                         $url $this->router->generate(
  277.                             'listing_slug',
  278.                             [
  279.                                 'id' => $listing->getId(),
  280.                                 'slug' => $listing->getSlug(),
  281.                                 '_locale' => $locale,
  282.                             ],
  283.                             UrlGeneratorInterface::ABSOLUTE_URL
  284.                         );
  285.                         // add homepage url to the urlset named default
  286.                         $event->getUrlContainer()->addUrl(
  287.                             new UrlConcrete(
  288.                                 $url,
  289.                                 new \DateTime(),
  290.                                 UrlConcrete::CHANGEFREQ_HOURLY,
  291.                                 1
  292.                             ),
  293.                             'projects-luxury'
  294.                         );
  295.                     }
  296.                 }
  297.             }
  298.         }
  299.     }
  300.     public function generateDiscussionsLinks(SitemapPopulateEvent $event): void
  301.     {
  302.         $discussions $this
  303.             ->em
  304.             ->getRepository('AqarmapDiscussionBundle:Discussion')
  305.             ->filter([
  306.                 'status' => DiscussionStatus::APPROVED,
  307.             ])
  308.         ;
  309.         foreach ($this->locales as $locale) {
  310.             foreach ($discussions as $discussion) {
  311.                 $url $this->router->generate(
  312.                     'neighborhood_discussion_comments',
  313.                     ['discussion' => $discussion->getId(), '_locale' => $locale],
  314.                     UrlGeneratorInterface::ABSOLUTE_URL
  315.                 );
  316.                 $event->getUrlContainer()->addUrl(
  317.                     new UrlConcrete(
  318.                         $url,
  319.                         new \DateTime(),
  320.                         UrlConcrete::CHANGEFREQ_HOURLY,
  321.                         0.8
  322.                     ),
  323.                     'ask-neighbors'
  324.                 );
  325.             }
  326.         }
  327.     }
  328.     public function generateListingsLinks(SitemapPopulateEvent $event): void
  329.     {
  330.         $listingRepository $this->em->getRepository(Listing::class);
  331.         $count $listingRepository->countAllLiveListings();
  332.         echo 'We have '.$count.' Live Listings'.\PHP_EOL;
  333.         $limit 400;
  334.         $chunk floor($count $limit);
  335.         echo 'We have '.$chunk.' Number of Chunks'.\PHP_EOL;
  336.         $locales $this->locales;
  337.         for ($i 0$i <= $chunk; ++$i) {
  338.             $listings $listingRepository->getSiteMapListings($limit$i $limit)->iterate();
  339.             foreach ($listings as $listing) {
  340.                 /** @var Listing $listing */
  341.                 $listing $listing[0];
  342.                 foreach ($locales as $locale) {
  343.                     if (!$listing->getSlug() || empty($listing->getSlug())) {
  344.                         continue;
  345.                     }
  346.                     $url $this->router->generate(
  347.                         'listing_slug',
  348.                         [
  349.                             'id' => $listing->getId(),
  350.                             'slug' => $listing->getSlug(),
  351.                             '_locale' => $locale,
  352.                         ],
  353.                         UrlGeneratorInterface::ABSOLUTE_URL
  354.                     );
  355.                     // add homepage url to the urlset named default
  356.                     $event->getUrlContainer()->addUrl(
  357.                         new UrlConcrete(
  358.                             $url,
  359.                             new \DateTime(),
  360.                             UrlConcrete::CHANGEFREQ_HOURLY,
  361.                             0.5
  362.                         ),
  363.                         'lisitngs-'.$i
  364.                     );
  365.                 }
  366.                 $this->em->detach($listing);
  367.             }
  368.             echo 'Done with Chunk number '.$i.\PHP_EOL;
  369.         }
  370.     }
  371. }