vendor/elements/cache-helper-bundle/src/Elements/Bundle/CacheHelperBundle/EventListener/SeoHelperListener.php line 60

Open in your IDE?
  1. <?php
  2. namespace Elements\Bundle\CacheHelperBundle\EventListener;
  3. use Elements\Bundle\CacheHelperBundle\Service\SeoHelper\Handler\GenericSeoHelperHandler;
  4. use Elements\Bundle\CacheHelperBundle\Service\SeoHelperIntegration;
  5. use Elements\Bundle\CacheHelperBundle\Service\SeoHelper\Handler\JsConfigHandler;
  6. use Elements\Bundle\CacheHelperBundle\Service\SeoHelper\SeoHandlerFactory;
  7. use Elements\Bundle\JsConfigBundle\Templating\Helper\JsConfig;
  8. use Elements\Bundle\SeoHelperBundle\Templating\Helper\Canonical;
  9. use Elements\Bundle\SeoHelperBundle\Templating\Helper\HeadMeta;
  10. use Elements\Bundle\SeoHelperBundle\Templating\Helper\HeadTitle;
  11. use Elements\Bundle\SeoHelperBundle\Templating\Helper\Robots;
  12. use Pimcore\Bundle\CoreBundle\EventListener\Traits\EnabledTrait;
  13. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  14. use Pimcore\Cache;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. class SeoHelperListener implements EventSubscriberInterface
  19. {
  20.     use EnabledTrait;
  21.     use PimcoreContextAwareTrait;
  22.     private $requestStack;
  23.     private $seoHandlerFactory;
  24.     private $cacheHelperService;
  25.     public function __construct(RequestStack $requestStackSeoHelperIntegration $cacheHelperServiceSeoHandlerFactory $seoHandlerFactory)
  26.     {
  27.         $this->requestStack $requestStack;
  28.         $this->seoHandlerFactory $seoHandlerFactory;
  29.         $this->cacheHelperService $cacheHelperService;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             JsConfig::EVENT_POST_ADD  => 'onJsConfigAdd',
  35.             HeadTitle::EVENT_HEAD_TITLE_SET_TITLE => 'onGenericDataAdd',
  36.             HeadMeta::EVENT_HEAD_META_SET_DESCRIPTION => 'onGenericDataAdd',
  37.             Robots::EVENT_SET_NO_INDEX => 'onGenericDataAdd',
  38.             Robots::EVENT_SET_NO_FOLLOW => 'onGenericDataAdd',
  39.             Canonical::EVENT_CANONICAL_SET_DOMAIN => 'onGenericDataAdd',
  40.         ];
  41.     }
  42.     public function onJsConfigAdd(GenericEvent $event) {
  43.         if (!$this->hasPassedPreconditions()) {
  44.             return;
  45.         }
  46.         $this->cacheHelperService->addDataFromSeoHelperEvent(JsConfigHandler::class, $event);
  47.     }
  48.     public function onGenericDataAdd(GenericEvent $event) {
  49.         if (!$this->hasPassedPreconditions()) {
  50.             return;
  51.         }
  52.         $this->cacheHelperService->addDataFromSeoHelperEvent(GenericSeoHelperHandler::class, $event);
  53.     }
  54.     private function hasPassedPreconditions() : bool {
  55.         if(!$this->isEnabled()) {
  56.             return false;
  57.         }
  58.         if (!Cache::isEnabled()) {
  59.             return false;
  60.         }
  61.         if (!$this->cacheHelperService->getCurrentCacheContext()) {
  62.             return false;
  63.         }
  64.         return true;
  65.     }
  66. }