custom/plugins/CompraStagingSW6/src/Storefront/Subscriber/FrontendSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\StagingSW6\Storefront\Subscriber;
  3. use Compra\FoundationSW6\Core\System\PluginConfigService;
  4. use Compra\StagingSW6\Core\System\Service\StagingService;
  5. use Compra\StagingSW6\Core\System\StagingConfiguration\StagingConfigurationEntity;
  6. use Exception;
  7. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class FrontendSubscriber implements EventSubscriberInterface
  11. {
  12.     protected ContainerInterface $container;
  13.     protected StagingService $stagingService;
  14.     protected PluginConfigService $pluginConfigService;
  15.     public function __construct(
  16.         ContainerInterface $container,
  17.         StagingService $stagingService,
  18.         PluginConfigService $pluginConfigService
  19.     )
  20.     {
  21.         $this->container $container;
  22.         $this->stagingService $stagingService;
  23.         $this->pluginConfigService $pluginConfigService;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
  29.         ];
  30.     }
  31.     /**
  32.      * @param HeaderPageletLoadedEvent $event
  33.      */
  34.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event): void
  35.     {
  36.         $shopDomains $event->getSalesChannelContext()->getSalesChannel()->getDomains();
  37.         if (!$shopDomains) {
  38.             return;
  39.         }
  40.         $domain $shopDomains->first();
  41.         if (!$domain) {
  42.             return;
  43.         }
  44.         try {
  45.             $showHint $this->checkShowHint($domain->getUrl());
  46.             $currentValue $this->pluginConfigService->getPluginConfig('showHint');
  47.             if ($currentValue !== $showHint['showHint']) {
  48.                 $this->pluginConfigService->setPluginConfig('showHint'$showHint['showHint']);
  49.             }
  50.             $currentValue $this->pluginConfigService->getPluginConfig('showHintName');
  51.             if ($currentValue !== $showHint['name']) {
  52.                 $this->pluginConfigService->setPluginConfig('showHintName'$showHint['name']);
  53.             }
  54.         } catch (Exception $e) {}
  55.     }
  56.     /**
  57.      * @param string $domain
  58.      * @return array
  59.      * @throws Exception
  60.      */
  61.     public function checkShowHint(string $domain): array
  62.     {
  63.         /** @var StagingConfigurationEntity[] $stagingConfigurations */
  64.         $stagingConfigurations $this->stagingService->getStagingConfigurationData();
  65.         foreach ($stagingConfigurations as $configuration) {
  66.             $domainNeedle $configuration->getDirectory() . '/public';
  67.             if ($this->stagingService->endsWith($domain,$domainNeedle)
  68.                 && $configuration->getShowHintInFrontend()) {
  69.                 return [
  70.                     'showHint' => true,
  71.                     'name' => $configuration->getDirectory()
  72.                 ];
  73.             }
  74.         }
  75.         return [
  76.             'showHint' => false,
  77.             'name' => ''
  78.         ];
  79.     }
  80. }