custom/plugins/zenitPlatformSalesCountdownBar/src/Subscriber/StorefrontRenderSubscriber.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformSalesCountdownBar\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  4. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  5. use Shopware\Core\System\SystemConfig\Exception\InvalidDomainException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Event\StorefrontRenderEvent;
  9. use zenit\PlatformSalesCountdownBar\Struct\SystemConfigData;
  10. use zenit\PlatformSalesCountdownBar\Service\GetControllerInfo;
  11. /**
  12.  * Class StorefrontRenderSubscriber
  13.  * @package zenit\PlatformSalesCountdownBar\Subscriber
  14.  */
  15. class StorefrontRenderSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var string
  19.      */
  20.     private $pluginName 'zenitPlatformSalesCountdownBar';
  21.     /**
  22.      * @var string
  23.      */
  24.     private $configPath 'zenitPlatformSalesCountdownBar.config.';
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $systemConfigService;
  29.     /**
  30.      * @var GetControllerInfo
  31.      */
  32.     private $getControllerInfo;
  33.     /**
  34.      * @param SystemConfigService $systemConfigService
  35.      * @param GetControllerInfo $getControllerInfo
  36.      */
  37.     public function __construct(SystemConfigService $systemConfigServiceGetControllerInfo $getControllerInfo)
  38.     {
  39.         $this->systemConfigService $systemConfigService;
  40.         $this->getControllerInfo $getControllerInfo;
  41.     }
  42.     /**
  43.      * @return array
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             StorefrontRenderEvent::class => 'onStorefrontRender'
  49.         ];
  50.     }
  51.     /**
  52.      * @param StorefrontRenderEvent $event
  53.      * @throws InvalidDomainException
  54.      * @throws InvalidUuidException
  55.      * @throws InconsistentCriteriaIdsException
  56.      */
  57.     public function onStorefrontRender(StorefrontRenderEvent $event)
  58.     {
  59.         $shopId $event->getSalesChannelContext()->getSalesChannel()->getId();
  60.         // is active check
  61.         if (!$this->systemConfigService->get($this->configPath 'active'$shopId)) {
  62.             return;
  63.         }
  64.         // controller check
  65.         $currentController $this->getControllerInfo->getCurrentController();
  66.         $allowedControllers $this->systemConfigService->get($this->configPath 'allowedControllers'$shopId);
  67.         // remove deprecated controllers
  68.         if (is_array($allowedControllers)) {
  69.             $allowedControllers array_diff($allowedControllers, array('Search''AccountProfile''Cms.page'));
  70.         }
  71.         if (!empty($allowedControllers) && !in_array($currentController$allowedControllers)) {
  72.             return;
  73.         }
  74.         // get config
  75.         $systemConfig $this->systemConfigService->getDomain($this->configPath$shopIdtrue);
  76.         // replace domainstrings in keys
  77.         $config = [];
  78.         foreach($systemConfig as $key => $value) {
  79.             $config[str_replace($this->configPath'',$key)] = $value;
  80.         }
  81.         // add banner id for StorageKey based on plugin-configuration
  82.         $config['configId'] = self::generateConfigId($config);
  83.         // set config
  84.         $configValues = new SystemConfigData($config);
  85.         // add config
  86.         $event->getContext()->addExtension($this->pluginName$configValues);
  87.     }
  88.     private function generateConfigId($config){
  89.         $id null;
  90.         foreach($config as $key => $value) {
  91.             if(is_array($value)) {
  92.                 $value implode($value);
  93.             }
  94.             if(is_bool($value)) {
  95.                 $value $value 'true''false';
  96.             }
  97.             $id .= strlen(strval($value));
  98.         }
  99.         return $id;
  100.     }
  101. }