|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Auth\Store; |
|
15
|
|
|
|
|
16
|
|
|
use Valkyrja\Auth\Data\Retrieval\Contract\Retrieval; |
|
17
|
|
|
use Valkyrja\Auth\Data\Retrieval\RetrievalById; |
|
18
|
|
|
use Valkyrja\Auth\Entity\Contract\User; |
|
19
|
|
|
use Valkyrja\Auth\Exception\InvalidUserException; |
|
20
|
|
|
use Valkyrja\Auth\Store\Contract\Store as Contract; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class InMemoryStore. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Melech Mizrachi |
|
26
|
|
|
* |
|
27
|
|
|
* @template U of User |
|
28
|
|
|
* |
|
29
|
|
|
* @implements Contract<U> |
|
30
|
|
|
*/ |
|
31
|
|
|
class InMemoryStore implements Contract |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* InMemoryStore construct. |
|
35
|
|
|
* |
|
36
|
|
|
* @param U[] $users The users |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
protected array $users = [], |
|
40
|
|
|
) { |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function retrieve(Retrieval $retrieval, string $user): User|null |
|
47
|
|
|
{ |
|
48
|
|
|
$retrievalFields = $retrieval->getRetrievalFields($user); |
|
49
|
|
|
|
|
50
|
|
|
return $this->getUserViaRetrievalFields($retrievalFields); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function create(User $user): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->users[] = clone $user; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritDoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function update(User $user): void |
|
65
|
|
|
{ |
|
66
|
|
|
$existingUser = $this->retrieve(new RetrievalById($user->getIdValue()), $user::class); |
|
67
|
|
|
|
|
68
|
|
|
if ($existingUser === null) { |
|
69
|
|
|
throw new InvalidUserException('User does not exist.'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$existingUser->updateProperties($user->asStorableChangedArray()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Filter users via retrieval fields. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array<non-empty-string, int|non-empty-string|bool|null> $retrievalFields |
|
|
|
|
|
|
79
|
|
|
* @param U $user |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function filterUsers(array $retrievalFields, User $user): bool |
|
84
|
|
|
{ |
|
85
|
|
|
$match = null; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($retrievalFields as $field => $retrievalField) { |
|
88
|
|
|
/** @var mixed $value */ |
|
89
|
|
|
$value = $user->__get($field); |
|
90
|
|
|
|
|
91
|
|
|
if ($match === null) { |
|
92
|
|
|
$match = $value === $retrievalField; |
|
93
|
|
|
} else { |
|
94
|
|
|
$match = $match && $value === $retrievalField; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $match ?? false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get user via retrieval fields. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array<non-empty-string, int|non-empty-string|bool|null> $retrievalFields |
|
|
|
|
|
|
105
|
|
|
* |
|
106
|
|
|
* @return U|null |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getUserViaRetrievalFields(array $retrievalFields): User|null |
|
109
|
|
|
{ |
|
110
|
|
|
$users = $this->users; |
|
111
|
|
|
|
|
112
|
|
|
$filteredUsers = array_filter( |
|
113
|
|
|
$users, |
|
114
|
|
|
/** |
|
115
|
|
|
* @param U $user |
|
|
|
|
|
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
fn (User $user): bool => $this->filterUsers($retrievalFields, $user) |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
return $filteredUsers[0] ?? null; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|