<?php
namespace Elements\Bundle\CacheHelperBundle\EventListener;
use Elements\Bundle\CacheHelperBundle\Service\SeoHelper\Handler\GenericSeoHelperHandler;
use Elements\Bundle\CacheHelperBundle\Service\SeoHelperIntegration;
use Elements\Bundle\CacheHelperBundle\Service\SeoHelper\Handler\JsConfigHandler;
use Elements\Bundle\CacheHelperBundle\Service\SeoHelper\SeoHandlerFactory;
use Elements\Bundle\JsConfigBundle\Templating\Helper\JsConfig;
use Elements\Bundle\SeoHelperBundle\Templating\Helper\Canonical;
use Elements\Bundle\SeoHelperBundle\Templating\Helper\HeadMeta;
use Elements\Bundle\SeoHelperBundle\Templating\Helper\HeadTitle;
use Elements\Bundle\SeoHelperBundle\Templating\Helper\Robots;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\EnabledTrait;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
use Pimcore\Cache;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\RequestStack;
class SeoHelperListener implements EventSubscriberInterface
{
use EnabledTrait;
use PimcoreContextAwareTrait;
private $requestStack;
private $seoHandlerFactory;
private $cacheHelperService;
public function __construct(RequestStack $requestStack, SeoHelperIntegration $cacheHelperService, SeoHandlerFactory $seoHandlerFactory)
{
$this->requestStack = $requestStack;
$this->seoHandlerFactory = $seoHandlerFactory;
$this->cacheHelperService = $cacheHelperService;
}
public static function getSubscribedEvents()
{
return [
JsConfig::EVENT_POST_ADD => 'onJsConfigAdd',
HeadTitle::EVENT_HEAD_TITLE_SET_TITLE => 'onGenericDataAdd',
HeadMeta::EVENT_HEAD_META_SET_DESCRIPTION => 'onGenericDataAdd',
Robots::EVENT_SET_NO_INDEX => 'onGenericDataAdd',
Robots::EVENT_SET_NO_FOLLOW => 'onGenericDataAdd',
Canonical::EVENT_CANONICAL_SET_DOMAIN => 'onGenericDataAdd',
];
}
public function onJsConfigAdd(GenericEvent $event) {
if (!$this->hasPassedPreconditions()) {
return;
}
$this->cacheHelperService->addDataFromSeoHelperEvent(JsConfigHandler::class, $event);
}
public function onGenericDataAdd(GenericEvent $event) {
if (!$this->hasPassedPreconditions()) {
return;
}
$this->cacheHelperService->addDataFromSeoHelperEvent(GenericSeoHelperHandler::class, $event);
}
private function hasPassedPreconditions() : bool {
if(!$this->isEnabled()) {
return false;
}
if (!Cache::isEnabled()) {
return false;
}
if (!$this->cacheHelperService->getCurrentCacheContext()) {
return false;
}
return true;
}
}