Completed
Branch 2.x (525e63)
by Julián
09:21
created

Session::regenerateId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * sessionware (https://github.com/juliangut/sessionware).
5
 * PSR7 session management middleware.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/sessionware
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Jgut\Middleware\Sessionware;
15
16
use Jgut\Middleware\Sessionware\Manager\Manager;
17
use League\Event\EmitterAwareInterface;
18
use League\Event\EmitterTrait;
19
use League\Event\Event;
20
21
/**
22
 * Session helper.
23
 */
24
class Session implements EmitterAwareInterface
25
{
26
    use EmitterTrait;
27
28
    /**
29
     * Session manager.
30
     *
31
     * @var Manager
32
     */
33
    protected $sessionManager;
34
35
    /**
36
     * Session data.
37
     *
38
     * @var array
39
     */
40
    protected $data = [];
41
42
    /**
43
     * Session constructor.
44
     *
45
     * @param Manager $sessionManager
46
     */
47
    public function __construct(Manager $sessionManager)
48
    {
49
        $this->sessionManager = $sessionManager;
50
    }
51
52
    /**
53
     * Start session.
54
     *
55
     * @throws \RuntimeException
56
     */
57
    public function start()
58
    {
59
        if ($this->isActive()) {
60
            return;
61
        }
62
63
        $this->data = $this->sessionManager->sessionStart();
64
65
        if ($this->sessionManager->shouldRegenerateId()) {
66
            $this->regenerateId();
67
        }
68
69
        $this->manageTimeout();
70
    }
71
72
    /**
73
     * Regenerate session identifier keeping parameters.
74
     *
75
     * @throws \RuntimeException
76
     *
77
     * @SuppressWarnings(PMD.Superglobals)
78
     */
79
    public function regenerateId()
80
    {
81
        if (!$this->isActive()) {
82
            throw new \RuntimeException('Cannot regenerate a not started session');
83
        }
84
85
        $this->sessionManager->sessionRegenerateId();
86
    }
87
88
    /**
89
     * Close session.
90
     *
91
     * @SuppressWarnings(PMD.Superglobals)
92
     */
93
    public function close()
94
    {
95
        if (!$this->isActive()) {
96
            return;
97
        }
98
99
        $this->sessionManager->sessionEnd($this->data);
100
    }
101
102
    /**
103
     * Destroy session.
104
     *
105
     * @throws \RuntimeException
106
     */
107
    public function destroy()
108
    {
109
        if (!$this->isActive()) {
110
            throw new \RuntimeException('Cannot destroy a not started session');
111
        }
112
113
        $this->sessionManager->sessionDestroy();
114
115
        $this->data = [];
116
    }
117
118
    /**
119
     * Is there an active session.
120
     *
121
     * @return bool
122
     */
123
    public function isActive() : bool
124
    {
125
        return $this->sessionManager->isSessionStarted();
126
    }
127
128
    /**
129
     * Get session identifier.
130
     *
131
     * @return string
132
     */
133
    public function getId() : string
134
    {
135
        return $this->sessionManager->getSessionId();
136
    }
137
138
    /**
139
     * Session parameter existence.
140
     *
141
     * @param string $key
142
     *
143
     * @return bool
144
     */
145
    public function has(string $key) : bool
146
    {
147
        return array_key_exists($key, $this->data);
148
    }
149
150
    /**
151
     * Retrieve session parameter.
152
     *
153
     * @param string     $key
154
     * @param mixed|null $default
155
     *
156
     * @return mixed
157
     */
158
    public function get(string $key, $default = null)
159
    {
160
        return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
161
    }
162
163
    /**
164
     * Set session parameter.
165
     *
166
     * @param string $key
167
     * @param mixed  $value
168
     *
169
     * @return static
170
     */
171
    public function set(string $key, $value) : self
172
    {
173
        $this->data[$key] = $value;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Remove session parameter.
180
     *
181
     * @param string $key
182
     *
183
     * @return static
184
     */
185
    public function remove(string $key) : self
186
    {
187
        if (array_key_exists($key, $this->data)) {
188
            unset($this->data[$key]);
189
        }
190
191
        return $this;
192
    }
193
194
    /**
195
     * Remove all session parameters.
196
     *
197
     * @return static
198
     */
199
    public function clear()
200
    {
201
        $this->data = [];
202
203
        $this->setTimeout();
204
205
        return $this;
206
    }
207
208
    /**
209
     * Get session timeout time.
210
     *
211
     * @return int
212
     */
213
    public function getTimeout() : int
214
    {
215
        return $this->data[$this->getConfiguration()->getTimeoutKey()];
216
    }
217
218
    /**
219
     * Set session timeout time.
220
     *
221
     * @return static
222
     */
223
    protected function setTimeout() : self
224
    {
225
        $this->data[$this->getConfiguration()->getTimeoutKey()] = time() + $this->getConfiguration()->getLifetime();
226
227
        return $this;
228
    }
229
230
    /**
231
     * Manage session timeout.
232
     *
233
     * @throws \RuntimeException
234
     */
235
    protected function manageTimeout()
236
    {
237
        $timeoutKey = $this->getConfiguration()->getTimeoutKey();
238
239
        if (array_key_exists($timeoutKey, $this->data) && $this->data[$timeoutKey] < time()) {
240
            $this->emit(Event::named('pre.session_timeout'), session_id(), $this);
241
242
            $this->sessionManager->sessionRegenerateId();
243
244
            $this->emit(Event::named('post.session_timeout'), session_id(), $this);
245
        }
246
247
        $this->setTimeout();
248
    }
249
250
    /**
251
     * Get session configuration.
252
     *
253
     * @return Configuration
254
     */
255
    protected function getConfiguration() : Configuration
256
    {
257
        return $this->sessionManager->getConfiguration();
258
    }
259
}
260