|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Lib\Session; |
|
4
|
|
|
|
|
5
|
|
|
use Override; |
|
|
|
|
|
|
6
|
|
|
use Stu\Exception\SessionInvalidException; |
|
7
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
8
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class SessionStorage implements SessionStorageInterface |
|
11
|
|
|
{ |
|
12
|
1 |
|
public function __construct( |
|
13
|
|
|
private readonly UserRepositoryInterface $userRepository, |
|
14
|
|
|
private readonly SessionInterface $session |
|
15
|
1 |
|
) {} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @api |
|
19
|
|
|
*/ |
|
20
|
|
|
#[Override] |
|
21
|
|
|
public function storeSessionData(string|int $key, mixed $value, bool $isSingleValue = false): void |
|
22
|
|
|
{ |
|
23
|
|
|
$stored = false; |
|
24
|
|
|
$user = $this->getUserMandatory(); |
|
25
|
|
|
|
|
26
|
|
|
$data = $user->getSessionDataUnserialized(); |
|
27
|
|
|
if (!array_key_exists($key, $data)) { |
|
28
|
|
|
if ($isSingleValue) { |
|
29
|
|
|
$data[$key] = $value; |
|
30
|
|
|
$stored = true; |
|
31
|
|
|
} else { |
|
32
|
|
|
$data[$key] = []; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
if (!$isSingleValue && !array_key_exists($value, $data[$key])) { |
|
36
|
|
|
$data[$key][$value] = 1; |
|
37
|
|
|
$stored = true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($stored) { |
|
41
|
|
|
$user->setSessionData(serialize($data)); |
|
42
|
|
|
$this->userRepository->save($user); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @api |
|
48
|
|
|
*/ |
|
49
|
1 |
|
#[Override] |
|
50
|
|
|
public function deleteSessionData(string $key, mixed $value = null): void |
|
51
|
|
|
{ |
|
52
|
1 |
|
$user = $this->getUserMandatory(); |
|
53
|
|
|
|
|
54
|
1 |
|
$data = $user->getSessionDataUnserialized(); |
|
55
|
1 |
|
if (!array_key_exists($key, $data)) { |
|
56
|
1 |
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
if ($value === null) { |
|
59
|
|
|
unset($data[$key]); |
|
60
|
|
|
} else { |
|
61
|
|
|
if (!array_key_exists($value, $data[$key])) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
unset($data[$key][$value]); |
|
65
|
|
|
} |
|
66
|
|
|
$user->setSessionData(serialize($data)); |
|
67
|
|
|
$this->userRepository->save($user); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @api |
|
72
|
|
|
*/ |
|
73
|
2 |
|
#[Override] |
|
74
|
|
|
public function hasSessionValue(string $key, mixed $value): bool |
|
75
|
|
|
{ |
|
76
|
2 |
|
$data = $this->getUserMandatory()->getSessionDataUnserialized(); |
|
77
|
2 |
|
if (!array_key_exists($key, $data)) { |
|
78
|
2 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
return array_key_exists($value, $data[$key]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @api |
|
85
|
|
|
*/ |
|
86
|
|
|
#[Override] |
|
87
|
|
|
public function getSessionValue(string $key): mixed |
|
88
|
|
|
{ |
|
89
|
|
|
$data = $this->getUserMandatory()->getSessionDataUnserialized(); |
|
90
|
|
|
if (!array_key_exists($key, $data)) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
return $data[$key]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
private function getUserMandatory(): UserInterface |
|
97
|
|
|
{ |
|
98
|
3 |
|
$user = $this->session->getUser(); |
|
99
|
3 |
|
if ($user === null) { |
|
100
|
|
|
throw new SessionInvalidException("No user logged in"); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
return $user; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths