custom/plugins/papooCcm19Integration6/src/Subscriber/Frontend.php line 73

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * @copyright Papoo Software & Media GmbH
  4.  * @author Christoph Grenz <info@papoo.de>
  5.  * @date 2022-02-03
  6.  */
  7. namespace Papoo\Ccm19Integration\Subscriber;
  8. use Papoo\Ccm19Integration\Util\CcmInformationStruct;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Event\StorefrontRenderEvent;
  11. use Shopware\Storefront\Page\Page;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * Subscriber for frontend events
  15.  */
  16. class Frontend implements EventSubscriberInterface
  17. {
  18.     /** @var SystemConfigService */
  19.     private $config;
  20.     public const CCM19_INTEGRATION_EXTENSION_ID 'papooCcm19Integration';
  21.     /**
  22.      * Initialize subscriber and read configuration
  23.      *
  24.      * @param SystemConfigService $configReader
  25.      */
  26.     public function __construct(SystemConfigService $config)
  27.     {
  28.         $this->config $config;
  29.     }
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             StorefrontRenderEvent::class => 'injectCcmData',
  37.         ];
  38.     }
  39.     /**
  40.      * @param string|null Sales-Channel-ID (null for 'global')
  41.      * @return string|null
  42.      */
  43.     private function getIntegrationUrl(?string $salesChannelId): ?string
  44.     {
  45.         $code $this->config->get('papooCcm19Integration6.config.integrationCode'$salesChannelId);
  46.         if ($code) {
  47.             $match = [];
  48.             preg_match('~\bhttps?://[^"\'\s]{1,256}\.js\?(?>[^"\'\s]{1,128})~i'$code$match);
  49.             if ($match and $match[0]) {
  50.                 if (strpos($match[0], ';') === false) {
  51.                     return $match[0];
  52.                 } else {
  53.                     return html_entity_decode($match[0], ENT_HTML401|ENT_QUOTES'UTF-8');
  54.                 }
  55.             }
  56.         }
  57.         return null;
  58.     }
  59.     /**
  60.      * Hook: load integration url into template
  61.      *
  62.      * @param PageLoadedEvent $args
  63.      * @return void
  64.      */
  65.     public function injectCcmData(StorefrontRenderEvent $event): void
  66.     {
  67.         $parameters $event->getParameters();
  68.         if (isset($parameters['page'])) {
  69.             /** @var Page $page */
  70.             $page $parameters['page'];
  71.             $salesChannel $event->getSalesChannelContext()->getSalesChannelId();
  72.             $data = new CcmInformationStruct(['url'=>$this->getIntegrationUrl($salesChannel)]);
  73.             $page->addExtension(self::CCM19_INTEGRATION_EXTENSION_ID$data);
  74.         }
  75.     }
  76. }