vendor/connectholland/cookie-consent-bundle/Controller/CookieConsentController.php line 82

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the ConnectHolland CookieConsentBundle package.
  5.  * (c) Connect Holland.
  6.  */
  7. namespace ConnectHolland\CookieConsentBundle\Controller;
  8. use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker;
  9. use ConnectHolland\CookieConsentBundle\Form\CookieConsentType;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Twig\Environment;
  17. class CookieConsentController
  18. {
  19.     /**
  20.      * @var Environment
  21.      */
  22.     private $twigEnvironment;
  23.     /**
  24.      * @var FormFactoryInterface
  25.      */
  26.     private $formFactory;
  27.     /**
  28.      * @var CookieChecker
  29.      */
  30.     private $cookieChecker;
  31.     /**
  32.      * @var string
  33.      */
  34.     private $cookieConsentTheme;
  35.     /**
  36.      * @var string
  37.      */
  38.     private $cookieConsentPosition;
  39.     /**
  40.      * @var TranslatorInterface
  41.      */
  42.     private $translator;
  43.     /**
  44.      * @var bool
  45.      */
  46.     private $cookieConsentSimplified;
  47.     public function __construct(
  48.         Environment $twigEnvironment,
  49.         FormFactoryInterface $formFactory,
  50.         CookieChecker $cookieChecker,
  51.         string $cookieConsentTheme,
  52.         string $cookieConsentPosition,
  53.         TranslatorInterface $translator,
  54.         bool $cookieConsentSimplified false
  55.     ) {
  56.         $this->twigEnvironment         $twigEnvironment;
  57.         $this->formFactory             $formFactory;
  58.         $this->cookieChecker           $cookieChecker;
  59.         $this->cookieConsentTheme      $cookieConsentTheme;
  60.         $this->cookieConsentPosition   $cookieConsentPosition;
  61.         $this->translator              $translator;
  62.         $this->cookieConsentSimplified $cookieConsentSimplified;
  63.     }
  64.     /**
  65.      * Show cookie consent.
  66.      *
  67.      * @Route("/cookie_consent", name="ch_cookie_consent.show")
  68.      */
  69.     public function show(Request $request): Response
  70.     {
  71.         $this->setLocale($request);
  72.         $response = new Response(
  73.             $this->twigEnvironment->render('@CHCookieConsent/cookie_consent.html.twig', [
  74.                 'form'       => $this->createCookieConsentForm()->createView(),
  75.                 'theme'      => $this->cookieConsentTheme,
  76.                 'position'   => $this->cookieConsentPosition,
  77.                 'simplified' => $this->cookieConsentSimplified,
  78.             ])
  79.         );
  80.         // Cache in ESI should not be shared
  81.         $response->setPrivate();
  82.         $response->setMaxAge(0);
  83.         return $response;
  84.     }
  85.     /**
  86.      * Show cookie consent.
  87.      *
  88.      * @Route("/cookie_consent_alt", name="ch_cookie_consent.show_if_cookie_consent_not_set")
  89.      */
  90.     public function showIfCookieConsentNotSet(Request $request): Response
  91.     {
  92.         if ($this->cookieChecker->isCookieConsentSavedByUser() === false) {
  93.             return $this->show($request);
  94.         }
  95.         return new Response();
  96.     }
  97.     /**
  98.      * Create cookie consent form.
  99.      */
  100.     protected function createCookieConsentForm(): FormInterface
  101.     {
  102.         return $this->formFactory->create(CookieConsentType::class);
  103.     }
  104.     /**
  105.      * Set locale if available as GET parameter.
  106.      */
  107.     protected function setLocale(Request $request)
  108.     {
  109.         $locale $request->get('locale');
  110.         if (empty($locale) === false) {
  111.             $this->translator->setLocale($locale);
  112.             $request->setLocale($locale);
  113.         }
  114.     }
  115. }