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

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.         return 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.     }
  81.     /**
  82.      * Show cookie consent.
  83.      *
  84.      * @Route("/cookie_consent_alt", name="ch_cookie_consent.show_if_cookie_consent_not_set")
  85.      */
  86.     public function showIfCookieConsentNotSet(Request $request): Response
  87.     {
  88.         if ($this->cookieChecker->isCookieConsentSavedByUser() === false) {
  89.             return $this->show($request);
  90.         }
  91.         return new Response();
  92.     }
  93.     /**
  94.      * Create cookie consent form.
  95.      */
  96.     protected function createCookieConsentForm(): FormInterface
  97.     {
  98.         return $this->formFactory->create(CookieConsentType::class);
  99.     }
  100.     /**
  101.      * Set locale if available as GET parameter.
  102.      */
  103.     protected function setLocale(Request $request)
  104.     {
  105.         $locale $request->get('locale');
  106.         if (empty($locale) === false) {
  107.             $this->translator->setLocale($locale);
  108.             $request->setLocale($locale);
  109.         }
  110.     }
  111. }