custom/plugins/DIScoGA4/src/Storefront/Controller/CookieControllerDecorator.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DISco\GA4\Storefront\Controller;
  3. use ReflectionMethod;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Controller\CookieController;
  9. use Shopware\Storefront\Framework\Captcha\GoogleReCaptchaV2;
  10. use Shopware\Storefront\Framework\Captcha\GoogleReCaptchaV3;
  11. use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Twig\Environment;
  16. /**
  17.  * Support v6.4.1
  18.  * @Shopware\Core\Framework\Routing\Annotation\RouteScope(scopes={"storefront"})
  19.  */
  20. #[Route(defaults: ['_routeScope' => ['storefront']])]
  21. #[Package('storefront')]
  22. class CookieControllerDecorator extends CookieController
  23. {
  24.     private CookieProviderInterface $cookieProvider;
  25.     private CookieController $originalService;
  26.     private SystemConfigService $systemConfigService;
  27.     private TranslatorInterface $translator;
  28.     private EntityRepository $salesChannelAnalyticsRepository;
  29.     public function __construct(
  30.         CookieController $service,
  31.         CookieProviderInterface $cookieProvider,
  32.         SystemConfigService $systemConfigService,
  33.         TranslatorInterface $translator,
  34.         EntityRepository $salesChannelAnalyticsRepository
  35.     ) {
  36.         $numberOfParameters = (new ReflectionMethod(parent::class, '__construct'))->getNumberOfRequiredParameters();
  37.         if (=== $numberOfParameters) { // For SW 6.6.*
  38.             parent::__construct($cookieProvider$systemConfigService$salesChannelAnalyticsRepository);
  39.         } else {
  40.             parent::__construct($cookieProvider$systemConfigService);
  41.         }
  42.         $this->originalService $service;
  43.         $this->cookieProvider $cookieProvider;
  44.         $this->systemConfigService $systemConfigService;
  45.         $this->translator $translator;
  46.     }
  47.     public function setTwig(Environment $twig): void
  48.     {
  49.         if (is_callable('parent::setTwig')) {
  50.             parent::setTwig($twig);
  51.         }
  52.     }
  53.     public function getDecorated(): CookieController
  54.     {
  55.         return $this->originalService;
  56.     }
  57.     /**
  58.      * @Route("/cookie/offcanvas", name="frontend.cookie.offcanvas", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  59.      */
  60.     #[Route(path'/cookie/offcanvas'name'frontend.cookie.offcanvas'options: ['seo' => false], defaults: ['XmlHttpRequest' => true], methods: ['GET'])]
  61.     public function offcanvas(SalesChannelContext $context): Response
  62.     {
  63.         $cookieGroups $this->cookieProvider->getCookieGroups();
  64.         $cookieGroups $this->filterGoogleAnalyticsCookie($context$cookieGroups);
  65.         $cookieGroups $this->filterComfortFeaturesCookie($context->getSalesChannelId(), $cookieGroups);
  66.         $cookieGroups $this->filterGoogleReCaptchaCookie($context->getSalesChannelId(), $cookieGroups);
  67.         $cookieGroups $this->filterGA4GoogleTagCookie($context->getSalesChannelId(), $cookieGroups);
  68.         $cookieGroups $this->filterGA4GoogleAdsCookie($context->getSalesChannelId(), $cookieGroups);
  69.         $cookieGroups $this->filterGA4GoogleAnalyticsCookie($context->getSalesChannelId(), $cookieGroups);
  70.         $response $this->renderStorefront('@Storefront/storefront/layout/cookie/cookie-configuration.html.twig', ['cookieGroups' => $cookieGroups]);
  71.         $response->headers->set('x-robots-tag''noindex,follow');
  72.         return $response;
  73.     }
  74.     /**
  75.      * @Route("/cookie/permission", name="frontend.cookie.permission", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  76.      */
  77.     #[Route(path'/cookie/permission'name'frontend.cookie.permission'options: ['seo' => false], defaults: ['XmlHttpRequest' => true], methods: ['GET'])]
  78.     public function permission(SalesChannelContext $context): Response
  79.     {
  80.         $cookieGroups $this->cookieProvider->getCookieGroups();
  81.         $cookieGroups $this->filterGoogleAnalyticsCookie($context$cookieGroups);
  82.         $cookieGroups $this->filterComfortFeaturesCookie($context->getSalesChannelId(), $cookieGroups);
  83.         $cookieGroups $this->filterGoogleReCaptchaCookie($context->getSalesChannelId(), $cookieGroups);
  84.         $cookieGroups $this->filterGA4GoogleTagCookie($context->getSalesChannelId(), $cookieGroups);
  85.         $cookieGroups $this->filterGA4GoogleAdsCookie($context->getSalesChannelId(), $cookieGroups);
  86.         $cookieGroups $this->filterGA4GoogleAnalyticsCookie($context->getSalesChannelId(), $cookieGroups);
  87.         $response $this->renderStorefront('@Storefront/storefront/layout/cookie/cookie-permission.html.twig', ['cookieGroups' => $cookieGroups]);
  88.         $response->headers->set('x-robots-tag''noindex,follow');
  89.         return $response;
  90.     }
  91.     private function filterGA4GoogleTagCookie(string $salesChannelId, array $cookieGroups): array
  92.     {
  93.         if ($this->systemConfigService->get('DIScoGA4.config.googleTagEnabled',$salesChannelId)) {
  94.             return $cookieGroups;
  95.         }
  96.         $filteredGroups = [];
  97.         foreach ($cookieGroups as $cookieGroup) {
  98.             if ($this->translator->trans($cookieGroup['snippet_name']) == $this->translator->trans('cookie.googletag_name')) {
  99.                 $cookieGroup['entries'] = array_filter($cookieGroup['entries'], function ($item) {
  100.                     return $item['snippet_name'] !== 'cookie.cookie-gtag';
  101.                 });
  102.                 // Only add cookie group if it has entries
  103.                 if (\count($cookieGroup['entries']) > 0) {
  104.                     $filteredGroups[] = $cookieGroup;
  105.                 }
  106.                 continue;
  107.             }
  108.             $filteredGroups[] = $cookieGroup;
  109.         }
  110.         return $filteredGroups;
  111.     }
  112.     private function filterGA4GoogleAdsCookie(string $salesChannelId, array $cookieGroups): array
  113.     {
  114.         if ($this->systemConfigService->get('DIScoGA4.config.googleAdsEnabled',$salesChannelId)) {
  115.             return $cookieGroups;
  116.         }
  117.         $filteredGroups = [];
  118.         foreach ($cookieGroups as $cookieGroup) {
  119.             if ($this->translator->trans($cookieGroup['snippet_name']) == $this->translator->trans('cookie.marketing_name')) {
  120.                 $cookieGroup['entries'] = array_filter($cookieGroup['entries'], function ($item) {
  121.                     return $item['snippet_name'] !== 'cookie.cookie-gads';
  122.                 });
  123.                 // Only add cookie group if it has entries
  124.                 if (\count($cookieGroup['entries']) > 0) {
  125.                     $filteredGroups[] = $cookieGroup;
  126.                 }
  127.                 continue;
  128.             }
  129.             $filteredGroups[] = $cookieGroup;
  130.         }
  131.         return $filteredGroups;
  132.     }
  133.     private function filterGA4GoogleAnalyticsCookie(string $salesChannelId, array $cookieGroups): array
  134.     {
  135.         if ($this->systemConfigService->get('DIScoGA4.config.googleAnalyticsEnabled',$salesChannelId)) {
  136.             return $cookieGroups;
  137.         }
  138.         $filteredGroups = [];
  139.         foreach ($cookieGroups as $cookieGroup) {
  140.             if ($this->translator->trans($cookieGroup['snippet_name']) == $this->translator->trans('cookie.statistics_name')) {
  141.                 $cookieGroup['entries'] = array_filter($cookieGroup['entries'], function ($item) {
  142.                     return $item['snippet_name'] !== 'cookie.cookie-ga';
  143.                 });
  144.                 // Only add cookie group if it has entries
  145.                 if (\count($cookieGroup['entries']) > 0) {
  146.                     $filteredGroups[] = $cookieGroup;
  147.                 }
  148.                 continue;
  149.             }
  150.             $filteredGroups[] = $cookieGroup;
  151.         }
  152.         return $filteredGroups;
  153.     }
  154.     private function filterGoogleAnalyticsCookie(SalesChannelContext $context, array $cookieGroups): array
  155.     {
  156.         if ($context->getSalesChannel()->getAnalytics() && $context->getSalesChannel()->getAnalytics()->isActive()) {
  157.             return $cookieGroups;
  158.         }
  159.         $filteredGroups = [];
  160.         foreach ($cookieGroups as $cookieGroup) {
  161.             if ($cookieGroup['snippet_name'] === 'cookie.groupStatistical') {
  162.                 $cookieGroup['entries'] = array_filter($cookieGroup['entries'], function ($item) {
  163.                     return $item['snippet_name'] !== 'cookie.groupStatisticalGoogleAnalytics';
  164.                 });
  165.                 // Only add statistics cookie group if it has entries
  166.                 if (\count($cookieGroup['entries']) > 0) {
  167.                     $filteredGroups[] = $cookieGroup;
  168.                 }
  169.                 continue;
  170.             }
  171.             $filteredGroups[] = $cookieGroup;
  172.         }
  173.         return $filteredGroups;
  174.     }
  175.     private function filterComfortFeaturesCookie(string $salesChannelId, array $cookieGroups): array
  176.     {
  177.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  178.             if ($cookieGroup['snippet_name'] !== 'cookie.groupComfortFeatures') {
  179.                 continue;
  180.             }
  181.             foreach ($cookieGroup['entries'] as $entryIndex => $entry) {
  182.                 if ($entry['snippet_name'] !== 'cookie.groupComfortFeaturesWishlist') {
  183.                     continue;
  184.                 }
  185.                 if (!$this->systemConfigService->get('core.cart.wishlistEnabled'$salesChannelId)) {
  186.                     unset($cookieGroups[$groupIndex]['entries'][$entryIndex]);
  187.                 }
  188.             }
  189.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  190.                 unset($cookieGroups[$groupIndex]);
  191.             }
  192.         }
  193.         return $cookieGroups;
  194.     }
  195.     private function filterGoogleReCaptchaCookie(string $salesChannelId, array $cookieGroups): array
  196.     {
  197.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  198.             if ($cookieGroup['snippet_name'] !== 'cookie.groupRequired') {
  199.                 continue;
  200.             }
  201.             foreach ($cookieGroup['entries'] as $entryIndex => $entry) {
  202.                 if ($entry['snippet_name'] !== 'cookie.groupRequiredCaptcha') {
  203.                     continue;
  204.                 }
  205.                 $activeGreCaptchaV2 $this->systemConfigService->get('core.basicInformation.activeCaptchasV2.' GoogleReCaptchaV2::CAPTCHA_NAME '.isActive'$salesChannelId) ?? false;
  206.                 $activeGreCaptchaV3 $this->systemConfigService->get('core.basicInformation.activeCaptchasV2.' GoogleReCaptchaV3::CAPTCHA_NAME '.isActive'$salesChannelId) ?? false;
  207.                 if (!$activeGreCaptchaV2 && !$activeGreCaptchaV3) {
  208.                     unset($cookieGroups[$groupIndex]['entries'][$entryIndex]);
  209.                 }
  210.             }
  211.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  212.                 unset($cookieGroups[$groupIndex]);
  213.             }
  214.         }
  215.         return $cookieGroups;
  216.     }
  217. }