|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the ConnectHolland CookieConsentBundle package. |
| 7 | + * (c) Connect Holland. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ConnectHolland\CookieConsentBundle\EventSubscriber; |
| 11 | + |
| 12 | +use ConnectHolland\CookieConsentBundle\Cookie\CookieHandler; |
| 13 | +use ConnectHolland\CookieConsentBundle\DOM\DOMParser; |
| 14 | +use ConnectHolland\CookieConsentBundle\Form\CookieConsentType; |
| 15 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 16 | +use Symfony\Component\Form\FormFactoryInterface; |
| 17 | +use Symfony\Component\Form\FormInterface; |
| 18 | +use Symfony\Component\HttpFoundation\Cookie; |
| 19 | +use Symfony\Component\HttpFoundation\Response; |
| 20 | +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
| 21 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 22 | + |
| 23 | +class AppendCookieConsentSubcriber implements EventSubscriberInterface |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var CookieHandler |
| 27 | + */ |
| 28 | + private $cookieHandler; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var DOMParser |
| 32 | + */ |
| 33 | + private $domParser; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var FormFactoryInterface |
| 37 | + */ |
| 38 | + private $formFactory; |
| 39 | + |
| 40 | + public function __construct(CookieHandler $cookieHandler, DOMParser $domParser, FormFactoryInterface $formFactory) |
| 41 | + { |
| 42 | + $this->cookieHandler = $cookieHandler; |
| 43 | + $this->domParser = $domParser; |
| 44 | + $this->formFactory = $formFactory; |
| 45 | + } |
| 46 | + |
| 47 | + public static function getSubscribedEvents(): array |
| 48 | + { |
| 49 | + return [ |
| 50 | + KernelEvents::RESPONSE => ['onResponse'], |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Appends Cookie Consent scripts into body. |
| 56 | + */ |
| 57 | + public function onResponse(FilterResponseEvent $event): void |
| 58 | + { |
| 59 | + if ($event->isMasterRequest() === false || $this->cookieHandler->hasCookieConsent()) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + $request = $event->getRequest(); |
| 64 | + $response = $event->getResponse(); |
| 65 | + |
| 66 | + $form = $this->createCookieConsentForm(); |
| 67 | + $form->handleRequest($request); |
| 68 | + |
| 69 | + // If form is submitted save in cookies, otherwise display cookie consent |
| 70 | + if ($form->isSubmitted() && $form->isValid()) { |
| 71 | + $this->cookieHandler->saveCookieConsent($response, $form->getData()); |
| 72 | + } else { |
| 73 | + $this->showCookieConsent($response, $form); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Create cookie consent form. |
| 79 | + */ |
| 80 | + protected function createCookieConsentForm(): FormInterface |
| 81 | + { |
| 82 | + return $this->formFactory->create(CookieConsentType::class); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Append cookie consent to Kernel Response. |
| 87 | + */ |
| 88 | + protected function showCookieConsent(Response $response, FormInterface $form) |
| 89 | + { |
| 90 | + $response->setContent( |
| 91 | + $this->domParser->appendCookieConsent($response, $form) |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments