vendor/nellapp/sdk-bundle/src/EventSubscriber/DeviceIdSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\EventSubscriber;
  3. use Nellapp\Bundle\SDKBundle\Routing\Utils\DomainsRoutingUtils;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. class DeviceIdSubscriber implements EventSubscriberInterface
  9. {
  10. public function __construct(
  11. private DomainsRoutingUtils $domainsRoutingUtils,
  12. )
  13. {
  14. }
  15. public static function getSubscribedEvents(): array
  16. {
  17. return [
  18. KernelEvents::RESPONSE => 'onResponse',
  19. ];
  20. }
  21. public function onResponse(ResponseEvent $event): void
  22. {
  23. $request = $event->getRequest();
  24. if ($request->cookies->has('DEVICE_ID')) {
  25. return;
  26. }
  27. $host = $request->getHost();
  28. $deviceId = bin2hex(random_bytes(16));
  29. $cookie = Cookie::create('DEVICE_ID')
  30. ->withValue($deviceId)
  31. ->withPath('/')
  32. ->withDomain($this->domainsRoutingUtils->getDomain($host))
  33. ->withHttpOnly(true)
  34. ->withSecure(true)
  35. ->withSameSite('lax');
  36. $event->getResponse()->headers->setCookie($cookie);
  37. }
  38. }