Completed
Push — master ( 407e74...eab238 )
by Arnold
06:18
created

GlobalSession::removeCookie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\Session;
6
7
use Jasny\Session\Flash\FlashBag;
8
use Jasny\Session\Flash\FlashTrait;
9
10
/**
11
 * Wrapper round $_SESSION.
12
 */
13
class GlobalSession implements SessionInterface
14
{
15
    use FlashTrait;
16
17
    /** @var array<string,mixed> */
18
    protected array $options;
19
20
    /**
21
     * Session constructor.
22
     *
23
     * @param array<string,mixed> $options   Passed to session_start()
24
     * @param FlashBag|null       $flashBag
25
     */
26 8
    public function __construct(array $options = [], ?FlashBag $flashBag = null)
27
    {
28 8
        $this->options = $options;
29 8
        $this->flashBag = $flashBag ?? new FlashBag();
30 8
    }
31
32
    /**
33
     * Start the session.
34
     * @see session_start()
35
     */
36 3
    public function start(): void
37
    {
38 3
        session_start($this->options);
39 3
    }
40
41
    /**
42
     * Write session data and end session.
43
     * @see session_write_close()
44
     */
45 3
    public function stop(): void
46
    {
47 3
        session_write_close();
48 3
    }
49
50
    /**
51
     * Discard session array changes and finish session.
52
     * @see session_abort()
53
     *
54
     * Only a shallow clone is done to save the original data. If the session data contains objects, make sure that
55
     *   `__clone()` is overwritten, so it does a deep clone.
56
     */
57 2
    public function abort(): void
58
    {
59 2
        session_abort();
60 2
    }
61
62
63
    /**
64
     * Get the sessions status.
65
     * @see session_status()
66
     */
67 5
    public function status(): int
68
    {
69 5
        return session_status();
70
    }
71
72
73
    /**
74
     * Clear all data from the session.
75
     */
76 1
    public function clear(): void
77
    {
78 1
        $this->assertStarted();
79
80 1
        $_SESSION = [];
81 1
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86 1
    public function kill(): void
87
    {
88 1
        $this->assertStarted();
89
90 1
        $this->removeCookie(session_name());
91 1
        $_SESSION = [];
92 1
        session_destroy();
93 1
    }
94
95
    /**
96
     * Remove the session cookie.
97
     * @codeCoverageIgnore
98
     */
99
    protected function removeCookie(string $name): void
100
    {
101
        if (!(bool)ini_get("session.use_cookies")) {
102
            return;
103
        }
104
105
        $options = ['expires' => time() - 42000] + session_get_cookie_params();
106
        setcookie($name, '', $options);
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112 1
    public function rotate(?callable $copy = null): void
113
    {
114 1
        $this->assertStarted();
115
116 1
        $data = isset($copy) ? $copy($_SESSION) : [];
117
118 1
        $_SESSION = [];
119 1
        $this->regenerateId(true);
120
121 1
        $_SESSION = $data;
122 1
    }
123
124
    /**
125
     * Wrapper around `session_regenerate_id()`
126
     * @codeCoverageIgnore
127
     */
128
    protected function regenerateId(bool $delete): void
129
    {
130
        session_regenerate_id($delete);
131
    }
132
133
134
    /**
135
     * @param string $offset
136
     * @return bool
137
     */
138 1
    public function offsetExists($offset): bool
139
    {
140 1
        $this->assertStarted();
141
142 1
        return array_key_exists($offset, $_SESSION);
143
    }
144
145
    /**
146
     * @param string $offset
147
     * @return mixed
148
     */
149 1
    public function offsetGet($offset)
150
    {
151 1
        $this->assertStarted();
152
153 1
        return $_SESSION[$offset];
154
    }
155
156
    /**
157
     * @param string $offset
158
     * @param mixed  $value
159
     */
160 4
    public function offsetSet($offset, $value): void
0 ignored issues
show
introduced by
Parameter #1 $offset (string) of method Jasny\Session\GlobalSession::offsetSet() should be contravariant with parameter $offset (string|null) of method ArrayAccess<string,mixed>::offsetSet()
Loading history...
161
    {
162 4
        $this->assertStarted();
163
164 3
        $_SESSION[$offset] = $value;
165 3
    }
166
167
    /**
168
     * @param string $offset
169
     */
170 1
    public function offsetUnset($offset): void
171
    {
172 1
        $this->assertStarted();
173
174 1
        unset($_SESSION[$offset]);
175 1
    }
176
177
178
    /**
179
     * Assert that there is an active session.
180
     *
181
     * @throws NoSessionException
182
     */
183 7
    protected function assertStarted(): void
184
    {
185 7
        if (session_status() !== \PHP_SESSION_ACTIVE) {
186 1
            throw new NoSessionException("Session not started");
187
        }
188 6
    }
189
}
190