Cookie::withPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Message\Header\Value;
15
16
use DateTimeInterface;
17
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Valkyrja\Http\Message\Enum\SameSite;
19
use Valkyrja\Http\Message\Header\Value\Component\Component;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\He...lue\Component\Component was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Valkyrja\Http\Message\Header\Value\Contract\CookieContract as Contract;
21
use Valkyrja\Support\Time\Time;
22
23
use function array_filter;
24
use function gmdate;
25
use function implode;
26
use function urlencode;
27
28
class Cookie extends Value implements Contract
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Header\Value\Value was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
{
30
    /**
31
     * @param string        $name     The cookie's name
32
     * @param string|null   $value    [optional] The cookie's value
33
     * @param int           $expire   [optional] The time the cookie should expire
34
     * @param string        $path     [optional] The path the cookie is available to
35
     * @param string|null   $domain   [optional] The domain the cookie is available to
36
     * @param bool          $secure   [optional] Whether the cookie should only be
37
     *                                transmitted over a secure HTTPS connection
38
     * @param bool          $httpOnly [optional] Whether the cookie will be made
39
     *                                accessible only through the HTTP protocol
40
     * @param bool          $raw      [optional] Whether the cookie value should be
41
     *                                sent with no url encoding
42
     * @param SameSite|null $sameSite [optional] Whether the cookie will be available
43
     *                                for cross-site requests
44
     */
45
    public function __construct(
46
        protected string $name,
47
        protected string|null $value = null,
48
        protected int $expire = 0,
49
        protected string $path = '/',
50
        protected string|null $domain = null,
51
        protected bool $secure = false,
52
        protected bool $httpOnly = true,
53
        protected bool $raw = false,
54
        protected SameSite|null $sameSite = null,
55
        protected bool $delete = false
56
    ) {
57
        parent::__construct();
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    #[Override]
64
    public function __toString(): string
65
    {
66
        $value  = $this->value;
67
        $expire = $this->expire;
68
        $maxAge = $this->getMaxAge();
69
70
        if ($this->delete || $value === null) {
71
            $expire = Time::get() - 31536001;
72
            $maxAge = -31536001;
73
            $value  = 'delete';
74
        }
75
76
        $arr = [
77
            new Component(urlencode($this->name), urlencode($value)),
78
        ];
79
80
        if ($expire !== 0) {
81
            $arr[] = new Component('expires', gmdate(DateTimeInterface::COOKIE, $expire));
82
            $arr[] = new Component('max-age', (string) $maxAge);
83
        }
84
85
        $arr[] = new Component('path', $this->path);
86
        $arr[] = $this->domain !== null ? new Component('domain', $this->domain) : '';
87
        $arr[] = $this->secure ? new Component('secure') : '';
88
        $arr[] = $this->httpOnly ? new Component('httponly') : '';
89
        $arr[] = $this->sameSite ? new Component('samesite', $this->sameSite->value) : '';
90
91
        $arrToString = array_map('strval', $arr);
92
93
        $arrFilteredEmptyStrings = array_filter($arrToString);
94
95
        return implode('; ', $arrFilteredEmptyStrings);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    #[Override]
102
    public function delete(): static
103
    {
104
        $new = clone $this;
105
106
        $new->delete = true;
107
108
        return $new;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    #[Override]
115
    public function getMaxAge(): int
116
    {
117
        return $this->expire > 0
118
            ? $this->expire - Time::get()
119
            : 0;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    #[Override]
126
    public function getName(): string
127
    {
128
        return $this->name;
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134
    #[Override]
135
    public function withName(string $name): static
136
    {
137
        $new = clone $this;
138
139
        $new->name = $name;
140
141
        return $new;
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147
    #[Override]
148
    public function getValue(): string|null
149
    {
150
        return $this->value;
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156
    #[Override]
157
    public function withValue(string|null $value = null): static
158
    {
159
        $new = clone $this;
160
161
        $new->value = $value;
162
163
        return $new;
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    #[Override]
170
    public function getExpire(): int
171
    {
172
        return $this->expire;
173
    }
174
175
    /**
176
     * @inheritDoc
177
     */
178
    #[Override]
179
    public function withExpire(int $expire): static
180
    {
181
        $new = clone $this;
182
183
        $new->expire = $expire;
184
185
        return $new;
186
    }
187
188
    /**
189
     * @inheritDoc
190
     */
191
    #[Override]
192
    public function getPath(): string
193
    {
194
        return $this->path;
195
    }
196
197
    /**
198
     * @inheritDoc
199
     */
200
    #[Override]
201
    public function withPath(string $path): static
202
    {
203
        $new = clone $this;
204
205
        $new->path = $path;
206
207
        return $new;
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    #[Override]
214
    public function getDomain(): string|null
215
    {
216
        return $this->domain;
217
    }
218
219
    /**
220
     * @inheritDoc
221
     */
222
    #[Override]
223
    public function withDomain(string|null $domain = null): static
224
    {
225
        $new = clone $this;
226
227
        $new->domain = $domain;
228
229
        return $new;
230
    }
231
232
    /**
233
     * @inheritDoc
234
     */
235
    #[Override]
236
    public function isSecure(): bool
237
    {
238
        return $this->secure;
239
    }
240
241
    /**
242
     * @inheritDoc
243
     */
244
    #[Override]
245
    public function withSecure(bool $secure): static
246
    {
247
        $new = clone $this;
248
249
        $new->secure = $secure;
250
251
        return $new;
252
    }
253
254
    /**
255
     * @inheritDoc
256
     */
257
    #[Override]
258
    public function isHttpOnly(): bool
259
    {
260
        return $this->httpOnly;
261
    }
262
263
    /**
264
     * @inheritDoc
265
     */
266
    #[Override]
267
    public function withHttpOnly(bool $httpOnly = false): static
268
    {
269
        $new = clone $this;
270
271
        $new->httpOnly = $httpOnly;
272
273
        return $new;
274
    }
275
276
    /**
277
     * @inheritDoc
278
     */
279
    #[Override]
280
    public function isRaw(): bool
281
    {
282
        return $this->raw;
283
    }
284
285
    /**
286
     * @inheritDoc
287
     */
288
    #[Override]
289
    public function withRaw(bool $raw): static
290
    {
291
        $new = clone $this;
292
293
        $new->raw = $raw;
294
295
        return $new;
296
    }
297
298
    /**
299
     * @inheritDoc
300
     */
301
    #[Override]
302
    public function getSameSite(): SameSite|null
303
    {
304
        return $this->sameSite;
305
    }
306
307
    /**
308
     * @inheritDoc
309
     */
310
    #[Override]
311
    public function withSameSite(SameSite|null $sameSite = null): static
312
    {
313
        $new = clone $this;
314
315
        $new->sameSite = $sameSite;
316
317
        return $new;
318
    }
319
}
320