src/EventListener/SitemapSubscriber.php line 66

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Grigor
  5.  * Date: 11/20/19
  6.  * Time: 5:45 PM
  7.  */
  8. namespace App\EventListener;
  9. use App\Entity\Account;
  10. use App\Entity\ContentUnit;
  11. use App\Entity\Publication;
  12. use Doctrine\Common\Persistence\ManagerRegistry;
  13. use Symfony\Component\DependencyInjection\Container;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  18. use Presta\SitemapBundle\Service\UrlContainerInterface;
  19. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  20. class SitemapSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var UrlGeneratorInterface
  24.      */
  25.     private $urlGenerator;
  26.     /**
  27.      * @var ManagerRegistry
  28.      */
  29.     private $doctrine;
  30.     /**
  31.      * @var Container $container
  32.      */
  33.     private $container;
  34.     /**
  35.      * @param UrlGeneratorInterface $urlGenerator
  36.      * @param ManagerRegistry $doctrine
  37.      * @param ContainerInterface $container
  38.      */
  39.     public function __construct(UrlGeneratorInterface $urlGeneratorManagerRegistry $doctrineContainerInterface $container)
  40.     {
  41.         ini_set('memory_limit''256M');
  42.         $this->urlGenerator $urlGenerator;
  43.         $this->doctrine $doctrine;
  44.         $this->container $container;
  45.     }
  46.     /**
  47.      * @inheritdoc
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
  53.         ];
  54.     }
  55.     /**
  56.      * @param SitemapPopulateEvent $event
  57.      */
  58.     public function populate(SitemapPopulateEvent $event): void
  59.     {
  60.         $this->registerStaticPages($event->getUrlContainer());
  61.         $this->registerArticles($event->getUrlContainer());
  62.         $this->registerAuthors($event->getUrlContainer());
  63.         $this->registerPublications($event->getUrlContainer());
  64.     }
  65.     /**
  66.      * @param UrlContainerInterface $urls
  67.      */
  68.     public function registerStaticPages(UrlContainerInterface $urls): void
  69.     {
  70.         $domain $this->container->getParameter('router.request_context.scheme') . '://' $this->container->getParameter('router.request_context.host');
  71.         //  HOME PAGE
  72.         $urls->addUrl(
  73.             new UrlConcrete(
  74.                 $domain
  75.             ),
  76.             'static'
  77.         );
  78.     }
  79.     /**
  80.      * @param UrlContainerInterface $urls
  81.      */
  82.     public function registerArticles(UrlContainerInterface $urls): void
  83.     {
  84.         $domain $this->container->getParameter('router.request_context.scheme') . '://' $this->container->getParameter('router.request_context.host');
  85.         $timezone = new \DateTimeZone('UTC');
  86.         $time = new \DateTime();
  87.         $time->setTimezone($timezone);
  88.         /**
  89.          * @var ContentUnit[] $contentUnits
  90.          */
  91.         $contentUnits $this->doctrine->getRepository(ContentUnit::class)->getArticles(500);
  92.         while ($contentUnits) {
  93.             foreach ($contentUnits as $contentUnit) {
  94.                 if (!$contentUnit->getContent()) {
  95.                     continue;
  96.                 }
  97.                 $timestamp $contentUnit->getTransaction()->getTimeSigned();
  98.                 $time->setTimestamp($timestamp);
  99.                 $urls->addUrl(
  100.                     new UrlConcrete(
  101.                         $domain '/s/' $contentUnit->getUri(),
  102.                         $time
  103.                     ),
  104.                     'article'
  105.                 );
  106.             }
  107.             $contentUnits $this->doctrine->getRepository(ContentUnit::class)->getArticles(500$contentUnit);
  108.         }
  109.     }
  110.     /**
  111.      * @param UrlContainerInterface $urls
  112.      */
  113.     public function registerAuthors(UrlContainerInterface $urls): void
  114.     {
  115.         $domain $this->container->getParameter('router.request_context.scheme') . '://' $this->container->getParameter('router.request_context.host');
  116.         /**
  117.          * @var Account[] $authors
  118.          */
  119.         $authors $this->doctrine->getRepository(Account::class)->findAll();
  120.         foreach ($authors as $author) {
  121.             if (count($author->getAuthorContentUnits())) {
  122.                 $urls->addUrl(
  123.                     new UrlConcrete(
  124.                         $domain '/a/' $author->getPublicKey()
  125.                     ),
  126.                     'author'
  127.                 );
  128.             }
  129.         }
  130.     }
  131.     /**
  132.      * @param UrlContainerInterface $urls
  133.      */
  134.     public function registerPublications(UrlContainerInterface $urls): void
  135.     {
  136.         $domain $this->container->getParameter('router.request_context.scheme') . '://' $this->container->getParameter('router.request_context.host');
  137.         /**
  138.          * @var Publication[] $publications
  139.          */
  140.         $publications $this->doctrine->getRepository(Publication::class)->findAll();
  141.         foreach ($publications as $publication) {
  142.             $urls->addUrl(
  143.                 new UrlConcrete(
  144.                     $domain '/p/' $publication->getSlug()
  145.                 ),
  146.                 'publication'
  147.             );
  148.         }
  149.     }
  150. }