Passed
Push — master ( 974680...fc5316 )
by Kirill
03:59
created

CookieQueue::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 21
rs 10
cc 3
nc 4
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Cookies;
13
14
final class CookieQueue
15
{
16
    public const ATTRIBUTE = 'cookieQueue';
17
18
    /** @var Cookie[] */
19
    private $scheduled = [];
20
21
    /** @var string|null */
22
    private $domain;
23
24
    /** @var bool */
25
    private $secure;
26
27
    /**
28
     * @param string|null $domain
29
     * @param bool        $secure
30
     */
31
    public function __construct(?string $domain = null, bool $secure = false)
32
    {
33
        $this->domain = $domain;
34
        $this->secure = $secure;
35
    }
36
37
    /**
38
     * Schedule new cookie. Cookie will be send while dispatching request.
39
     *
40
     * Domain, path, and secure values can be left in null state, in this case cookie manager will
41
     * populate them automatically.
42
     *
43
     * @link http://php.net/manual/en/function.setcookie.php
44
     *
45
     * @param string      $name     The name of the cookie.
46
     * @param string|null $value    The value of the cookie. This value is stored on the clients
47
     *                              computer; do not store sensitive information.
48
     * @param int|null    $lifetime Cookie lifetime. This value specified in seconds and declares period
49
     *                              of time in which cookie will expire relatively to current time()
50
     *                              value.
51
     * @param string|null $path     The path on the server in which the cookie will be available on.
52
     *                              If set to '/', the cookie will be available within the entire
53
     *                              domain.
54
     *                              If set to '/foo/', the cookie will only be available within the
55
     *                              /foo/
56
     *                              directory and all sub-directories such as /foo/bar/ of domain. The
57
     *                              default value is the current directory that the cookie is being set
58
     *                              in.
59
     * @param string|null $domain   The domain that the cookie is available. To make the cookie
60
     *                              available
61
     *                              on all subdomains of example.com then you'd set it to
62
     *                              '.example.com'.
63
     *                              The . is not required but makes it compatible with more browsers.
64
     *                              Setting it to www.example.com will make the cookie only available in
65
     *                              the www subdomain. Refer to tail matching in the spec for details.
66
     * @param bool        $secure   Indicates that the cookie should only be transmitted over a secure
67
     *                              HTTPS connection from the client. When set to true, the cookie will
68
     *                              only be set if a secure connection exists. On the server-side, it's
69
     *                              on the programmer to send this kind of cookie only on secure
70
     *                              connection (e.g. with respect to $_SERVER["HTTPS"]).
71
     * @param bool        $httpOnly When true the cookie will be made accessible only through the HTTP
72
     *                              protocol. This means that the cookie won't be accessible by
73
     *                              scripting
74
     *                              languages, such as JavaScript. This setting can effectively help to
75
     *                              reduce identity theft through XSS attacks (although it is not
76
     *                              supported by all browsers).
77
     * @param string|null $sameSite The value of the samesite element should be either None, Lax or Strict. If any of
78
     *                              the allowed options are not given, their default values are the same as the default
79
     *                              values of the explicit parameters. If the samesite element is omitted, no SameSite
80
     *                              cookie attribute is set. When Same-Site attribute is set to "None" it is required
81
     *                              to have "Secure" attribute enable. Otherwise it will be converted to "Lax".
82
     *
83
     * @return $this
84
     */
85
    public function set(
86
        string $name,
87
        string $value = null,
88
        int $lifetime = null,
89
        string $path = null,
90
        string $domain = null,
91
        bool $secure = null,
92
        bool $httpOnly = true,
93
        ?string $sameSite = null
94
    ): self {
95
        if (is_null($domain)) {
96
            //Let's resolve domain via config
97
            $domain = $this->domain;
98
        }
99
100
        if (is_null($secure)) {
101
            $secure = $this->secure;
102
        }
103
104
        return $this->schedule(
105
            new Cookie($name, $value, $lifetime, $path, $domain, $secure, $httpOnly, $sameSite)
106
        );
107
    }
108
109
    /**
110
     * Schedule new cookie instance to be send while dispatching request.
111
     *
112
     * @param Cookie $cookie
113
     *
114
     * @return CookieQueue
115
     */
116
    public function schedule(Cookie $cookie): CookieQueue
117
    {
118
        $this->scheduled[] = $cookie;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Schedule cookie removal.
125
     *
126
     * @param string $name
127
     */
128
    public function delete(string $name): void
129
    {
130
        foreach ($this->scheduled as $index => $cookie) {
131
            if ($cookie->getName() === $name) {
132
                unset($this->scheduled[$index]);
133
            }
134
        }
135
136
        $this->scheduled[] = new Cookie($name, null, -86400);
137
    }
138
139
    /**
140
     * Cookies has to be send (specified via global scope).
141
     *
142
     * @return Cookie[]
143
     */
144
    public function getScheduled(): array
145
    {
146
        return $this->scheduled;
147
    }
148
}
149