Cookie::__toString()   B
last analyzed

Complexity

Conditions 8
Paths 64

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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