custom/plugins/zenitPlatformFeaturesBar/src/Subscriber/StorefrontRenderSubscriber.php line 65

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformFeaturesBar\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\PlatformFeaturesBar\Struct\SystemConfigData;
  10. use zenit\PlatformFeaturesBar\Service\GetControllerInfo;
  11. class StorefrontRenderSubscriber  implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var string
  15.      */
  16.     private $pluginName 'zenitPlatformFeaturesBar';
  17.     /**
  18.      * @var string
  19.      */
  20.     private $configPath 'zenitPlatformFeaturesBar.config.';
  21.     /**
  22.      * @var SystemConfigService
  23.      */
  24.     private $systemConfigService;
  25.     /**
  26.      * @var GetControllerInfo
  27.      */
  28.     private $getControllerInfo;
  29.     /**
  30.      * HeaderPageletLoadedSubscriber constructor.
  31.      * @param SystemConfigService $systemConfigService
  32.      * @param GetControllerInfo $getControllerInfo
  33.      */
  34.     public function __construct(SystemConfigService $systemConfigServiceGetControllerInfo $getControllerInfo)
  35.     {
  36.         $this->systemConfigService $systemConfigService;
  37.         $this->getControllerInfo $getControllerInfo;
  38.     }
  39.     /**
  40.      * @return array
  41.      */
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return[
  45.             StorefrontRenderEvent::class => 'onStorefrontRender'
  46.         ];
  47.     }
  48.     /**
  49.      * @param StorefrontRenderEvent $event
  50.      * @throws InvalidDomainException
  51.      * @throws InvalidUuidException
  52.      * @throws InconsistentCriteriaIdsException
  53.      */
  54.     public function onStorefrontRender(StorefrontRenderEvent $event)
  55.     {
  56.         $shopId $event->getSalesChannelContext()->getSalesChannel()->getId();
  57.         // is active check
  58.         if (!$this->systemConfigService->get($this->configPath 'active'$shopId)) {
  59.             return;
  60.         }
  61.         // controller check
  62.         $currentController $this->getControllerInfo->getCurrentController();
  63.         $allowedControllers $this->systemConfigService->get($this->configPath 'allowedControllers'$shopId);
  64.         // remove deprecated controllers
  65.         if (is_array($allowedControllers)) {
  66.             $allowedControllers array_diff($allowedControllers, array('Search''AccountProfile''Cms.page'));
  67.         }
  68.         if (!empty($allowedControllers) && !in_array($currentController$allowedControllers)) {
  69.             return;
  70.         }
  71.         // get custom arrays from config
  72.         $icons = array
  73.         (
  74.             $this->systemConfigService->get($this->configPath 'icon1'$shopId),
  75.             $this->systemConfigService->get($this->configPath 'icon2'$shopId),
  76.             $this->systemConfigService->get($this->configPath 'icon3'$shopId),
  77.             $this->systemConfigService->get($this->configPath 'icon4'$shopId),
  78.         );
  79.         $texts = array
  80.         (
  81.             $this->systemConfigService->get($this->configPath 'text1'$shopId),
  82.             $this->systemConfigService->get($this->configPath 'text2'$shopId),
  83.             $this->systemConfigService->get($this->configPath 'text3'$shopId),
  84.             $this->systemConfigService->get($this->configPath 'text4'$shopId),
  85.         );
  86.         $textSlides = array
  87.         (
  88.             $this->systemConfigService->get($this->configPath 'text1Slide'$shopId),
  89.             $this->systemConfigService->get($this->configPath 'text2Slide'$shopId),
  90.             $this->systemConfigService->get($this->configPath 'text3Slide'$shopId),
  91.             $this->systemConfigService->get($this->configPath 'text4Slide'$shopId),
  92.         );
  93.         $textLinks = array
  94.         (
  95.             $this->systemConfigService->get($this->configPath 'text1Link'),
  96.             $this->systemConfigService->get($this->configPath 'text2Link'),
  97.             $this->systemConfigService->get($this->configPath 'text3Link'),
  98.             $this->systemConfigService->get($this->configPath 'text4Link'),
  99.         );
  100.         $textLinksTargets = array
  101.         (
  102.             $this->systemConfigService->get($this->configPath 'text1LinkTarget'),
  103.             $this->systemConfigService->get($this->configPath 'text2LinkTarget'),
  104.             $this->systemConfigService->get($this->configPath 'text3LinkTarget'),
  105.             $this->systemConfigService->get($this->configPath 'text4LinkTarget'),
  106.         );
  107.         $textStatemanager = array
  108.         (
  109.             $this->systemConfigService->get($this->configPath 'text1Statemanager'),
  110.             $this->systemConfigService->get($this->configPath 'text2Statemanager'),
  111.             $this->systemConfigService->get($this->configPath 'text3Statemanager'),
  112.             $this->systemConfigService->get($this->configPath 'text4Statemanager'),
  113.         );
  114.         // get config
  115.         $systemConfig $this->systemConfigService->getDomain($this->configPath$shopIdtrue);
  116.         // replace domainstrings in keys
  117.         $config = [];
  118.         foreach($systemConfig as $key => $value) {
  119.             $config[str_replace($this->configPath'',$key)] = $value;
  120.         }
  121.         // set config
  122.         $configValues = new SystemConfigData($config);
  123.         // set custom arrays to config
  124.         $configValues->setValue($config'icon'$icons);
  125.         $configValues->setValue($config'text'$texts);
  126.         $configValues->setValue($config'textSlide'$textSlides);
  127.         $configValues->setValue($config'textLink'$textLinks);
  128.         $configValues->setValue($config'textLinkTargets'$textLinksTargets);
  129.         $configValues->setValue($config'textStatemanager'$textStatemanager);
  130.         // add config
  131.         $event->getContext()->addExtension($this->pluginName$configValues);
  132.     }
  133. }