vendor/shopware/storefront/Controller/CookieController.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  5. use Shopware\Core\Framework\Routing\Annotation\Since;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Framework\Captcha\GoogleReCaptchaV2;
  9. use Shopware\Storefront\Framework\Captcha\GoogleReCaptchaV3;
  10. use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route(defaults={"_routeScope"={"storefront"}})
  15.  *
  16.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  17.  */
  18. #[Package('storefront')]
  19. class CookieController extends StorefrontController
  20. {
  21.     /**
  22.      * @var CookieProviderInterface
  23.      */
  24.     private $cookieProvider;
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $systemConfigService;
  29.     /**
  30.      * @internal
  31.      */
  32.     public function __construct(CookieProviderInterface $cookieProviderSystemConfigService $systemConfigService)
  33.     {
  34.         $this->cookieProvider $cookieProvider;
  35.         $this->systemConfigService $systemConfigService;
  36.     }
  37.     /**
  38.      * @Since("6.1.0.0")
  39.      * @Route("/cookie/offcanvas", name="frontend.cookie.offcanvas", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  40.      */
  41.     public function offcanvas(SalesChannelContext $context): Response
  42.     {
  43.         $cookieGroups $this->cookieProvider->getCookieGroups();
  44.         $cookieGroups $this->filterGoogleAnalyticsCookie($context$cookieGroups);
  45.         $cookieGroups $this->filterComfortFeaturesCookie($context->getSalesChannelId(), $cookieGroups);
  46.         $cookieGroups $this->filterGoogleReCaptchaCookie($context->getSalesChannelId(), $cookieGroups);
  47.         $response $this->renderStorefront('@Storefront/storefront/layout/cookie/cookie-configuration.html.twig', ['cookieGroups' => $cookieGroups]);
  48.         $response->headers->set('x-robots-tag''noindex,follow');
  49.         return $response;
  50.     }
  51.     /**
  52.      * @Since("6.1.0.0")
  53.      * @Route("/cookie/permission", name="frontend.cookie.permission", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  54.      */
  55.     public function permission(SalesChannelContext $context): Response
  56.     {
  57.         $cookieGroups $this->cookieProvider->getCookieGroups();
  58.         $cookieGroups $this->filterGoogleAnalyticsCookie($context$cookieGroups);
  59.         $cookieGroups $this->filterComfortFeaturesCookie($context->getSalesChannelId(), $cookieGroups);
  60.         $cookieGroups $this->filterGoogleReCaptchaCookie($context->getSalesChannelId(), $cookieGroups);
  61.         $response $this->renderStorefront('@Storefront/storefront/layout/cookie/cookie-permission.html.twig', ['cookieGroups' => $cookieGroups]);
  62.         $response->headers->set('x-robots-tag''noindex,follow');
  63.         return $response;
  64.     }
  65.     private function filterGoogleAnalyticsCookie(SalesChannelContext $context, array $cookieGroups): array
  66.     {
  67.         if ($context->getSalesChannel()->getAnalytics() && $context->getSalesChannel()->getAnalytics()->isActive()) {
  68.             return $cookieGroups;
  69.         }
  70.         $filteredGroups = [];
  71.         foreach ($cookieGroups as $cookieGroup) {
  72.             if ($cookieGroup['snippet_name'] === 'cookie.groupStatistical') {
  73.                 $cookieGroup['entries'] = array_filter($cookieGroup['entries'], function ($item) {
  74.                     return $item['snippet_name'] !== 'cookie.groupStatisticalGoogleAnalytics';
  75.                 });
  76.                 // Only add statistics cookie group if it has entries
  77.                 if (\count($cookieGroup['entries']) > 0) {
  78.                     $filteredGroups[] = $cookieGroup;
  79.                 }
  80.                 continue;
  81.             }
  82.             $filteredGroups[] = $cookieGroup;
  83.         }
  84.         return $filteredGroups;
  85.     }
  86.     private function filterComfortFeaturesCookie(string $salesChannelId, array $cookieGroups): array
  87.     {
  88.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  89.             if ($cookieGroup['snippet_name'] !== 'cookie.groupComfortFeatures') {
  90.                 continue;
  91.             }
  92.             foreach ($cookieGroup['entries'] as $entryIndex => $entry) {
  93.                 if ($entry['snippet_name'] !== 'cookie.groupComfortFeaturesWishlist') {
  94.                     continue;
  95.                 }
  96.                 if (!$this->systemConfigService->get('core.cart.wishlistEnabled'$salesChannelId)) {
  97.                     unset($cookieGroups[$groupIndex]['entries'][$entryIndex]);
  98.                 }
  99.             }
  100.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  101.                 unset($cookieGroups[$groupIndex]);
  102.             }
  103.         }
  104.         return $cookieGroups;
  105.     }
  106.     private function filterGoogleReCaptchaCookie(string $salesChannelId, array $cookieGroups): array
  107.     {
  108.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  109.             if ($cookieGroup['snippet_name'] !== 'cookie.groupRequired') {
  110.                 continue;
  111.             }
  112.             foreach ($cookieGroup['entries'] as $entryIndex => $entry) {
  113.                 if ($entry['snippet_name'] !== 'cookie.groupRequiredCaptcha') {
  114.                     continue;
  115.                 }
  116.                 $activeGreCaptchaV2 $this->systemConfigService->get('core.basicInformation.activeCaptchasV2.' GoogleReCaptchaV2::CAPTCHA_NAME '.isActive'$salesChannelId) ?? false;
  117.                 $activeGreCaptchaV3 $this->systemConfigService->get('core.basicInformation.activeCaptchasV2.' GoogleReCaptchaV3::CAPTCHA_NAME '.isActive'$salesChannelId) ?? false;
  118.                 if (!$activeGreCaptchaV2 && !$activeGreCaptchaV3) {
  119.                     unset($cookieGroups[$groupIndex]['entries'][$entryIndex]);
  120.                 }
  121.             }
  122.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  123.                 unset($cookieGroups[$groupIndex]);
  124.             }
  125.         }
  126.         return $cookieGroups;
  127.     }
  128. }