src/Hitso/Bundle/SeoBundle/EventListener/SeoAwareSubscriber.php line 97

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\SeoBundle\EventListener;
  4. use Hitso\Bundle\CommonBundle\Helper\Router\RouterHelper;
  5. use Hitso\Bundle\SeoBundle\Manager\SeoPageManager;
  6. use Hitso\Bundle\SeoBundle\Repository\SeoPageRepository;
  7. use Hitso\Bundle\SeoBundle\Service\SeoPresentation;
  8. use Sonata\SeoBundle\Seo\SeoPage;
  9. use Symfony\Cmf\Bundle\SeoBundle\SeoAwareInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\FilterControllerArgumentsEvent;
  12. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. /**
  15.  * Class SeoAwareSubscriber
  16.  *
  17.  * @package Hitso\Bundle\SeoBundle\EventListener
  18.  */
  19. class SeoAwareSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var SeoPresentation
  23.      */
  24.     protected $seoPresentation;
  25.     /**
  26.      * @var SeoPage
  27.      */
  28.     protected $seoPage;
  29.     /**
  30.      * @var SeoPageManager
  31.      */
  32.     protected $seoPageRepository;
  33.     /**
  34.      * @var RouterHelper
  35.      */
  36.     protected $routerHelper;
  37.     /**
  38.      * SeoAwareSubscriber constructor.
  39.      *
  40.      * @param SeoPresentation   $seoPresentation
  41.      * @param SeoPage           $seoPage
  42.      * @param SeoPageRepository $seoPageRepository
  43.      * @param RouterHelper      $routerHelper
  44.      */
  45.     public function __construct(
  46.         SeoPresentation $seoPresentation,
  47.         SeoPage $seoPage,
  48.         SeoPageRepository $seoPageRepository,
  49.         RouterHelper $routerHelper
  50.     ) {
  51.         $this->seoPresentation   $seoPresentation;
  52.         $this->seoPage           $seoPage;
  53.         $this->seoPageRepository $seoPageRepository;
  54.         $this->routerHelper      $routerHelper;
  55.     }
  56.     /**
  57.      * @param FilterControllerEvent $event
  58.      */
  59.     public function onKernelController(FilterControllerEvent $event)
  60.     {
  61.         $request $event->getRequest();
  62.         $context $request->attributes->get('_firewall_context');
  63.         if ('security.firewall.map.context.admin' === $context || 'security.firewall.map.context.api') {
  64.             return;
  65.         }
  66.         if ($request->attributes->has('_route')) {
  67.             $route       $request->attributes->get('_route');
  68.             $routeParams $request->attributes->get('_route_params', []);
  69.             $url         $this->routerHelper->generateUrl($route$routeParams0);
  70.             $seoAware    $this->seoPageRepository->getPageByRoute($route);
  71.             if ($seoAware instanceof SeoAwareInterface) {
  72.                 if (strlen($seoAware->getSeoMetadata()->getOriginalUrl())) {
  73.                     $url $seoAware->getSeoMetadata()->getOriginalUrl();
  74.                 }
  75.                 $this->seoPresentation->updateSeoPage($seoAware);
  76.             }
  77.             $this->seoPage->setLinkCanonical($url);
  78.         }
  79.     }
  80.     /**
  81.      * @param FilterControllerArgumentsEvent $event
  82.      */
  83.     public function onControllerArguments(FilterControllerArgumentsEvent $event)
  84.     {
  85.         $arguments $event->getArguments();
  86.         foreach ($arguments as $argument) {
  87.             if ($argument instanceof SeoAwareInterface) {
  88.                 $seoMetadata $argument->getSeoMetadata();
  89.                 if ($seoMetadata) {
  90.                     $this->seoPresentation->updateSeoPage($seoMetadata);
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     /**
  96.      * @return array
  97.      */
  98.     public static function getSubscribedEvents()
  99.     {
  100.         return [
  101.             KernelEvents::CONTROLLER           => [['onKernelController'1]],
  102.             KernelEvents::CONTROLLER_ARGUMENTS => ['onControllerArguments'],
  103.         ];
  104.     }
  105. }