<?php declare(strict_types=1);
namespace zenit\PlatformSalesCountdownBar\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\PlatformSalesCountdownBar\Struct\SystemConfigData;
use zenit\PlatformSalesCountdownBar\Service\GetControllerInfo;
/**
* Class StorefrontRenderSubscriber
* @package zenit\PlatformSalesCountdownBar\Subscriber
*/
class StorefrontRenderSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $pluginName = 'zenitPlatformSalesCountdownBar';
/**
* @var string
*/
private $configPath = 'zenitPlatformSalesCountdownBar.config.';
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var GetControllerInfo
*/
private $getControllerInfo;
/**
* @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 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;
}
// add banner id for StorageKey based on plugin-configuration
$config['configId'] = self::generateConfigId($config);
// set config
$configValues = new SystemConfigData($config);
// add config
$event->getContext()->addExtension($this->pluginName, $configValues);
}
private function generateConfigId($config){
$id = null;
foreach($config as $key => $value) {
if(is_array($value)) {
$value = implode($value);
}
if(is_bool($value)) {
$value = $value ? 'true': 'false';
}
$id .= strlen(strval($value));
}
return $id;
}
}