Completed
Push — currency-cookie ( 346dee...14f9d4 )
by Kamil
20:35
created

CookieStorage::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\Storage;
13
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpFoundation\Cookie;
16
use Symfony\Component\HttpFoundation\ParameterBag;
17
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
18
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19
use Symfony\Component\HttpKernel\KernelEvents;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class CookieStorage implements StorageInterface, EventSubscriberInterface
25
{
26
    /**
27
     * @var ParameterBag
28
     */
29
    private $requestCookies;
30
31
    /**
32
     * @var ParameterBag
33
     */
34
    private $responseCookies;
35
36
    public function __construct()
37
    {
38
        $this->requestCookies = new ParameterBag();
39
        $this->responseCookies = new ParameterBag();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public static function getSubscribedEvents()
46
    {
47
        return [
48
            KernelEvents::REQUEST => [['onKernelRequest', 1024]],
49
            KernelEvents::RESPONSE => [['onKernelResponse', -1024]],
50
        ];
51
    }
52
53
    /**
54
     * @param GetResponseEvent $event
55
     */
56
    public function onKernelRequest(GetResponseEvent $event)
57
    {
58
        if (!$event->isMasterRequest()) {
59
            return;
60
        }
61
62
        $this->requestCookies = new ParameterBag($event->getRequest()->cookies->all());
63
        $this->responseCookies = new ParameterBag();
64
    }
65
66
    /**
67
     * @param FilterResponseEvent $event
68
     */
69
    public function onKernelResponse(FilterResponseEvent $event)
70
    {
71
        if (!$event->isMasterRequest()) {
72
            return;
73
        }
74
75
        $response = $event->getResponse();
76
        foreach ($this->responseCookies as $name => $value)
77
        {
78
            $response->headers->setCookie(new Cookie($name, $value));
79
        }
80
81
        $this->requestCookies = new ParameterBag();
82
        $this->responseCookies = new ParameterBag();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function has($name)
89
    {
90
        return !in_array($this->get($name), ['', null], true);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function get($name, $default = null)
97
    {
98
        return $this->responseCookies->get($name, $this->requestCookies->get($name, $default));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function set($name, $value)
105
    {
106
        $this->responseCookies->set($name, $value);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function remove($name)
113
    {
114
        $this->set($name, null);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function all()
121
    {
122
        return array_merge($this->responseCookies->all(), $this->requestCookies->all());
123
    }
124
}
125