Cookie::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace RafaelDms\Cookie;
4
5
/**
6
 * Class Cookie
7
 * @package RafaelDms\Cookie
8
 */
9
final class Cookie
10
{
11
    /** @var string the name of the cookie which is also the key for future accesses via `$_COOKIE[...]` */
12
    private string $name;
13
14
    /** @var string|null the value of the cookie that will be stored on the client's machine */
15
    private ?string $value;
16
17
    /** @var int the Unix timestamp indicating the time that the cookie will expire at, i.e. usually `time() + $seconds` */
18
    private int $expiryTime;
19
20
    /** @var string|null the path on the server that the cookie will be valid for (including all sub-directories), e.g. an empty string for the current directory or `/` for the root directory */
21
    private ?string $path;
22
23
    /** @var string|null the domain that the cookie will be valid for (including subdomains) or `null` for the current host (excluding subdomains) */
24
    private ?string $domain;
25
26
    /** @var bool indicates that the cookie should be accessible through the HTTP protocol only and not through scripting languages */
27
    private bool $httpOnly;
28
29
    /** @var bool indicates that the cookie should be sent back by the client over secure HTTPS connections only */
30
    private bool $secureOnly;
31
32
    /** @var bool indicates that the cookie encrypt this value */
33
    private bool $encrypt;
34
35
    /**
36
     * Prepares a new cookie
37
     *
38
     * @param string $name the name of the cookie which is also the key for future accesses via `$_COOKIE[...]`
39
     */
40
    public function __construct(string $name)
41
    {
42
        $this->name = $name;
43
        $this->value = null;
44
        $this->expiryTime = 0;
45
        $this->path = "/";
46
        $this->domain = null;
47
        $this->httpOnly = true;
48
        $this->secureOnly = false;
49
        $this->encrypt = true;
50
    }
51
52
    /**
53
     * Returns the name of the cookie
54
     *
55
     * @return string the name of the cookie which is also the key for future acesses via `$_COOKIE[...]`
56
     */
57
    public function getName(): string
58
    {
59
        return $this->name;
60
    }
61
62
63
    /**
64
     * Return the values of the cookie
65
     * @return string|null the value of the cookie that will be stored on the client's machine
66
     */
67
    public function getValue(): null|string
68
    {
69
        return $this->value;
70
    }
71
72
    /**
73
     * Sets the value for the cookie
74
     *
75
     * @param mixed|null $value the value of the cookie that will be stored on the client's machine
76
     * @return Cookie this instance ofr chaining
77
     */
78
    public function setValue(mixed $value): Cookie
79
    {
80
        $this->value = $value;
81
        return $this;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    public function isEncrypt(): bool
88
    {
89
        return $this->encrypt;
90
    }
91
92
    /**
93
     * @param bool $encrypt
94
     * @return Cookie
95
     */
96
    public function setEncrypt(bool $encrypt): Cookie
97
    {
98
        $this->encrypt = $encrypt;
99
        return $this;
100
    }
101
102
    /**
103
     * Returns the expiry time of the cookie
104
     *
105
     * @return int the Unix timestamp indicating the time that the cookie will expire at, i.e. usually `time() + $seconds`
106
     */
107
    public function getExpiryTime(): int
108
    {
109
        return $this->expiryTime;
110
    }
111
112
    /**
113
     * Sets the expiry time for the cookie
114
     *
115
     * @param int $expiryTime the Unix timestamp indicating the time that the cookie will expire at, i.e. usually `time() + $seconds`
116
     * @return static this instance for chaining
117
     */
118
    public function setExpiryTime(int $expiryTime): Cookie
119
    {
120
        $this->expiryTime = $expiryTime;
121
122
        return $this;
123
    }
124
125
126
    /**
127
     * Returns the path of the cookie
128
     *
129
     * @return string|null the path on the server that the cookie will be valid for (including all sub-directories), e.g. an empty string for the current directory or `/` for the root directory
130
     */
131
    public function getPath(): ?string
132
    {
133
        return $this->path;
134
    }
135
136
    /**
137
     * Sets the path for the cookie
138
     *
139
     * @param string|null $path the path on the server that the cookie will be valid for (including all sub-directories), e.g. an empty string for the current directory or `/` for the root directory
140
     * @return static this instance for chaining
141
     */
142
    public function setPath(?string $path): Cookie
143
    {
144
        $this->path = $path;
145
        return $this;
146
    }
147
148
    /**
149
     * Returns the domain of the cookie
150
     *
151
     * @return string|null the domain that the cookie will be valid for (including subdomains) or `null` for the current host (excluding subdomains)
152
     */
153
    public function getDomain(): ?string
154
    {
155
        return $this->domain;
156
    }
157
158
    /**
159
     * Sets the domain for the cookie
160
     *
161
     * @param string|null $domain the domain that the cookie will be valid for (including subdomains) or `null` for the current host (excluding subdomains)
162
     * @return static this instance for chaining
163
     */
164
    public function setDomain(?string $domain): Cookie
165
    {
166
        $this->domain = $domain;
167
        return $this;
168
    }
169
170
    /**
171
     * Returns whether the cookie should be accessible through HTTP only
172
     * @return bool whether the cookie should be accessible through the HTTP protocol only and not through scripting languages
173
     */
174
    public function isHttpOnly(): bool
175
    {
176
        return $this->httpOnly;
177
    }
178
179
    /**
180
     * Sets whether the cookie should be accessible through HTTP only
181
     *
182
     * @param bool $httpOnly indicates that the cookie should be accessible through the HTTP protocol only and not through scripting languages
183
     * @return static this instance for chaining
184
     */
185
    public function setHttpOnly(bool $httpOnly): Cookie
186
    {
187
        $this->httpOnly = $httpOnly;
188
        return $this;
189
    }
190
191
    /**
192
     * Returns whether the cookie should be sent over HTTPS only
193
     *
194
     * @return bool whether the cookie should be sent back by the client over secure HTTPS connections only
195
     */
196
    public function isSecureOnly(): bool
197
    {
198
        return $this->secureOnly;
199
    }
200
201
    /**
202
     * Sets whether the cookie should be sent over HTTPS only
203
     * @param bool $secureOnly indicates that the cookie should be sent back by the client over secure HTTPS connections only
204
     * @return static this instance for chaining
205
     */
206
    public function setSecureOnly(bool $secureOnly): Cookie
207
    {
208
        $this->secureOnly = $secureOnly;
209
        return $this;
210
    }
211
212
    /**
213
     * Saves the cookie
214
     * @return bool whether the cookie header has successfully been sent (and will *probably* cause the client to set the cookie)
215
     */
216
    public function save(): bool
217
    {
218
        return StaticCookie::set($this->name, $this->value, $this->expiryTime, $this->path, $this->encrypt, $this->domain, $this->secureOnly);
0 ignored issues
show
Bug introduced by
$this->secureOnly of type boolean is incompatible with the type null|string expected by parameter $domain of RafaelDms\Cookie\StaticCookie::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
        return StaticCookie::set($this->name, $this->value, $this->expiryTime, $this->path, $this->encrypt, $this->domain, /** @scrutinizer ignore-type */ $this->secureOnly);
Loading history...
Bug introduced by
$this->path of type null|string is incompatible with the type boolean expected by parameter $secureOnly of RafaelDms\Cookie\StaticCookie::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
        return StaticCookie::set($this->name, $this->value, $this->expiryTime, /** @scrutinizer ignore-type */ $this->path, $this->encrypt, $this->domain, $this->secureOnly);
Loading history...
219
    }
220
221
    /**
222
     * @return bool
223
     */
224
    public function saveAndSet(): bool
225
    {
226
        $_COOKIE[$this->name] = $this->value;
227
        return $this->save();
228
    }
229
230
    /**
231
     * Deletes the cookie
232
     *
233
     * @return bool whether the cookie header has successfully been sent (and will *probably* cause the client to delete the cookie)
234
     */
235
    public function delete(): bool
236
    {
237
        // set the copied cookie's value to an empty string which internally sets the required options for a deletion
238
        $this->setValue('');
239
240
        // save the copied "deletion" cookie
241
        return $this->save();
242
    }
243
244
    /**
245
     * Deletes the cookie and immediately removes the corresponding variable from the superglobal `$_COOKIE` array
246
     *
247
     * The variable would otherwise only be deleted at the start of the next HTTP request
248
     *
249
     * @return bool whether the cookie header has successfully been sent (and will *probably* cause the client to delete the cookie)
250
     */
251
    public function deleteAndUnset(): bool
252
    {
253
        StaticCookie::unset($this->name);
254
255
        return $this->delete();
256
    }
257
258
    /**
259
     * Verify if exists cookie
260
     * @return bool
261
     */
262
    public function hasCookie(): bool
263
    {
264
        if (StaticCookie::has($this->name, $this->value)) {
265
            return true;
266
        }
267
        return false;
268
    }
269
}