custom/plugins/zenitPlatformDataBadges/src/Subscriber/Subscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformDataBadges\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 Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  11. use zenit\PlatformDataBadges\Struct\SystemConfigData;
  12. /**
  13.  * Class ProductPageLoadedSubscriber
  14.  * @package zenit\PlatformDataBadges\Storefront\Page\Product\Subscriber
  15.  */
  16. class Subscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var string
  20.      */
  21.     private $pluginName 'zenitPlatformDataBadges';
  22.     /**
  23.      * @var string
  24.      */
  25.     private $configPath 'zenitPlatformDataBadges.config.';
  26.     /**
  27.      * @var SystemConfigService
  28.      */
  29.     private $systemConfigService;
  30.     /**
  31.      * ProductPageLoadedSubscriber constructor.
  32.      * @param SystemConfigService $systemConfigService
  33.      */
  34.     public function __construct(SystemConfigService $systemConfigService)
  35.     {
  36.         $this->systemConfigService $systemConfigService;
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return[
  44.             'sales_channel.product.loaded' => 'onProductLoaded'
  45.         ];
  46.     }
  47.     /**
  48.      * @param ProductPageLoadedEvent|NavigationPageLoadedEvent|SearchPageLoadedEvent $event
  49.      * @throws InconsistentCriteriaIdsException
  50.      * @throws InvalidDomainException
  51.      * @throws InvalidUuidException
  52.      */
  53.     public function onProductLoaded($event): void
  54.     {
  55.         $shopId $event->getSalesChannelContext()->getSalesChannel()->getId();
  56.         // is active check
  57.         if (!$this->systemConfigService->get($this->configPath 'active'$shopId)) {
  58.             return;
  59.         }
  60.         // get config
  61.         $systemConfig $this->systemConfigService->getDomain($this->configPath$shopIdtrue);
  62.         // replace domainstrings in keys
  63.         $config = [];
  64.         foreach($systemConfig as $key => $value) {
  65.             $config[str_replace($this->configPath'',$key)] = $value;
  66.         }
  67.         // set config
  68.         $configValues = new SystemConfigData($config);
  69.         // add config & custom fields
  70.         $event->getContext()->addExtensions([
  71.             $this->pluginName => $configValues
  72.             ]
  73.         );
  74.     }
  75. }