<?php declare(strict_types=1);
namespace zenit\PlatformShippingBar\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
use Shopware\Core\System\SystemConfig\Exception\InvalidDomainException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use zenit\PlatformShippingBar\Struct\SystemConfigData;
use zenit\PlatformShippingBar\Service\GetControllerInfo;
/**
* Class StorefrontRenderSubscriber
* @package Zenit\ShippingBar\Subscriber
*/
class StorefrontRenderSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $pluginName = 'zenitPlatformShippingBar';
/**
* @var string
*/
private $configPath = 'zenitPlatformShippingBar.config.';
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var GetControllerInfo
*/
private $getControllerInfo;
/**
* StorefrontRenderSubscriber constructor.
* @param SystemConfigService $systemConfigService
* @param GetControllerInfo $getControllerInfo
*/
public function __construct(SystemConfigService $systemConfigService, GetControllerInfo $getControllerInfo)
{
$this->systemConfigService = $systemConfigService;
$this->getControllerInfo = $getControllerInfo;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return[
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
/**
* @param StorefrontRenderEvent $event
* @throws InconsistentCriteriaIdsException
* @throws InvalidDomainException
* @throws InvalidUuidException
*/
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$shopId = $event->getSalesChannelContext()->getSalesChannel()->getId();
// is active check
if (!$this->systemConfigService->get($this->configPath . 'active', $shopId)) {
return;
}
// controller check
$currentController = $this->getControllerInfo->getCurrentController();
$allowedControllers = $this->systemConfigService->get($this->configPath . 'allowedControllers', $shopId);
if (!empty($allowedControllers) && !in_array($currentController, $allowedControllers)) {
return;
}
// get config
$systemConfig = $this->systemConfigService->getDomain($this->configPath, $shopId, true);
// replace domainstrings in keys
$config = [];
foreach($systemConfig as $key => $value) {
$config[str_replace($this->configPath, '',$key)] = $value;
}
// set config
$configValues = new SystemConfigData($config);
// add config
$event->getContext()->addExtension($this->pluginName, $configValues);
}
}