custom/plugins/zenitPlatformShippingBar/src/Subscriber/StorefrontRenderSubscriber.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformShippingBar\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\PlatformShippingBar\Struct\SystemConfigData;
  10. use zenit\PlatformShippingBar\Service\GetControllerInfo;
  11. /**
  12.  * Class StorefrontRenderSubscriber
  13.  * @package Zenit\ShippingBar\Subscriber
  14.  */
  15. class StorefrontRenderSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var string
  19.      */
  20.     private $pluginName 'zenitPlatformShippingBar';
  21.     /**
  22.      * @var string
  23.      */
  24.     private $configPath 'zenitPlatformShippingBar.config.';
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $systemConfigService;
  29.     /**
  30.      * @var GetControllerInfo
  31.      */
  32.     private $getControllerInfo;
  33.     /**
  34.      * StorefrontRenderSubscriber constructor.
  35.      * @param SystemConfigService $systemConfigService
  36.      * @param GetControllerInfo $getControllerInfo
  37.      */
  38.     public function __construct(SystemConfigService $systemConfigServiceGetControllerInfo $getControllerInfo)
  39.     {
  40.         $this->systemConfigService $systemConfigService;
  41.         $this->getControllerInfo $getControllerInfo;
  42.     }
  43.     /**
  44.      * @return array
  45.      */
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return[
  49.             StorefrontRenderEvent::class => 'onStorefrontRender'
  50.         ];
  51.     }
  52.     /**
  53.      * @param StorefrontRenderEvent $event
  54.      * @throws InconsistentCriteriaIdsException
  55.      * @throws InvalidDomainException
  56.      * @throws InvalidUuidException
  57.      */
  58.     public function onStorefrontRender(StorefrontRenderEvent $event)
  59.     {
  60.         $shopId $event->getSalesChannelContext()->getSalesChannel()->getId();
  61.         // is active check
  62.         if (!$this->systemConfigService->get($this->configPath 'active'$shopId)) {
  63.             return;
  64.         }
  65.         // controller check
  66.         $currentController $this->getControllerInfo->getCurrentController();
  67.         $allowedControllers $this->systemConfigService->get($this->configPath 'allowedControllers'$shopId);
  68.         if (!empty($allowedControllers) && !in_array($currentController$allowedControllers)) {
  69.             return;
  70.         }
  71.         // get config
  72.         $systemConfig $this->systemConfigService->getDomain($this->configPath$shopIdtrue);
  73.         // replace domainstrings in keys
  74.         $config = [];
  75.         foreach($systemConfig as $key => $value) {
  76.             $config[str_replace($this->configPath'',$key)] = $value;
  77.         }
  78.         // set config
  79.         $configValues = new SystemConfigData($config);
  80.         // add config
  81.         $event->getContext()->addExtension($this->pluginName$configValues);
  82.     }
  83. }