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