Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
18.97% |
11 / 58 |
|
18.75% |
3 / 16 |
CRAP | |
0.00% |
0 / 1 |
SessionData | |
18.97% |
11 / 58 |
|
18.75% |
3 / 16 |
686.85 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSession | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
writeData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setGroup | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
42 | |||
set | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
get | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
getEntity | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
setEntityClass | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
remove | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
clearGroup | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
clear | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
restart | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
has | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
isEmpty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
convertValueToScalar | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace BO\Slim\Middleware\Session; |
4 | |
5 | use Psr\Http\Message\ServerRequestInterface as Request; |
6 | |
7 | class SessionData implements SessionInterface |
8 | { |
9 | /** |
10 | * @var array |
11 | */ |
12 | protected $data; |
13 | |
14 | /** |
15 | * @var boolean |
16 | */ |
17 | private $isLocked = false; |
18 | |
19 | |
20 | protected $entityClass = null; |
21 | |
22 | /** |
23 | * __construct is not allowed, use |
24 | * - {@see SessionData::getSessionFromName} |
25 | * instead |
26 | */ |
27 | public function __construct($data = null) |
28 | { |
29 | $this->data = $data; |
30 | } |
31 | |
32 | /** |
33 | * |
34 | * @SuppressWarnings(Superglobals) |
35 | * @SuppressWarnings(Unused) |
36 | * |
37 | * @return self |
38 | */ |
39 | public static function getSession(Request $request) |
40 | { |
41 | $session = array(); |
42 | if (headers_sent() === false && session_status() !== PHP_SESSION_ACTIVE) { |
43 | session_start(); |
44 | if (!count($_SESSION)) { |
45 | $_SESSION['status'] = 'start'; |
46 | }; |
47 | $session = $_SESSION; |
48 | } else { |
49 | throw new \BO\Slim\Exception\SessionFailed("Headers sent or a session is already active"); |
50 | } |
51 | |
52 | $instance = new self(); |
53 | $instance->data = $session; |
54 | return $instance; |
55 | } |
56 | |
57 | public function writeData() |
58 | { |
59 | session_write_close(); |
60 | $this->isLocked = true; |
61 | } |
62 | |
63 | public function setGroup(array $group, $clear) |
64 | { |
65 | foreach ($group as $index => $items) { |
66 | if ($clear) { |
67 | $this->clearGroup($index); |
68 | } |
69 | if (is_array($items) && 0 < count($items)) { |
70 | foreach ($items as $key => $value) { |
71 | $this->set($key, $value, $index); |
72 | } |
73 | } |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * |
79 | * @SuppressWarnings(Superglobals) |
80 | * |
81 | * @return array |
82 | */ |
83 | public function set($key, $value, $groupIndex = null) |
84 | { |
85 | if (null === $groupIndex) { |
86 | $this->data[$key] = self::convertValueToScalar($value); |
87 | } else { |
88 | $this->data[$groupIndex][$key] = self::convertValueToScalar($value); |
89 | } |
90 | if ($this->isLocked) { |
91 | throw new \BO\Slim\Exception\SessionLocked(); |
92 | } |
93 | $_SESSION = $this->data; |
94 | } |
95 | |
96 | public function get($key, $groupIndex = null, $default = null) |
97 | { |
98 | if (! $this->has($key, $groupIndex)) { |
99 | return self::convertValueToScalar($default); |
100 | } elseif (null === $groupIndex) { |
101 | return $this->data[$key]; |
102 | } else { |
103 | return $this->data[$groupIndex][$key]; |
104 | } |
105 | } |
106 | |
107 | public function getEntity() |
108 | { |
109 | if (null === $this->entityClass) { |
110 | throw new \Exception("Entity-Class not set"); |
111 | } |
112 | $sessionContent = clone $this->entityClass; |
113 | $sessionContent->content = $this->data; |
114 | return $sessionContent; |
115 | } |
116 | |
117 | public function setEntityClass($entityClass) |
118 | { |
119 | $this->entityClass = $entityClass; |
120 | return $this; |
121 | } |
122 | |
123 | public function remove($key, $groupIndex = null) |
124 | { |
125 | if (null === $groupIndex) { |
126 | unset($this->data[$key]); |
127 | } else { |
128 | unset($this->data[$groupIndex][$key]); |
129 | } |
130 | } |
131 | |
132 | /** |
133 | * |
134 | * @SuppressWarnings(Superglobals) |
135 | * |
136 | * @return self |
137 | */ |
138 | public function clearGroup($groupIndex = null) |
139 | { |
140 | if (null !== $groupIndex) { |
141 | $this->data[$groupIndex] = []; |
142 | $_SESSION = $this->data; |
143 | } |
144 | } |
145 | |
146 | /** |
147 | * |
148 | * @SuppressWarnings(Superglobals) |
149 | * |
150 | * @return self |
151 | */ |
152 | public function clear() |
153 | { |
154 | if (session_status() === PHP_SESSION_ACTIVE) { |
155 | setcookie(session_name(), '', time() - 3600, '/'); |
156 | $_SESSION = array(); |
157 | session_destroy(); |
158 | } |
159 | } |
160 | |
161 | /** |
162 | * |
163 | * @SuppressWarnings(Superglobals) |
164 | * |
165 | * @return self |
166 | */ |
167 | public function restart() |
168 | { |
169 | if (session_status() === PHP_SESSION_ACTIVE) { |
170 | session_regenerate_id(true); |
171 | $_SESSION = array(); |
172 | $this->data = $_SESSION; |
173 | } |
174 | } |
175 | |
176 | public function has($key, $groupIndex = null) |
177 | { |
178 | if (null === $groupIndex) { |
179 | return array_key_exists($key, $this->data); |
180 | } else { |
181 | if (array_key_exists($groupIndex, $this->data)) { |
182 | return array_key_exists($key, $this->data[$groupIndex]); |
183 | } |
184 | } |
185 | } |
186 | |
187 | public function isEmpty() |
188 | { |
189 | return empty($this->data); |
190 | } |
191 | |
192 | // TODO: Review the use of `mixed` return type. |
193 | // This method delegates to getSession()->jsonSerialize(), which may return various types. |
194 | // Consider adding stricter return typing if getSession() can be more precisely typed. |
195 | public function jsonSerialize(): mixed |
196 | { |
197 | return json_encode($this->data); |
198 | } |
199 | |
200 | private static function convertValueToScalar($value) |
201 | { |
202 | return json_decode(json_encode($value), true); |
203 | } |
204 | } |