Completed
Branch 2.x (b1c655)
by Julián
07:53
created

Session::regenerate()   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
namespace Jgut\Middleware\Sessionware;
13
14
use Jgut\Middleware\Sessionware\Manager\Manager;
15
use Jgut\Middleware\Sessionware\Manager\Native;
16
use League\Event\EmitterAwareInterface;
17
use League\Event\EmitterTrait;
18
use League\Event\Event;
19
20
/**
21
 * Session helper.
22
 */
23
class Session implements EmitterAwareInterface
24
{
25
    use EmitterTrait;
26
27
    /**
28
     * @var Native
29
     */
30
    protected $sessionManager;
31
32
    /**
33
     * Session data.
34
     *
35
     * @var array
36
     */
37
    protected $data = [];
38
39
    /**
40
     * Session constructor.
41
     *
42
     * @param Manager $sessionManager
43
     */
44
    public function __construct(Manager $sessionManager)
45
    {
46
        $this->sessionManager = $sessionManager;
0 ignored issues
show
Documentation Bug introduced by
$sessionManager is of type object<Jgut\Middleware\S...onware\Manager\Manager>, but the property $sessionManager was declared to be of type object<Jgut\Middleware\S...ionware\Manager\Native>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
47
    }
48
49
    /**
50
     * Start session.
51
     *
52
     * @throws \RuntimeException
53
     */
54
    public function start()
55
    {
56
        if ($this->isActive()) {
57
            return;
58
        }
59
60
        $this->sessionManager->sessionStart();
61
62
        $this->data = $this->sessionManager->loadSessionData();
63
64
        if ($this->sessionManager->shouldRegenerate()) {
65
            $this->regenerate();
66
        }
67
68
        $this->manageTimeout();
69
    }
70
71
    /**
72
     * Close session.
73
     *
74
     * @SuppressWarnings(PMD.Superglobals)
75
     */
76
    public function close()
77
    {
78
        if (!$this->isActive()) {
79
            return;
80
        }
81
82
        $this->sessionManager->sessionEnd($this->data);
83
    }
84
85
    /**
86
     * Regenerate session keeping parameters.
87
     *
88
     * @throws \RuntimeException
89
     *
90
     * @SuppressWarnings(PMD.Superglobals)
91
     */
92
    public function regenerate()
93
    {
94
        if (!$this->isActive()) {
95
            throw new \RuntimeException('Cannot regenerate a not started session');
96
        }
97
98
        $this->sessionManager->sessionReset();
99
    }
100
101
    /**
102
     * Is there an active session.
103
     *
104
     * @return bool
105
     */
106
    public function isActive()
107
    {
108
        return $this->sessionManager->isSessionStarted();
109
    }
110
111
    /**
112
     * Get session identifier.
113
     *
114
     * @return string|null
115
     */
116
    public function getId()
117
    {
118
        return $this->sessionManager->getSessionId();
119
    }
120
121
    /**
122
     * Session parameter existence.
123
     *
124
     * @param string $key
125
     *
126
     * @return bool
127
     */
128
    public function has($key)
129
    {
130
        return array_key_exists($key, $this->data);
131
    }
132
133
    /**
134
     * Retrieve session parameter.
135
     *
136
     * @param string     $key
137
     * @param mixed|null $default
138
     *
139
     * @return mixed
140
     */
141
    public function get($key, $default = null)
142
    {
143
        return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
144
    }
145
146
    /**
147
     * Set session parameter.
148
     *
149
     * @param string $key
150
     * @param mixed  $value
151
     *
152
     * @return static
153
     */
154
    public function set($key, $value)
155
    {
156
        $this->data[$key] = $value;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Remove session parameter.
163
     *
164
     * @param string $key
165
     *
166
     * @return static
167
     */
168
    public function remove($key)
169
    {
170
        if (array_key_exists($key, $this->data)) {
171
            unset($this->data[$key]);
172
        }
173
174
        return $this;
175
    }
176
177
    /**
178
     * Remove all session parameters.
179
     *
180
     * @return static
181
     */
182
    public function clear()
183
    {
184
        $this->data = [];
185
186
        return $this;
187
    }
188
189
    /**
190
     * Manage session timeout.
191
     *
192
     * @throws \RuntimeException
193
     */
194
    protected function manageTimeout()
195
    {
196
        $timeoutKey = $this->getConfiguration()->getTimeoutKey();
197
198
        if (array_key_exists($timeoutKey, $this->data) && $this->data[$timeoutKey] < time()) {
199
            $this->emit(Event::named('pre.session_timeout'), session_id(), $this);
200
201
            $this->sessionManager->sessionReset();
202
203
            $this->emit(Event::named('post.session_timeout'), session_id(), $this);
204
        }
205
206
        $this->data[$timeoutKey] = time() + $this->getConfiguration()->getLifetime();
207
    }
208
209
    /**
210
     * Get session configuration.
211
     *
212
     * @return Configuration
213
     */
214
    protected function getConfiguration()
215
    {
216
        return $this->sessionManager->getConfiguration();
217
    }
218
}
219