vendor/connectholland/cookie-consent-bundle/EventSubscriber/CookieConsentFormSubscriber.php line 61

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\EventSubscriber;
  8. use ConnectHolland\CookieConsentBundle\Cookie\CookieHandler;
  9. use ConnectHolland\CookieConsentBundle\Cookie\CookieLogger;
  10. use ConnectHolland\CookieConsentBundle\Enum\CookieNameEnum;
  11. use ConnectHolland\CookieConsentBundle\Form\CookieConsentType;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\HttpFoundation\Cookie;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  19. use Symfony\Component\HttpKernel\Event\KernelEvent;
  20. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. class CookieConsentFormSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var FormFactoryInterface
  26.      */
  27.     private $formFactory;
  28.     /**
  29.      * @var CookieLogger
  30.      */
  31.     private $cookieLogger;
  32.     /**
  33.      * @var bool
  34.      */
  35.     private $useLogger;
  36.     public function __construct(FormFactoryInterface $formFactoryCookieLogger $cookieLoggerbool $useLogger)
  37.     {
  38.         $this->formFactory  $formFactory;
  39.         $this->cookieLogger $cookieLogger;
  40.         $this->useLogger    $useLogger;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.            KernelEvents::RESPONSE => ['onResponse'],
  46.         ];
  47.     }
  48.     /**
  49.      * Checks if form has been submitted and saves users preferences in cookies by calling the CookieHandler.
  50.      */
  51.     public function onResponse(KernelEvent $event): void
  52.     {
  53.         if ($event instanceof FilterResponseEvent === false && $event instanceof ResponseEvent === false) {
  54.             throw new \RuntimeException('No ResponseEvent class found');
  55.         }
  56.         $request  $event->getRequest();
  57.         $response $event->getResponse();
  58.         $form $this->createCookieConsentForm();
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             $this->handleFormSubmit($form->getData(), $request$response);
  62.         }
  63.     }
  64.     /**
  65.      * Handle form submit.
  66.      */
  67.     protected function handleFormSubmit(array $categoriesRequest $requestResponse $response): void
  68.     {
  69.         $cookieConsentKey $this->getCookieConsentKey($request);
  70.         $cookieHandler = new CookieHandler($response);
  71.         $cookieHandler->save($categories$cookieConsentKey);
  72.         if ($this->useLogger) {
  73.             $this->cookieLogger->log($categories$cookieConsentKey);
  74.         }
  75.     }
  76.     /**
  77.      *  Return existing key from cookies or create new one.
  78.      */
  79.     protected function getCookieConsentKey(Request $request): string
  80.     {
  81.         return $request->cookies->get(CookieNameEnum::COOKIE_CONSENT_KEY_NAME) ?? uniqid();
  82.     }
  83.     /**
  84.      * Create cookie consent form.
  85.      */
  86.     protected function createCookieConsentForm(): FormInterface
  87.     {
  88.         return $this->formFactory->create(CookieConsentType::class);
  89.     }
  90. }