vendor\fatalnetwork\symfony-cookie-consent-bundle\EventSubscriber\CookieConsentFormSubscriber.php line 67

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