Passed
Push — master ( 580f55...117038 )
by Enjoys
02:13
created

Cookie::setExpires()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 28
rs 8.8333
cc 7
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Cookie;
6
7
use Enjoys\Http\ServerRequest;
8
use Enjoys\Http\ServerRequestInterface;
9
10
class Cookie
11
{
12
    /**
13
     * @var Options
14
     */
15
    private Options $options;
16
17
    public function __construct(Options $options)
18
    {
19
        $this->options = $options;
20
    }
21
22
    public static function get(string $key): ?string
23
    {
24
        return (self::has($key)) ? $_COOKIE[$key] : null;
25
    }
26
27
28
    public static function has(string $key): bool
29
    {
30
        return array_key_exists($key, $_COOKIE);
31
    }
32
33
34
    /**
35
     * @param string $name
36
     * @throws Exception
37
     */
38
    public function delete(string $name): void
39
    {
40
        $this->set($name, '', '-1 day');
41
    }
42
43
44
    /**
45
     * @param string $key
46
     * @param string|null $value
47
     * @param bool|int|string $ttl
48
     * @param array<mixed> $addedOptions Ассоциативный массив (array), который может иметь любой из ключей: expires, path, domain, secure, httponly и samesite.
49
     * @throws Exception
50
     * @see https://www.php.net/manual/ru/function.setcookie.php
51
     */
52
    public function set(string $key, ?string $value, $ttl = true, array $addedOptions = []): void
53
    {
54
        $setParams = $this->getSetParams($key, $value, $ttl, $addedOptions);
55
        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

55
        setcookie(/** @scrutinizer ignore-type */ ...$setParams);
Loading history...
56
    }
57
58
    /**
59
     * Отправляет cookie без URL-кодирования значения
60
     * @param string $key
61
     * @param string|null $value
62
     * @param bool|int|string $ttl
63
     * @param array<mixed> $addedOptions
64
     * @throws Exception
65
     * @see https://www.php.net/manual/ru/function.setrawcookie.php
66
     */
67
    public function setRaw(string $key, ?string $value, $ttl = true, array $addedOptions = []): void
68
    {
69
        $setParams = $this->getSetParams($key, $value, $ttl, $addedOptions);
70
        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

70
        setrawcookie(/** @scrutinizer ignore-type */ ...$setParams);
Loading history...
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param string|null $value
76
     * @param bool|int|string $ttl
77
     * @param array<mixed> $addedOptions
78
     * @return array<mixed>
79
     * @throws Exception
80
     */
81
    private function getSetParams(string $key, ?string $value, $ttl = true, array $addedOptions = []): array
82
    {
83
        if (headers_sent($filename, $linenum)) {
84
            throw new Exception(
85
                sprintf(
86
                    "Cookies are not set. Headers have already been sent to %s in line %s\n",
87
                    $filename,
88
                    $linenum
89
                )
90
            );
91
        }
92
93
        if ($value !== null) {
94
            $expires = new Expires($ttl);
95
            $this->options->setExpires($expires->getExpires());
96
        }
97
98
        return [
99
            $key,
100
            (string)$value,
101
            $this->options->getOptions($addedOptions),
102
        ];
103
    }
104
}
105