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

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\Component\Routing\RouterInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use Twig\Environment;
  18. class CookieConsentController
  19. {
  20.     /**
  21.      * @var Environment
  22.      */
  23.     private $twigEnvironment;
  24.     /**
  25.      * @var FormFactoryInterface
  26.      */
  27.     private $formFactory;
  28.     /**
  29.      * @var CookieChecker
  30.      */
  31.     private $cookieChecker;
  32.     /**
  33.      * @var RouterInterface
  34.      */
  35.     private $router;
  36.     /**
  37.      * @var string
  38.      */
  39.     private $cookieConsentTheme;
  40.     /**
  41.      * @var string
  42.      */
  43.     private $cookieConsentPosition;
  44.     /**
  45.      * @var TranslatorInterface
  46.      */
  47.     private $translator;
  48.     /**
  49.      * @var bool
  50.      */
  51.     private $cookieConsentSimplified;
  52.     /**
  53.      * @var string|null
  54.      */
  55.     private $formAction;
  56.     public function __construct(
  57.         Environment $twigEnvironment,
  58.         FormFactoryInterface $formFactory,
  59.         CookieChecker $cookieChecker,
  60.         RouterInterface $router,
  61.         string $cookieConsentTheme,
  62.         string $cookieConsentPosition,
  63.         TranslatorInterface $translator,
  64.         bool $cookieConsentSimplified false,
  65.         string $formAction null
  66.     ) {
  67.         $this->twigEnvironment         $twigEnvironment;
  68.         $this->formFactory             $formFactory;
  69.         $this->cookieChecker           $cookieChecker;
  70.         $this->router                  $router;
  71.         $this->cookieConsentTheme      $cookieConsentTheme;
  72.         $this->cookieConsentPosition   $cookieConsentPosition;
  73.         $this->translator              $translator;
  74.         $this->cookieConsentSimplified $cookieConsentSimplified;
  75.         $this->formAction              $formAction;
  76.     }
  77.     /**
  78.      * Show cookie consent.
  79.      *
  80.      * @Route("/cookie_consent", name="ch_cookie_consent.show")
  81.      */
  82.     public function show(Request $request): Response
  83.     {
  84.         $this->setLocale($request);
  85.         $response = new Response(
  86.             $this->twigEnvironment->render('@CHCookieConsent/cookie_consent.html.twig', [
  87.                 'form'       => $this->createCookieConsentForm()->createView(),
  88.                 'theme'      => $this->cookieConsentTheme,
  89.                 'position'   => $this->cookieConsentPosition,
  90.                 'simplified' => $this->cookieConsentSimplified,
  91.             ])
  92.         );
  93.         // Cache in ESI should not be shared
  94.         $response->setPrivate();
  95.         $response->setMaxAge(0);
  96.         return $response;
  97.     }
  98.     /**
  99.      * Show cookie consent.
  100.      *
  101.      * @Route("/cookie_consent_alt", name="ch_cookie_consent.show_if_cookie_consent_not_set")
  102.      */
  103.     public function showIfCookieConsentNotSet(Request $request): Response
  104.     {
  105.         if ($this->cookieChecker->isCookieConsentSavedByUser() === false) {
  106.             return $this->show($request);
  107.         }
  108.         return new Response();
  109.     }
  110.     /**
  111.      * Create cookie consent form.
  112.      */
  113.     protected function createCookieConsentForm(): FormInterface
  114.     {
  115.         if ($this->formAction === null) {
  116.             $form $this->formFactory->create(CookieConsentType::class);
  117.         } else {
  118.             $form $this->formFactory->create(
  119.                 CookieConsentType::class,
  120.                 null,
  121.                 [
  122.                     'action' => $this->router->generate($this->formAction),
  123.                 ]
  124.             );
  125.         }
  126.         return $form;
  127.     }
  128.     /**
  129.      * Set locale if available as GET parameter.
  130.      */
  131.     protected function setLocale(Request $request)
  132.     {
  133.         $locale $request->get('locale');
  134.         if (empty($locale) === false) {
  135.             $this->translator->setLocale($locale);
  136.             $request->setLocale($locale);
  137.         }
  138.     }
  139. }