|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stu\Lib\Session; |
|
4
|
|
|
|
|
5
|
|
|
use Override; |
|
|
|
|
|
|
6
|
|
|
use Stu\Exception\SessionInvalidException; |
|
7
|
|
|
use Stu\Orm\Entity\User; |
|
8
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class SessionStorage implements SessionStorageInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array<array<mixed>> */ |
|
13
|
|
|
private array $sessionDataPerUser = []; |
|
14
|
|
|
|
|
15
|
1 |
|
public function __construct( |
|
16
|
|
|
private readonly UserRepositoryInterface $userRepository, |
|
17
|
|
|
private readonly SessionInterface $session |
|
18
|
1 |
|
) {} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @api |
|
22
|
|
|
*/ |
|
23
|
|
|
#[Override] |
|
24
|
|
|
public function storeSessionData(string|int $key, mixed $value, bool $isSingleValue = false): void |
|
25
|
|
|
{ |
|
26
|
|
|
$stored = false; |
|
27
|
|
|
$user = $this->getUserMandatory(); |
|
28
|
|
|
|
|
29
|
|
|
$data = $this->getSessionDataUnserialized($user); |
|
30
|
|
|
if (!array_key_exists($key, $data)) { |
|
31
|
|
|
if ($isSingleValue) { |
|
32
|
|
|
$data[$key] = $value; |
|
33
|
|
|
$stored = true; |
|
34
|
|
|
} else { |
|
35
|
|
|
$data[$key] = []; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
if (!$isSingleValue && !array_key_exists($value, $data[$key])) { |
|
39
|
|
|
$data[$key][$value] = 1; |
|
40
|
|
|
$stored = true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($stored) { |
|
44
|
|
|
unset($this->sessionDataPerUser[$user->getId()]); |
|
45
|
|
|
$user->setSessionData(serialize($data)); |
|
46
|
|
|
$this->userRepository->save($user); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @return array<mixed> */ |
|
51
|
3 |
|
private function getSessionDataUnserialized(User $user): array |
|
52
|
|
|
{ |
|
53
|
3 |
|
if (!array_key_exists($user->getId(), $this->sessionDataPerUser)) { |
|
54
|
1 |
|
$sessiondataUnserialized = unserialize($user->getSessionData()); |
|
55
|
1 |
|
if (!is_array($sessiondataUnserialized)) { |
|
56
|
1 |
|
$sessiondataUnserialized = []; |
|
57
|
|
|
} |
|
58
|
1 |
|
$this->sessionDataPerUser[$user->getId()] = $sessiondataUnserialized; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
return $this->sessionDataPerUser[$user->getId()]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @api |
|
66
|
|
|
*/ |
|
67
|
1 |
|
#[Override] |
|
68
|
|
|
public function deleteSessionData(string $key, mixed $value = null): void |
|
69
|
|
|
{ |
|
70
|
1 |
|
$user = $this->getUserMandatory(); |
|
71
|
|
|
|
|
72
|
1 |
|
$data = $this->getSessionDataUnserialized($user); |
|
73
|
1 |
|
if (!array_key_exists($key, $data)) { |
|
74
|
1 |
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
if ($value === null) { |
|
77
|
|
|
unset($data[$key]); |
|
78
|
|
|
} else { |
|
79
|
|
|
if (!array_key_exists($value, $data[$key])) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
unset($data[$key][$value]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
unset($this->sessionDataPerUser[$user->getId()]); |
|
86
|
|
|
$user->setSessionData(serialize($data)); |
|
87
|
|
|
$this->userRepository->save($user); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @api |
|
92
|
|
|
*/ |
|
93
|
2 |
|
#[Override] |
|
94
|
|
|
public function hasSessionValue(string $key, mixed $value): bool |
|
95
|
|
|
{ |
|
96
|
2 |
|
$data = $this->getSessionDataUnserialized($this->getUserMandatory()); |
|
97
|
2 |
|
if (!array_key_exists($key, $data)) { |
|
98
|
2 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
return array_key_exists($value, $data[$key]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @api |
|
105
|
|
|
*/ |
|
106
|
|
|
#[Override] |
|
107
|
|
|
public function getSessionValue(string $key): mixed |
|
108
|
|
|
{ |
|
109
|
|
|
$data = $this->getSessionDataUnserialized($this->getUserMandatory()); |
|
110
|
|
|
if (!array_key_exists($key, $data)) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
return $data[$key]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
private function getUserMandatory(): User |
|
117
|
|
|
{ |
|
118
|
3 |
|
$user = $this->session->getUser(); |
|
119
|
3 |
|
if ($user === null) { |
|
120
|
|
|
throw new SessionInvalidException("No user logged in"); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
return $user; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
#[Override] |
|
127
|
|
|
public function reset(): void |
|
128
|
|
|
{ |
|
129
|
|
|
$this->sessionDataPerUser = []; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
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