<?php declare(strict_types=1);
namespace IronMatomo\Subscriber;
use IronMatomo\Matomo\MatomoDataService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class MatomoSettingsSubscriber
*
* @package IronMatomo\Subscriber
*/
class MatomoSettingsSubscriber implements EventSubscriberInterface
{
const MATOMO_DATA_EXTENSION_ID = 'ironMatomoData';
/**
* @var MatomoDataService
*/
private $matomoDataService;
/**
* MatomoSettingsSubscriber constructor.
*
* @param MatomoDataService $matomoDataService
*/
public function __construct(
MatomoDataService $matomoDataService
)
{
$this->matomoDataService = $matomoDataService;
}
/*
* @inherit
*/
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender',
];
}
/**
* @param StorefrontRenderEvent $event
* @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
*/
public function onStorefrontRender(StorefrontRenderEvent $event)
{
if ($event->getRequest()->isXmlHttpRequest()) {
return;
}
$parameters = $event->getParameters();
if (isset($parameters['page'])) {
$matomoData = $this->matomoDataService->getMatomoData($event->getSalesChannelContext());
$page = $parameters['page'];
$page->addExtensions([self::MATOMO_DATA_EXTENSION_ID => $matomoData]);
}
}
}