Cookie::setRaw()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 30
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Cookie;
6
7
class Cookie
8
{
9
    public function __construct(private Options $options)
10
    {
11
    }
12
13
14
    /**
15
     * @param string|null $key
16
     * @return array<string,string>|string|null
17
     */
18
    public function get(?string $key = null): null|array|string
19
    {
20
        /** @var string[] $cookie */
21
        $cookie = $this->options->getRequest()->getCookieParams();
22
        if ($key === null) {
23
            return $cookie;
24
        }
25
26
        return $cookie[$key] ?? null;
27
    }
28
29
30
    public function has(string $key): bool
31
    {
32
        return array_key_exists($key, $this->options->getRequest()->getCookieParams());
33
    }
34
35
36
    /**
37
     * @throws NotCorrectTtlString
38
     */
39
    public function delete(string $name): void
40
    {
41
        if ($this->set($name, '', '-1 day')) {
42
            /** @var array<string, string> $cookie */
43
            $cookie = $this->options->getRequest()->getCookieParams();
44
            unset($cookie[$name]);
45
            $this->options->setRequest($this->options->getRequest()->withCookieParams($cookie));
46
        }
47
    }
48
49
50
    /**
51
     * @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{path?: string, dom...one'|'Strict'|'strict'} at position 20 could not be parsed: Unknown type name ''Lax'' at position 20 in array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'}.
Loading history...
52
     * @throws NotCorrectTtlString
53
     * @see https://www.php.net/manual/ru/function.setcookie.php
54
     */
55
    public function set(
56
        string $key,
57
        string $value,
58
        bool|int|string|\DateTimeInterface $ttl = true,
59
        array $addedOptions = []
60
    ): bool {
61
        $setParams = $this->getSetParams($key, $value, $ttl, $addedOptions);
62
63
        /**
64
         * @psalm-suppress InvalidArgument, MixedArgument
65
         */
66
        if (setcookie(...$setParams)) {
0 ignored issues
show
Bug introduced by
It seems like $setParams can also be of type array; however, parameter $name of setcookie() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        if (setcookie(/** @scrutinizer ignore-type */ ...$setParams)) {
Loading history...
67
            if ($setParams[2]['expires'] < 0) {
68
                return true;
69
            }
70
            $this->options->setRequest(
71
                $this->options->getRequest()->withCookieParams(
72
                    array_merge(
73
                        $this->options->getRequest()->getCookieParams(),
74
                        [
75
                            $key => urlencode($value)
76
                        ]
77
                    )
78
                )
79
            );
80
            return true;
81
        }
82
        return false;
83
    }
84
85
    /**
86
     * Отправляет cookie без URL-кодирования значения
87
     * @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{path?: string, dom...one'|'Strict'|'strict'} at position 20 could not be parsed: Unknown type name ''Lax'' at position 20 in array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'}.
Loading history...
88
     * @throws NotCorrectTtlString
89
     * @see https://www.php.net/manual/ru/function.setrawcookie.php
90
     */
91
    public function setRaw(
92
        string $key,
93
        string $value,
94
        bool|int|string|\DateTimeInterface $ttl = true,
95
        array $addedOptions = []
96
    ): bool {
97
        $setParams = $this->getSetParams($key, $value, $ttl, $addedOptions);
98
99
        /**
100
         * @psalm-suppress InvalidArgument, MixedArgument
101
         */
102
        if (setrawcookie(...$setParams)) {
0 ignored issues
show
Bug introduced by
It seems like $setParams can also be of type array; however, parameter $name of setrawcookie() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

102
        if (setrawcookie(/** @scrutinizer ignore-type */ ...$setParams)) {
Loading history...
103
            if ($setParams[2]['expires'] < 0) {
104
                return true;
105
            }
106
            $this->options->setRequest(
107
                $this->options->getRequest()->withCookieParams(
108
                    array_merge(
109
                        $this->options->getRequest()->getCookieParams(),
110
                        [
111
                            $key => $value
112
                        ]
113
                    )
114
                )
115
            );
116
            return true;
117
        }
118
119
120
        return false;
121
    }
122
123
    /**
124
     * @param array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'} $addedOptions
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{path?: string, dom...one'|'Strict'|'strict'} at position 20 could not be parsed: Unknown type name ''Lax'' at position 20 in array{path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'}.
Loading history...
125
     * @return array{string, string, array{expires: int, path: string, domain: string, secure: bool, httponly: bool, samesite?: 'Lax'|'lax'|'None'|'none'|'Strict'|'strict'}}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{string, string, ar...ne'|'Strict'|'strict'}} at position 2 could not be parsed: Expected ':' at position 2, but found 'string'.
Loading history...
126
     * @throws NotCorrectTtlString
127
     */
128
    private function getSetParams(
129
        string $key,
130
        string $value,
131
        bool|int|string|\DateTimeInterface $ttl,
132
        array $addedOptions = []
133
    ): array {
134
        $expires = new Expires($ttl);
135
        $this->options->setExpires($expires->getExpires());
136
137
        return [$key, $value, $this->options->getOptions($addedOptions)];
138
    }
139
}
140