custom/plugins/BpaPlatformVideoIframeCookieHandler/src/Subscriber/FilterCookiesSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Bpa\VideoIframeCookieHandler\Subscriber;
  3. use Bpa\VideoIframeCookieHandler\BpaPlatformVideoIframeCookieHandler;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Event\StorefrontRenderEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class FilterCookiesSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var SystemConfigService
  11.      */
  12.     private SystemConfigService $systemConfigService;
  13.     /**
  14.      * @param SystemConfigService $systemConfigService
  15.      */
  16.     public function __construct(SystemConfigService $systemConfigService)
  17.     {
  18.         $this->systemConfigService $systemConfigService;
  19.     }
  20.     /**
  21.      * @return string[]
  22.      */
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             StorefrontRenderEvent::class => 'onStorefrontRender',
  27.         ];
  28.     }
  29.     /**
  30.      * @param StorefrontRenderEvent $event
  31.      * @return void
  32.      */
  33.     public function onStorefrontRender(StorefrontRenderEvent $event)
  34.     {
  35.         $parameters $event->getParameters();
  36.         $salesChannelContext $event->getSalesChannelContext();
  37.         if (array_key_exists('cookieGroups'$parameters)) {
  38.             $cookieGroups $this->filterIframeCookie($salesChannelContext->getSalesChannelId(), $parameters['cookieGroups']);
  39.             $event->setParameter('cookieGroups'$cookieGroups);
  40.         }
  41.     }
  42.     /**
  43.      * Removes disabled cookies from group
  44.      *
  45.      * @param string $salesChannelId
  46.      * @param array $cookieGroups
  47.      * @return array
  48.      */
  49.     private function filterIframeCookie(string $salesChannelId, array $cookieGroups): array
  50.     {
  51.         $config $this->systemConfigService->get('BpaPlatformVideoIframeCookieHandler.config'$salesChannelId);
  52.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  53.             if ($cookieGroup['snippet_name'] !== 'cookie.groupStatistical') {
  54.                 continue;
  55.             }
  56.             foreach (BpaPlatformVideoIframeCookieHandler::AVAILABLE_IFRAMES as $iframe) {
  57.                 foreach ($cookieGroup['entries'] as $entryIdx => $entry) {
  58.                     if ($entry['cookie'] !== $iframe['cookieName']) {
  59.                         continue;
  60.                     }
  61.                     if (!$config[$iframe['configName']]) {
  62.                         unset($cookieGroups[$groupIndex]['entries'][$entryIdx]);
  63.                     }
  64.                 }
  65.             }
  66.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  67.                 unset($cookieGroups[$groupIndex]);
  68.             }
  69.         }
  70.         return $cookieGroups;
  71.     }
  72. }