src/EventListener/SitemapSubscriber.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Blog;
  4. //use App\Entity\Category;
  5. use App\Entity\Career;
  6. use App\Entity\News;
  7. use App\Entity\Office;
  8. use App\Entity\Product;
  9. //use App\Entity\TextPage;
  10. //use App\Service\RouteBuilderService;
  11. use App\Entity\Promotion;
  12. use App\SiteMap\SiteMapService;
  13. use DateTime;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  16. use Presta\SitemapBundle\Service\UrlContainerInterface;
  17. use Presta\SitemapBundle\Sitemap\Url\GoogleImage;
  18. use Presta\SitemapBundle\Sitemap\Url\GoogleImageUrlDecorator;
  19. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. class SitemapSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var UrlGeneratorInterface
  26.      */
  27.     private $urlGenerator;
  28.     /**
  29.      * @var ManagerRegistry
  30.      */
  31.     private $doctrine;
  32.     private $locales;
  33.     private $productAndCategoryRouteBuilder;
  34.     private $baseURL;
  35.     private $uploadsDir;
  36.     private $defaultLocale;
  37.     private $siteMapService;
  38.     /**
  39.      * SitemapSubscriber constructor.
  40.      * @param UrlGeneratorInterface $urlGenerator
  41.      * @param ManagerRegistry $doctrine
  42.      * @param $defaultLocale
  43.      * @param $localesArr
  44.      * @param SiteMapService $siteMapService
  45.      * @param $uploadsDir
  46.      * @param $siteBaseUrl
  47.      */
  48.     public function __construct(UrlGeneratorInterface $urlGeneratorManagerRegistry $doctrine$defaultLocale$localesArrSiteMapService $siteMapService/*RouteBuilderService $productAndCategoryRouteBuilder,*/
  49.                                 $uploadsDir$siteBaseUrl)
  50.     {
  51.         $this->urlGenerator $urlGenerator;
  52.         $this->doctrine $doctrine;
  53.         $this->locales $localesArr;
  54.         $this->defaultLocale $defaultLocale;
  55. //        $this->productAndCategoryRouteBuilder = $productAndCategoryRouteBuilder;
  56.         $this->baseURL $siteBaseUrl;
  57.         $this->uploadsDir $uploadsDir;
  58.         $this->siteMapService $siteMapService;
  59.     }
  60.     /**
  61.      * @inheritdoc
  62.      */
  63.     public static function getSubscribedEvents()
  64.     {
  65.         return [
  66.             SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
  67.         ];
  68.     }
  69.     /**
  70.      * @param SitemapPopulateEvent $event
  71.      */
  72.     public function populate(SitemapPopulateEvent $event): void
  73.     {
  74.         $this->registerSiteUrls($event->getUrlContainer());
  75.         $this->registerBlogPostsUrls($event->getUrlContainer());
  76.         $this->registerNewsUrls($event->getUrlContainer());
  77.         $this->registerOfficesUrls($event->getUrlContainer());
  78.         $this->registerProductsUrls($event->getUrlContainer());
  79.         $this->registerCareersUrls($event->getUrlContainer());
  80.         $this->registerPromotionsUrls($event->getUrlContainer());
  81. //        $this->registerBlogPostCategoriesUrls($event->getUrlContainer());
  82. //        $this->registerTextPagesUrls($event->getUrlContainer());
  83. //        $this->registerProductsUrls($event->getUrlContainer());
  84. //        $this->registerProductCategoriesUrls($event->getUrlContainer());
  85.     }
  86.     /**
  87.      * @param UrlContainerInterface $urls
  88.      * @throws \Psr\Cache\InvalidArgumentException
  89.      * @throws \ReflectionException
  90.      */
  91.     public function registerSiteUrls(UrlContainerInterface $urls): void
  92.     {
  93.         $collection $this->siteMapService->routesForSiteMap(true)->itemsForLocale();
  94. //        foreach ($this->locales as $locale) {
  95.         foreach ($collection as $siteUrl) {
  96.             $priority 1;
  97. //                $lastMod = $post->getUpdatedAt() ?? $post->getCreatedAt();
  98.             $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  99.             $url = (new UrlConcrete(
  100.                 $this->baseURL $siteUrl->path
  101.             ))->setChangefreq($changeFreq)
  102.                 ->setPriority($priority)//                    ->setLastmod($lastMod)
  103.             ;
  104.             $urls->addUrl(
  105.                 $url,
  106.                 'site'
  107.             );
  108.         }
  109. //        }
  110.     }
  111.     /**
  112.      * @param UrlContainerInterface $urls
  113.      */
  114.     public function registerBlogPostsUrls(UrlContainerInterface $urls): void
  115.     {
  116.         $postsCount $this->doctrine->getRepository(Blog::class)->getBlogPostsCount();
  117.         $limit 100;
  118.         $counter 0;
  119.         while ($counter $postsCount) {
  120.             $posts $this->doctrine->getRepository(Blog::class)->getBlogPostsByLimit($counter$limit);
  121.             foreach ($this->locales as $locale) {
  122.                 foreach ($posts as $post) {
  123.                     $post->setTranslatableLocale($locale);
  124.                     $this->doctrine->getManager()->refresh($post);
  125.                     $priority 1;
  126.                     $lastMod $post->getUpdatedAt() ?? $post->getCreatedAt();
  127.                     $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  128. //                    $category = $post->getCategory();
  129. //                    $category->setTranslatableLocale($locale);
  130. //                    $this->doctrine->getManager()->refresh($category);
  131.                     $url = (new UrlConcrete(
  132.                         $this->urlGenerator->generate(
  133.                             'blog.show',
  134.                             [
  135. //                                '_locale' => $locale,
  136.                                 'slug' => $post->getSlug(),
  137. //                                'route' => $post->getRoute(),
  138.                             ],
  139.                             UrlGeneratorInterface::ABSOLUTE_URL
  140.                         )
  141.                     ))->setChangefreq($changeFreq)
  142.                         ->setPriority($priority)
  143.                         ->setLastmod($lastMod);
  144.                     //Add images
  145. //                    $gallery = $post->getImage();
  146. //                    $gallery->setTranslatableLocale($locale);
  147. //                    $this->doctrine->getManager()->refresh($gallery);
  148.                     $url = new GoogleImageUrlDecorator($url);
  149.                     $imagePath $post->getImage();
  150. //                    $imageRelativeURL = $image;
  151.                     if ($imagePath$url->addImage(new GoogleImage($imagePath));
  152. //                    if ($gallery !== null) {
  153. //                        $mediaItems = $gallery->getMediaItems();
  154. //                        $url = new GoogleImageUrlDecorator($url);
  155. //
  156. //                        foreach ($mediaItems as $mediaItem) {
  157. //                            if ($mediaItem->isImage()) {
  158. //                                $image = $mediaItem->getImage();
  159. //                                $imageRelativeURL = $this->uploadsDir . '/' . $image->getFileName();
  160. //                                $url->addImage(new GoogleImage($this->baseURL . '/' . $imageRelativeURL));
  161. //                            }
  162. //                        }
  163. //                    }
  164.                     $urls->addUrl(
  165.                         $url,
  166.                         'blog-posts'
  167.                     );
  168.                 }
  169.             }
  170.             $counter += $limit;
  171.         }
  172.     }
  173.     /**
  174.      * @param UrlContainerInterface $urls
  175.      */
  176.     public function registerNewsUrls(UrlContainerInterface $urls): void
  177.     {
  178.         $posts $this->doctrine->getRepository(News::class)->findAll();
  179.         foreach ($this->locales as $locale) {
  180.             foreach ($posts as $post) {
  181.                 $post->setTranslatableLocale($locale);
  182.                 $priority 1;
  183.                 $lastMod $post->getUpdatedAt() ?? $post->getCreatedAt();
  184.                 $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  185.                 $url = (new UrlConcrete(
  186.                     $this->urlGenerator->generate(
  187.                         'news.show',
  188.                         [
  189.                             'slug' => $post->getSlug(),
  190.                         ],
  191.                         UrlGeneratorInterface::ABSOLUTE_URL
  192.                     )
  193.                 ))->setChangefreq($changeFreq)
  194.                     ->setPriority($priority)
  195.                     ->setLastmod($lastMod);
  196.                 $url = new GoogleImageUrlDecorator($url);
  197.                 $imagePath $post->getImage();
  198.                 if ($imagePath$url->addImage(new GoogleImage($imagePath));
  199.                 $urls->addUrl(
  200.                     $url,
  201.                     'news'
  202.                 );
  203.             }
  204.         }
  205.     }
  206.     /**
  207.      * @param UrlContainerInterface $urls
  208.      */
  209.     public function registerOfficesUrls(UrlContainerInterface $urls): void
  210.     {
  211.         $posts $this->doctrine->getRepository(Office::class)->findBy(['isActive' => 1]);
  212.         foreach ($this->locales as $locale) {
  213.             foreach ($posts as $post) {
  214. //                $post->setTranslatableLocale($locale);
  215.                 $priority 1;
  216.                 $lastMod $post->getUpdatedAt() ?? $post->getCreatedAt();
  217.                 $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  218.                 $url = (new UrlConcrete(
  219.                     $this->urlGenerator->generate(
  220.                         'office.view',
  221.                         [
  222.                             'slug' => $post->getSlug(),
  223.                         ],
  224.                         UrlGeneratorInterface::ABSOLUTE_URL
  225.                     )
  226.                 ))->setChangefreq($changeFreq)
  227.                     ->setPriority($priority)
  228.                     ->setLastmod($lastMod);
  229.                 $url = new GoogleImageUrlDecorator($url);
  230.                 $imagePath $post->getImage();
  231.                 if ($imagePath$url->addImage(new GoogleImage($imagePath));
  232.                 $urls->addUrl(
  233.                     $url,
  234.                     'offices'
  235.                 );
  236.             }
  237.         }
  238.     }
  239.     /**
  240.      * @param UrlContainerInterface $urls
  241.      */
  242.     public function registerProductsUrls(UrlContainerInterface $urls): void
  243.     {
  244.         $posts $this->doctrine->getRepository(Product::class)->findBy(['active' => 1]);
  245.         foreach ($this->locales as $locale) {
  246.             foreach ($posts as $post) {
  247. //                $post->setTranslatableLocale($locale);
  248.                 $priority 1;
  249.                 $lastMod $post->getUpdatedAt() ?? $post->getCreatedAt();
  250.                 $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  251.                 $url = (new UrlConcrete(
  252.                     $this->urlGenerator->generate(
  253.                         'products.details',
  254.                         [
  255.                             'slug' => $post->getSlug(),
  256.                         ],
  257.                         UrlGeneratorInterface::ABSOLUTE_URL
  258.                     )
  259.                 ))->setChangefreq($changeFreq)
  260.                     ->setPriority($priority)
  261.                     ->setLastmod($lastMod);
  262.                 $url = new GoogleImageUrlDecorator($url);
  263.                 $imagePath $post->getImage();
  264.                 if ($imagePath$url->addImage(new GoogleImage($imagePath));
  265.                 $urls->addUrl(
  266.                     $url,
  267.                     'products'
  268.                 );
  269.             }
  270.         }
  271.     }
  272.     /**
  273.      * @param UrlContainerInterface $urls
  274.      */
  275.     public function registerCareersUrls(UrlContainerInterface $urls): void
  276.     {
  277.         $posts $this->doctrine->getRepository(Career::class)->findAll();
  278.         foreach ($this->locales as $locale) {
  279.             foreach ($posts as $post) {
  280. //                $post->setTranslatableLocale($locale);
  281.                 $priority 1;
  282.                 $lastMod $post->getUpdatedAt() ?? $post->getCreatedAt();
  283.                 $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  284.                 $url = (new UrlConcrete(
  285.                     $this->urlGenerator->generate(
  286.                         'career.view',
  287.                         [
  288.                             'slug' => $post->getSlug(),
  289.                         ],
  290.                         UrlGeneratorInterface::ABSOLUTE_URL
  291.                     )
  292.                 ))->setChangefreq($changeFreq)
  293.                     ->setPriority($priority)
  294.                     ->setLastmod($lastMod);
  295. //                $url = new GoogleImageUrlDecorator($url);
  296. //                $imagePath = $post->getImage();
  297. //                if ($imagePath) $url->addImage(new GoogleImage($imagePath));
  298.                 $urls->addUrl(
  299.                     $url,
  300.                     'careers'
  301.                 );
  302.             }
  303.         }
  304.     }
  305.     /**
  306.      * @param UrlContainerInterface $urls
  307.      */
  308.     public function registerPromotionsUrls(UrlContainerInterface $urls): void
  309.     {
  310.         $posts $this->doctrine->getRepository(Promotion::class)->findAll();
  311.         foreach ($this->locales as $locale) {
  312.             foreach ($posts as $post) {
  313. //                $post->setTranslatableLocale($locale);
  314.                 $priority 1;
  315.                 $lastMod $post->getUpdatedAt() ?? $post->getCreatedAt();
  316.                 $changeFreq UrlConcrete::CHANGEFREQ_WEEKLY;
  317.                 $url = (new UrlConcrete(
  318.                     $this->urlGenerator->generate(
  319.                         'promotion.view',
  320.                         [
  321.                             'slug' => $post->getSlug(),
  322.                         ],
  323.                         UrlGeneratorInterface::ABSOLUTE_URL
  324.                     )
  325.                 ))->setChangefreq($changeFreq)
  326.                     ->setPriority($priority)
  327.                     ->setLastmod($lastMod);
  328.                 $url = new GoogleImageUrlDecorator($url);
  329.                 $imagePath $post->getImage();
  330.                 if ($imagePath$url->addImage(new GoogleImage($imagePath));
  331.                 $urls->addUrl(
  332.                     $url,
  333.                     'promotions'
  334.                 );
  335.             }
  336.         }
  337.     }
  338. }