Passed
Push — master ( 86aed3...9c525b )
by Petr
06:19 queued 03:36
created

CookieAdapter::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_input\Simplified;
4
5
6
use ArrayAccess;
7
use kalanis\kw_input\InputException;
8
9
10
/**
11
 * Class CookieAdapter
12
 * @package kalanis\kw_input\Extras
13
 * Accessing _COOKIES via ArrayAccess
14
 * Also set them into the headers
15
 * "Cannot modify header information - headers already sent"
16
 */
17
class CookieAdapter implements ArrayAccess
18
{
19
    use TNullBytes;
20
21
    /** @var string */
22
    protected static $domain = '';
23
    /** @var string */
24
    protected static $path = '';
25
    /** @var int|null */
26
    protected static $expire = null;
27
    /** @var bool */
28
    protected static $secure = false;
29
    /** @var bool */
30
    protected static $httpOnly = false;
31
    /** @var bool */
32
    protected static $sameSite = false;
33
    /** @var bool */
34
    protected static $dieOnSent = false;
35
36 3
    public static function init(string $domain, string $path, ?int $expire = null, bool $secure = false, bool $httpOnly = false, bool $sameSite = false, bool $dieOnSent = false): void
37
    {
38 3
        static::$domain = $domain;
39 3
        static::$path = $path;
40 3
        static::$expire = $expire;
41 3
        static::$secure = $secure;
42 3
        static::$httpOnly = $httpOnly;
43 3
        static::$sameSite = $sameSite;
44 3
        static::$dieOnSent = $dieOnSent;
45
    }
46
47
    /**
48
     * @param string|int $offset
49
     * @return mixed
50
     */
51 1
    public final function __get($offset)
52
    {
53 1
        return $this->offsetGet($offset);
54
    }
55
56
    /**
57
     * @param string|int $offset
58
     * @param mixed|null $value
59
     * @throws InputException
60
     */
61 2
    public final function __set($offset, $value): void
62
    {
63 2
        $this->offsetSet($offset, $value);
64
    }
65
66
    /**
67
     * @param string|int $offset
68
     * @return bool
69
     */
70 1
    public final function __isset($offset): bool
71
    {
72 1
        return $this->offsetExists($offset);
73
    }
74
75
    /**
76
     * @param string|int $offset
77
     * @throws InputException
78
     */
79 2
    public final function __unset($offset): void
80
    {
81 2
        $this->offsetUnset($offset);
82
    }
83
84 1
    public final function offsetExists($offset): bool
85
    {
86 1
        $offset = $this->removeNullBytes(strval($offset));
87 1
        return isset($_COOKIE[$offset]) && ('' != $_COOKIE[$offset]);
88
    }
89
90
    #[\ReturnTypeWillChange]
91 1
    public final function offsetGet($offset)
92
    {
93 1
        return $_COOKIE[$this->removeNullBytes(strval($offset))];
94
    }
95
96
    /**
97
     * @param string|int $offset
98
     * @param mixed|null $value
99
     * @throws InputException
100
     */
101 2
    public final function offsetSet($offset, $value): void
102
    {
103 2
        $offset = $this->removeNullBytes(strval($offset));
104
        // access immediately
105 2
        $_COOKIE[$offset] = $value;
106
107
        // now permanent ones
108 2
        if (headers_sent()) {
109 2
            if (static::$dieOnSent) {
110 1
                throw new InputException('Cannot modify header information - headers already sent');
111
            }
112 1
            return;
113
        }
114
        // @codeCoverageIgnoreStart
115
        $expire = is_null(static::$expire) ? null : time() + static::$expire;
116
        // TODO: php 7.3 required for 'samesite'
117
        if (73000 < PHP_VERSION_ID) {
118
            setcookie($offset, strval($value), [
119
                'expires'  => intval($expire),
120
                'path'     => strval(static::$path),
121
                'domain'   => strval(static::$domain),
122
                'secure'   => boolval(static::$secure),
123
                'httponly' => boolval(static::$httpOnly),
124
                'samesite' => static::$sameSite ? 'Strict' : 'Lax', // not in usual config
125
            ]);
126
        } else {
127
            setcookie(
128
                $offset,
129
                strval($value),
130
                intval($expire),
131
                strval(static::$path),
132
                strval(static::$domain),
133
                boolval(static::$secure),
134
                boolval(static::$httpOnly)
135
            );
136
        }
137
    }
138
    // @codeCoverageIgnoreEnd
139
140
    /**
141
     * @param string|int $offset
142
     * @throws InputException
143
     */
144 2
    public function offsetUnset($offset): void
145
    {
146 2
        unset($_COOKIE[strval($this->removeNullBytes(strval($offset)))]); // remove immediately
147 2
        if (headers_sent()) {
148 2
            if (static::$dieOnSent) {
149 1
                throw new InputException('Cannot modify header information - headers already sent');
150
            }
151 1
            return;
152
        }
153
        // @codeCoverageIgnoreStart
154
        setcookie(strval($this->removeNullBytes(strval($offset))), '', (time() - 3600), static::$path, static::$domain);
155
    }
156
    // @codeCoverageIgnoreEnd
157
}
158