Passed
Push — master ( ff614d...6c840c )
by Anton
05:03 queued 02:24
created

CookieManager::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 2
dl 0
loc 12
rs 10
c 2
b 0
f 2
cc 1
nc 1
nop 7
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Cookies;
11
12
use Psr\Container\ContainerInterface;
13
use Psr\Container\NotFoundExceptionInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Spiral\Core\Container\SingletonInterface;
16
use Spiral\Core\Exception\ScopeException;
17
18
/**
19
 * Cookies manages provides the ability to write and read cookies from the active request/response scope.
20
 */
21
final class CookieManager implements SingletonInterface
22
{
23
    /** @var ContainerInterface */
24
    private $container;
25
26
    /**
27
     * @param ContainerInterface $container
28
     */
29
    public function __construct(ContainerInterface $container)
30
    {
31
        $this->container = $container;
32
    }
33
34
    /**
35
     * @param string $name
36
     * @return bool
37
     *
38
     * @throws ScopeException
39
     */
40
    public function has(string $name): bool
41
    {
42
        return array_key_exists($name, $this->getRequest()->getCookieParams());
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param null   $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
48
     * @return mixed
49
     *
50
     * @throws ScopeException
51
     */
52
    public function get(string $name, $default = null)
53
    {
54
        return $this->getRequest()->getCookieParams()[$name] ?? $default;
55
    }
56
57
    /**
58
     * Schedule new cookie. Cookie will be send while dispatching request.
59
     *
60
     * Domain, path, and secure values can be left in null state, in this case cookie manager will
61
     * populate them automatically.
62
     *
63
     * @link http://php.net/manual/en/function.setcookie.php
64
     *
65
     * @param string $name     The name of the cookie.
66
     * @param string $value    The value of the cookie. This value is stored on the clients
67
     *                         computer; do not store sensitive information.
68
     * @param int    $lifetime Cookie lifetime. This value specified in seconds and declares period
69
     *                         of time in which cookie will expire relatively to current time()
70
     *                         value.
71
     * @param string $path     The path on the server in which the cookie will be available on.
72
     *                         If set to '/', the cookie will be available within the entire
73
     *                         domain.
74
     *                         If set to '/foo/', the cookie will only be available within the
75
     *                         /foo/
76
     *                         directory and all sub-directories such as /foo/bar/ of domain. The
77
     *                         default value is the current directory that the cookie is being set
78
     *                         in.
79
     * @param string $domain   The domain that the cookie is available. To make the cookie
80
     *                         available
81
     *                         on all subdomains of example.com then you'd set it to
82
     *                         '.example.com'.
83
     *                         The . is not required but makes it compatible with more browsers.
84
     *                         Setting it to www.example.com will make the cookie only available in
85
     *                         the www subdomain. Refer to tail matching in the spec for details.
86
     * @param bool   $secure   Indicates that the cookie should only be transmitted over a secure
87
     *                         HTTPS connection from the client. When set to true, the cookie will
88
     *                         only be set if a secure connection exists. On the server-side, it's
89
     *                         on the programmer to send this kind of cookie only on secure
90
     *                         connection (e.g. with respect to $_SERVER["HTTPS"]).
91
     * @param bool   $httpOnly When true the cookie will be made accessible only through the HTTP
92
     *                         protocol. This means that the cookie won't be accessible by
93
     *                         scripting
94
     *                         languages, such as JavaScript. This setting can effectively help to
95
     *                         reduce identity theft through XSS attacks (although it is not
96
     *                         supported by all browsers).
97
     *
98
     * @return $this
99
     *
100
     * @throws ScopeException
101
     */
102
    public function set(
103
        string $name,
104
        string $value = null,
105
        int $lifetime = null,
106
        string $path = null,
107
        string $domain = null,
108
        bool $secure = null,
109
        bool $httpOnly = true
110
    ) {
111
        $this->getCookieQueue()->set($name, $value, $lifetime, $path, $domain, $secure, $httpOnly);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Schedule new cookie instance to be send while dispatching request.
118
     *
119
     * @param Cookie $cookie
120
     *
121
     * @return $this
122
     */
123
    public function schedule(Cookie $cookie): self
124
    {
125
        $this->getCookieQueue()->schedule($cookie);
126
127
        return $this;
128
    }
129
130
    /**
131
     * Schedule cookie removal.
132
     *
133
     * @param string $name
134
     *
135
     * @throws ScopeException
136
     */
137
    public function delete(string $name): void
138
    {
139
        $this->getCookieQueue()->delete($name);
140
    }
141
142
    /**
143
     * Cookies has to be send (specified via global scope).
144
     *
145
     * @return Cookie[]
146
     *
147
     * @throws ScopeException
148
     */
149
    public function getScheduled(): array
150
    {
151
        return $this->getCookieQueue()->getScheduled();
152
    }
153
154
    /**
155
     * @return ServerRequestInterface
156
     *
157
     * @throws ScopeException
158
     */
159
    private function getRequest(): ServerRequestInterface
160
    {
161
        try {
162
            return $this->container->get(ServerRequestInterface::class);
163
        } catch (NotFoundExceptionInterface $e) {
164
            throw new ScopeException("Unable to receive active request", $e->getCode(), $e);
165
        }
166
    }
167
168
    /**
169
     * @return CookieQueue
170
     *
171
     * @throws ScopeException
172
     */
173
    private function getCookieQueue(): CookieQueue
174
    {
175
        $request = $this->getRequest();
176
        $queue = $request->getAttribute(CookieQueue::ATTRIBUTE, null);
177
        if ($queue === null) {
178
            throw new ScopeException("Unable to receive cookie queue, invalid request scope");
179
        }
180
181
        return $queue;
182
    }
183
}