|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProyectoTAU\TAU\Common; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
|
6
|
|
|
use ProyectoTAU\TAU\Module\Administration\User\Domain\User; |
|
7
|
|
|
use ProyectoTAU\TAU\Module\Administration\Group\Domain\Group; |
|
8
|
|
|
use ProyectoTAU\TAU\Module\Administration\Role\Domain\Role; |
|
9
|
|
|
use ProyectoTAU\TAU\Module\Administration\Module\Domain\Module; |
|
10
|
|
|
|
|
11
|
|
|
/* |
|
12
|
|
|
* @see https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
// General singleton class. |
|
16
|
|
|
class InMemoryRepository implements Repository |
|
17
|
|
|
{ |
|
18
|
|
|
// Hold the class instance. |
|
19
|
|
|
private static ?Repository $instance = null; |
|
20
|
|
|
|
|
21
|
|
|
// Tables |
|
22
|
|
|
private $userDataStore = []; |
|
23
|
|
|
private $groupDataStore = []; |
|
24
|
|
|
private $roleDataStore = []; |
|
25
|
|
|
private $moduleDataStore = []; |
|
26
|
|
|
|
|
27
|
|
|
// Relations |
|
28
|
|
|
private $user_group = []; |
|
29
|
|
|
private $group_role = []; |
|
30
|
|
|
private $role_module = []; |
|
31
|
|
|
|
|
32
|
|
|
// The constructor is private |
|
33
|
|
|
// to prevent initiation with outer code. |
|
34
|
|
|
private function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
// The expensive process (e.g.,db connection) goes here. |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// The object is created from within the class itself |
|
40
|
|
|
// only if the class has no instance. |
|
41
|
71 |
|
public static function getInstance(): Repository |
|
42
|
|
|
{ |
|
43
|
71 |
|
if (self::$instance == null) |
|
44
|
|
|
{ |
|
45
|
|
|
self::$instance = new static(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
71 |
|
return self::$instance; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/* |
|
52
|
|
|
* Transactions |
|
53
|
|
|
*/ |
|
54
|
31 |
|
public function begin(): void |
|
55
|
|
|
{ |
|
56
|
31 |
|
} |
|
57
|
|
|
|
|
58
|
31 |
|
public function commit(): void |
|
59
|
|
|
{ |
|
60
|
31 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function rollBack(): void |
|
63
|
|
|
{ |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
33 |
|
public function clearUser(): void |
|
67
|
|
|
{ |
|
68
|
33 |
|
$this->userDataStore = []; |
|
69
|
33 |
|
$this->user_group = []; |
|
70
|
33 |
|
} |
|
71
|
|
|
|
|
72
|
46 |
|
public function clearGroup(): void |
|
73
|
|
|
{ |
|
74
|
46 |
|
$this->groupDataStore = []; |
|
75
|
46 |
|
$this->user_group = []; |
|
76
|
46 |
|
$this->group_role = []; |
|
77
|
46 |
|
} |
|
78
|
|
|
|
|
79
|
40 |
|
public function clearRole(): void |
|
80
|
|
|
{ |
|
81
|
40 |
|
$this->roleDataStore = []; |
|
82
|
40 |
|
$this->group_role = []; |
|
83
|
40 |
|
$this->role_module = []; |
|
84
|
40 |
|
} |
|
85
|
|
|
|
|
86
|
24 |
|
public function clearModule(): void |
|
87
|
|
|
{ |
|
88
|
24 |
|
$this->moduleDataStore = []; |
|
89
|
24 |
|
$this->role_module = []; |
|
90
|
24 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* |
|
93
|
|
|
* User |
|
94
|
|
|
*/ |
|
95
|
|
|
|
|
96
|
26 |
|
public function createUser(User $user): void |
|
97
|
|
|
{ |
|
98
|
26 |
|
$this->userDataStore[$user->getId()] = $user; |
|
99
|
26 |
|
} |
|
100
|
|
|
|
|
101
|
18 |
|
public function readUser($id): User |
|
102
|
|
|
{ |
|
103
|
18 |
|
$this->failIfNotExists('User', $id); |
|
104
|
|
|
|
|
105
|
17 |
|
return $this->userDataStore[$id]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
public function updateUser($id, $name, $surname, $login): void |
|
109
|
|
|
{ |
|
110
|
2 |
|
$this->failIfNotExists('User', $id); |
|
111
|
|
|
|
|
112
|
2 |
|
$this->userDataStore[$id]->setName($name); |
|
113
|
2 |
|
$this->userDataStore[$id]->setSurname($surname); |
|
114
|
2 |
|
$this->userDataStore[$id]->setLogin($login); |
|
115
|
2 |
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
public function deleteUser($id): void |
|
118
|
|
|
{ |
|
119
|
2 |
|
$this->failIfNotExists('User', $id); |
|
120
|
|
|
|
|
121
|
2 |
|
unset($this->userDataStore[$id]); |
|
122
|
2 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/* |
|
125
|
|
|
* Group |
|
126
|
|
|
*/ |
|
127
|
|
|
|
|
128
|
38 |
|
public function createGroup(Group $group): void |
|
129
|
|
|
{ |
|
130
|
38 |
|
$this->groupDataStore[$group->getId()] = $group; |
|
131
|
38 |
|
} |
|
132
|
|
|
|
|
133
|
31 |
|
public function readGroup($id): Group |
|
134
|
|
|
{ |
|
135
|
31 |
|
$this->failIfNotExists('Group', $id); |
|
136
|
|
|
|
|
137
|
30 |
|
return $this->groupDataStore[$id]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
public function updateGroup($id, $name, $desc): void |
|
141
|
|
|
{ |
|
142
|
2 |
|
$this->failIfNotExists('Group', $id); |
|
143
|
|
|
|
|
144
|
2 |
|
$this->groupDataStore[$id]->setName($name); |
|
145
|
2 |
|
$this->groupDataStore[$id]->setDesc($desc); |
|
146
|
2 |
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
public function deleteGroup($id): void |
|
149
|
|
|
{ |
|
150
|
2 |
|
$this->failIfNotExists('Group', $id); |
|
151
|
|
|
|
|
152
|
2 |
|
unset($this->groupDataStore[$id]); |
|
153
|
2 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/* |
|
156
|
|
|
* Role |
|
157
|
|
|
*/ |
|
158
|
|
|
|
|
159
|
29 |
|
public function createRole(Role $role): void |
|
160
|
|
|
{ |
|
161
|
29 |
|
$this->roleDataStore[$role->getId()] = $role; |
|
162
|
29 |
|
} |
|
163
|
|
|
|
|
164
|
24 |
|
public function readRole($id): Role |
|
165
|
|
|
{ |
|
166
|
24 |
|
$this->failIfNotExists('Role', $id); |
|
167
|
|
|
|
|
168
|
23 |
|
return $this->roleDataStore[$id]; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
2 |
|
public function updateRole($id, $name, $desc): void |
|
172
|
|
|
{ |
|
173
|
2 |
|
$this->failIfNotExists('Role', $id); |
|
174
|
|
|
|
|
175
|
2 |
|
$this->roleDataStore[$id]->setName($name); |
|
176
|
2 |
|
$this->roleDataStore[$id]->setDesc($desc); |
|
177
|
2 |
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
public function deleteRole($id): void |
|
180
|
|
|
{ |
|
181
|
2 |
|
$this->failIfNotExists('Role', $id); |
|
182
|
|
|
|
|
183
|
2 |
|
unset($this->roleDataStore[$id]); |
|
184
|
2 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/* |
|
187
|
|
|
* Module |
|
188
|
|
|
*/ |
|
189
|
|
|
|
|
190
|
17 |
|
public function createModule(Module $module): void |
|
191
|
|
|
{ |
|
192
|
17 |
|
$this->moduleDataStore[$module->getId()] = $module; |
|
193
|
17 |
|
} |
|
194
|
|
|
|
|
195
|
13 |
|
public function readModule($id): Module |
|
196
|
|
|
{ |
|
197
|
13 |
|
$this->failIfNotExists('Module', $id); |
|
198
|
|
|
|
|
199
|
12 |
|
return $this->moduleDataStore[$id]; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
2 |
|
public function updateModule($id, $name, $desc): void |
|
203
|
|
|
{ |
|
204
|
2 |
|
$this->failIfNotExists('Module', $id); |
|
205
|
|
|
|
|
206
|
2 |
|
$this->moduleDataStore[$id]->setName($name); |
|
207
|
2 |
|
$this->moduleDataStore[$id]->setDesc($desc); |
|
208
|
2 |
|
} |
|
209
|
|
|
|
|
210
|
2 |
|
public function deleteModule($id): void |
|
211
|
|
|
{ |
|
212
|
2 |
|
unset($this->moduleDataStore[$id]); |
|
213
|
2 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/* |
|
216
|
|
|
* Relations |
|
217
|
|
|
*/ |
|
218
|
|
|
|
|
219
|
11 |
|
public function addUserToGroup(User $user, Group $group) |
|
220
|
|
|
{ |
|
221
|
11 |
|
$this->user_group[$user->getId()][$group->getId()] = [$user, $group]; |
|
222
|
11 |
|
} |
|
223
|
|
|
|
|
224
|
2 |
|
public function removeUserFromGroup(User $user, Group $group) |
|
225
|
|
|
{ |
|
226
|
2 |
|
unset($this->user_group[$user->getId()][$group->getId()]); |
|
227
|
2 |
|
} |
|
228
|
|
|
|
|
229
|
3 |
|
public function addGroupToUser(Group $group, User $user) |
|
230
|
|
|
{ |
|
231
|
3 |
|
$this->user_group[$user->getId()][$group->getId()] = [$user, $group]; |
|
232
|
3 |
|
} |
|
233
|
|
|
|
|
234
|
2 |
|
public function removeGroupFromUser(Group $group, User $user) |
|
235
|
|
|
{ |
|
236
|
2 |
|
unset($this->user_group[$user->getId()][$group->getId()]); |
|
237
|
2 |
|
} |
|
238
|
|
|
|
|
239
|
4 |
|
public function getGroupsFromUser(User $user): array |
|
240
|
|
|
{ |
|
241
|
4 |
|
return $this->extractY($this->user_group, 'belongsto', $this->groupDataStore, $user); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
10 |
|
public function getUsersFromGroup(Group $group): array |
|
245
|
|
|
{ |
|
246
|
10 |
|
return $this->extractX($this->user_group, 'members', $this->userDataStore, $group); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
6 |
|
public function addGroupToRole(Group $group, Role $role) |
|
250
|
|
|
{ |
|
251
|
6 |
|
$this->group_role[$group->getId()][$role->getId()] = [$group, $role]; |
|
252
|
6 |
|
} |
|
253
|
|
|
|
|
254
|
2 |
|
public function removeGroupFromRole(Group $group, Role $role) |
|
255
|
|
|
{ |
|
256
|
2 |
|
unset($this->group_role[$group->getId()][$role->getId()]); |
|
257
|
2 |
|
} |
|
258
|
|
|
|
|
259
|
6 |
|
public function addRoleToGroup(Role $role, Group $group) |
|
260
|
|
|
{ |
|
261
|
6 |
|
$this->group_role[$group->getId()][$role->getId()] = [$group, $role]; |
|
262
|
6 |
|
} |
|
263
|
|
|
|
|
264
|
2 |
|
public function removeRoleFromGroup(Role $role, Group $group) |
|
265
|
|
|
{ |
|
266
|
2 |
|
unset($this->group_role[$group->getId()][$role->getId()]); |
|
267
|
2 |
|
} |
|
268
|
|
|
|
|
269
|
4 |
|
public function getRolesFromGroup(Group $group): array |
|
270
|
|
|
{ |
|
271
|
4 |
|
return $this->extractY($this->group_role, 'plays', $this->roleDataStore, $group); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
4 |
|
public function getGroupsFromRole(Role $role): array |
|
275
|
|
|
{ |
|
276
|
4 |
|
return $this->extractX($this->group_role, 'grantedby', $this->groupDataStore, $role); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
6 |
|
public function addRoleToModule(Role $role, Module $module) |
|
280
|
|
|
{ |
|
281
|
6 |
|
$this->role_module[$role->getId()][$module->getId()] = [$role, $module]; |
|
282
|
6 |
|
} |
|
283
|
|
|
|
|
284
|
2 |
|
public function removeRoleFromModule(Role $role, Module $module) |
|
285
|
|
|
{ |
|
286
|
2 |
|
unset($this->role_module[$role->getId()][$module->getId()]); |
|
287
|
2 |
|
} |
|
288
|
|
|
|
|
289
|
3 |
|
public function addModuleToRole(Module $module, Role $role) |
|
290
|
|
|
{ |
|
291
|
3 |
|
$this->role_module[$role->getId()][$module->getId()] = [$role, $module]; |
|
292
|
3 |
|
} |
|
293
|
|
|
|
|
294
|
1 |
|
public function removeModuleFromRole(Module $module, Role $role) |
|
295
|
|
|
{ |
|
296
|
1 |
|
unset($this->role_module[$role->getId()][$module->getId()]); |
|
297
|
1 |
|
} |
|
298
|
|
|
|
|
299
|
3 |
|
public function getModulesFromRole(Role $role): array |
|
300
|
|
|
{ |
|
301
|
3 |
|
return $this->extractY($this->role_module, 'canrun', $this->moduleDataStore, $role); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
4 |
|
public function getRolesFromModule(Module $module): array |
|
305
|
|
|
{ |
|
306
|
4 |
|
return $this->extractX($this->role_module, 'authorizedby', $this->roleDataStore, $module); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/* |
|
310
|
|
|
* Privates |
|
311
|
|
|
*/ |
|
312
|
|
|
|
|
313
|
18 |
|
private function extractX($from, $who, $available, $entityY): array |
|
314
|
|
|
{ |
|
315
|
|
|
$r = [ |
|
316
|
18 |
|
$who => [], |
|
317
|
18 |
|
'available' => $available |
|
318
|
|
|
]; |
|
319
|
|
|
|
|
320
|
18 |
|
foreach($from as $xKey => $xs) |
|
321
|
16 |
|
foreach($xs as $yKey => $pair) |
|
322
|
13 |
|
if( $yKey === $entityY->getId() ) { |
|
323
|
13 |
|
$r[$who][$xKey] = $pair[0]; |
|
324
|
13 |
|
unset($r['available'][$xKey]); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
18 |
|
return $r; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
11 |
|
private function extractY($from, $who, $available, $entityX): array |
|
331
|
|
|
{ |
|
332
|
|
|
$r = [ |
|
333
|
11 |
|
$who => [], |
|
334
|
11 |
|
'available' => $available |
|
335
|
|
|
]; |
|
336
|
|
|
|
|
337
|
11 |
|
foreach($from as $xKey => $xs) |
|
338
|
9 |
|
foreach($xs as $yKey => $pair) |
|
339
|
7 |
|
if ($xKey === $entityX->getId()) { |
|
340
|
7 |
|
$r[$who][$yKey] = $pair[1]; |
|
341
|
7 |
|
unset($r['available'][$yKey]); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
11 |
|
return $r; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
65 |
|
private function failIfNotExists($entity, $id) |
|
348
|
|
|
{ |
|
349
|
65 |
|
$datastore = strtolower($entity) . 'DataStore'; |
|
350
|
65 |
|
if( ! isset($this->$datastore[$id]) ){ |
|
351
|
4 |
|
throw new InvalidArgumentException(ucfirst($entity)." with id = {$id} not found"); |
|
352
|
|
|
} |
|
353
|
64 |
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|