<?php declare(strict_types=1);
namespace zenit\PlatformFeaturesBar\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\PlatformFeaturesBar\Struct\SystemConfigData;
use zenit\PlatformFeaturesBar\Service\GetControllerInfo;
class StorefrontRenderSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $pluginName = 'zenitPlatformFeaturesBar';
/**
* @var string
*/
private $configPath = 'zenitPlatformFeaturesBar.config.';
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var GetControllerInfo
*/
private $getControllerInfo;
/**
* HeaderPageletLoadedSubscriber 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 InvalidDomainException
* @throws InvalidUuidException
* @throws InconsistentCriteriaIdsException
*/
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);
// remove deprecated controllers
if (is_array($allowedControllers)) {
$allowedControllers = array_diff($allowedControllers, array('Search', 'AccountProfile', 'Cms.page'));
}
if (!empty($allowedControllers) && !in_array($currentController, $allowedControllers)) {
return;
}
// get custom arrays from config
$icons = array
(
$this->systemConfigService->get($this->configPath . 'icon1', $shopId),
$this->systemConfigService->get($this->configPath . 'icon2', $shopId),
$this->systemConfigService->get($this->configPath . 'icon3', $shopId),
$this->systemConfigService->get($this->configPath . 'icon4', $shopId),
);
$texts = array
(
$this->systemConfigService->get($this->configPath . 'text1', $shopId),
$this->systemConfigService->get($this->configPath . 'text2', $shopId),
$this->systemConfigService->get($this->configPath . 'text3', $shopId),
$this->systemConfigService->get($this->configPath . 'text4', $shopId),
);
$textSlides = array
(
$this->systemConfigService->get($this->configPath . 'text1Slide', $shopId),
$this->systemConfigService->get($this->configPath . 'text2Slide', $shopId),
$this->systemConfigService->get($this->configPath . 'text3Slide', $shopId),
$this->systemConfigService->get($this->configPath . 'text4Slide', $shopId),
);
$textLinks = array
(
$this->systemConfigService->get($this->configPath . 'text1Link'),
$this->systemConfigService->get($this->configPath . 'text2Link'),
$this->systemConfigService->get($this->configPath . 'text3Link'),
$this->systemConfigService->get($this->configPath . 'text4Link'),
);
$textLinksTargets = array
(
$this->systemConfigService->get($this->configPath . 'text1LinkTarget'),
$this->systemConfigService->get($this->configPath . 'text2LinkTarget'),
$this->systemConfigService->get($this->configPath . 'text3LinkTarget'),
$this->systemConfigService->get($this->configPath . 'text4LinkTarget'),
);
$textStatemanager = array
(
$this->systemConfigService->get($this->configPath . 'text1Statemanager'),
$this->systemConfigService->get($this->configPath . 'text2Statemanager'),
$this->systemConfigService->get($this->configPath . 'text3Statemanager'),
$this->systemConfigService->get($this->configPath . 'text4Statemanager'),
);
// 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);
// set custom arrays to config
$configValues->setValue($config, 'icon', $icons);
$configValues->setValue($config, 'text', $texts);
$configValues->setValue($config, 'textSlide', $textSlides);
$configValues->setValue($config, 'textLink', $textLinks);
$configValues->setValue($config, 'textLinkTargets', $textLinksTargets);
$configValues->setValue($config, 'textStatemanager', $textStatemanager);
// add config
$event->getContext()->addExtension($this->pluginName, $configValues);
}
}