File size: 2,723 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Helper;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class CookieHelper implements EventSubscriberInterface
{
    private ?Request $request = null;

    /**
     * @var array<string, Cookie>
     */
    private array $cookies = [];

    public function __construct(
        private ?string $path,
        private ?string $domain,
        private bool $secure,
        private bool $httponly,
        private RequestStack $requestStack
    ) {
    }

    /**
     * @param mixed $default
     *
     * @return mixed
     */
    public function getCookie(string $key, $default = null)
    {
        if (null === $this->getRequest()) {
            return $default;
        }

        return $this->getRequest()->cookies->get($key, $default);
    }

    /**
     * @param int|string|float|bool|object|null $value
     */
    public function setCookie(string $name, $value, ?int $expire = 1800, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, ?string $sameSite = Cookie::SAMESITE_LAX): void
    {
        if (null !== $value) {
            $value = (string) $value;
        }

        $cookie = Cookie::create(
            $name,
            $value,
            null !== $expire ? time() + $expire : 0,
            $path ?? $this->path,
            $domain ?? $this->domain,
            $secure ?? $this->secure,
            $httponly ?? $this->httponly,
            false,
            $sameSite
        );

        $this->cookies[$name] = $cookie;
    }

    /**
     * Deletes a cookie by expiring it.
     */
    public function deleteCookie(string $name, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, ?string $sameSite = Cookie::SAMESITE_LAX): void
    {
        $this->setCookie($name, '', -86400, $path, $domain, $secure, $httponly, $sameSite);
    }

    public function onResponse(ResponseEvent $event): void
    {
        foreach ($this->cookies as $cookie) {
            $event->getResponse()->headers->setCookie($cookie);
        }
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::RESPONSE => 'onResponse',
        ];
    }

    private function getRequest(): ?Request
    {
        if (null !== $this->request) {
            return $this->request;
        }

        return $this->request = $this->requestStack->getMainRequest();
    }
}