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 |
17
|
|
|
{ |
18
|
|
|
// Hold the class instance. |
19
|
|
|
private static $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
|
|
|
public static function getInstance() |
42
|
|
|
{ |
43
|
|
|
if (self::$instance == null) |
44
|
|
|
{ |
45
|
|
|
self::$instance = new static(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return self::$instance; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function clear() |
52
|
|
|
{ |
53
|
|
|
$this->groupDataStore = []; |
54
|
|
|
$this->userDataStore = []; |
55
|
|
|
$this->roleDataStore = []; |
56
|
|
|
$this->moduleDataStore = []; |
57
|
|
|
$this->user_group = []; |
58
|
|
|
$this->group_role = []; |
59
|
|
|
$this->role_module = []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/* |
63
|
|
|
* User |
64
|
|
|
*/ |
65
|
|
|
|
66
|
|
|
public function createUser(User $user): void |
67
|
|
|
{ |
68
|
|
|
$this->userDataStore[$user->getId()] = $user; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function readUser($id): User |
72
|
|
|
{ |
73
|
|
|
$this->failIfNotExists('User', $id); |
74
|
|
|
|
75
|
|
|
return $this->userDataStore[$id]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function updateUser($id, $name, $surname, $login): void |
79
|
|
|
{ |
80
|
|
|
$this->failIfNotExists('User', $id); |
81
|
|
|
|
82
|
|
|
$this->userDataStore[$id]->setName($name); |
83
|
|
|
$this->userDataStore[$id]->setName($surname); |
84
|
|
|
$this->userDataStore[$id]->setName($login); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function deleteUser($id): void |
88
|
|
|
{ |
89
|
|
|
$this->failIfNotExists('User', $id); |
90
|
|
|
|
91
|
|
|
unset($this->userDataStore[$id]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/* |
95
|
|
|
* Group |
96
|
|
|
*/ |
97
|
|
|
|
98
|
|
|
public function createGroup(Group $group): void |
99
|
|
|
{ |
100
|
|
|
$this->groupDataStore[$group->getId()] = $group; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function readGroup($id): Group |
104
|
|
|
{ |
105
|
|
|
$this->failIfNotExists('Group', $id); |
106
|
|
|
|
107
|
|
|
return $this->groupDataStore[$id]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function updateGroup($id, $name, $desc): void |
111
|
|
|
{ |
112
|
|
|
$this->failIfNotExists('Group', $id); |
113
|
|
|
|
114
|
|
|
$this->groupDataStore[$id]->setName($name); |
115
|
|
|
$this->groupDataStore[$id]->setName($desc); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function deleteGroup($id): void |
119
|
|
|
{ |
120
|
|
|
$this->failIfNotExists('Group', $id); |
121
|
|
|
|
122
|
|
|
unset($this->groupDataStore[$id]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* Role |
127
|
|
|
*/ |
128
|
|
|
|
129
|
|
|
public function createRole(Role $role): void |
130
|
|
|
{ |
131
|
|
|
$this->roleDataStore[$role->getId()] = $role; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function readRole($id): Role |
135
|
|
|
{ |
136
|
|
|
$this->failIfNotExists('Role', $id); |
137
|
|
|
|
138
|
|
|
return $this->roleDataStore[$id]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function updateRole($id, $name, $desc): void |
142
|
|
|
{ |
143
|
|
|
$this->failIfNotExists('Role', $id); |
144
|
|
|
|
145
|
|
|
$this->roleDataStore[$id]->setName($name); |
146
|
|
|
$this->roleDataStore[$id]->setName($desc); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function deleteRole($id): void |
150
|
|
|
{ |
151
|
|
|
$this->failIfNotExists('Role', $id); |
152
|
|
|
|
153
|
|
|
unset($this->roleDataStore[$id]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/* |
157
|
|
|
* Module |
158
|
|
|
*/ |
159
|
|
|
|
160
|
|
|
public function createModule(Module $module): void |
161
|
|
|
{ |
162
|
|
|
$this->moduleDataStore[$module->getId()] = $module; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function readModule($id): Module |
166
|
|
|
{ |
167
|
|
|
$this->failIfNotExists('Module', $id); |
168
|
|
|
|
169
|
|
|
return $this->moduleDataStore[$id]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function updateModule($id, $name, $desc): void |
173
|
|
|
{ |
174
|
|
|
$this->failIfNotExists('Module', $id); |
175
|
|
|
|
176
|
|
|
$this->moduleDataStore[$id]->setName($name); |
177
|
|
|
$this->moduleDataStore[$id]->setName($desc); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function deleteModule($id): void |
181
|
|
|
{ |
182
|
|
|
unset($this->moduleDataStore[$id]); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/* |
186
|
|
|
* Relations |
187
|
|
|
*/ |
188
|
|
|
|
189
|
|
|
public function addUserToGroup(User $user, Group $group) |
190
|
|
|
{ |
191
|
|
|
$this->user_group[$user->getId()][$group->getId()] = [$user, $group]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function removeUserToGroup(User $user, Group $group) |
195
|
|
|
{ |
196
|
|
|
unset($this->user_group[$user->getId()][$group->getId()]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function addGroupToUser(Group $group, User $user) |
200
|
|
|
{ |
201
|
|
|
$this->user_group[$user->getId()][$group->getId()] = [$user, $group]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function removeGroupFromUser(Group $group, User $user) |
205
|
|
|
{ |
206
|
|
|
unset($this->user_group[$user->getId()][$group->getId()]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getGroupsFromUser(User $user): array |
210
|
|
|
{ |
211
|
|
|
return $this->extractY($this->user_group, 'belongsto', $this->groupDataStore, $user); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function getUsersFromGroup(Group $group): array |
215
|
|
|
{ |
216
|
|
|
return $this->extractX($this->user_group, 'members', $this->userDataStore, $group); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function addGroupToRole(Group $group, Role $role) |
220
|
|
|
{ |
221
|
|
|
$this->group_role[$group->getId()][$role->getId()] = [$group, $role]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function removeGroupFromRole(Group $group, Role $role) |
225
|
|
|
{ |
226
|
|
|
unset($this->group_role[$group->getId()][$role->getId()]); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function addRoleToGroup(Role $role, Group $group) |
230
|
|
|
{ |
231
|
|
|
$this->group_role[$group->getId()][$role->getId()] = [$group, $role]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function removeRoleToGroup(Role $role, Group $group) |
235
|
|
|
{ |
236
|
|
|
unset($this->group_role[$group->getId()][$role->getId()]); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function getRolesFromGroup(Group $group): array |
240
|
|
|
{ |
241
|
|
|
return $this->extractY($this->group_role, 'plays', $this->roleDataStore, $group); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function getGroupsFromRole(Role $role): array |
245
|
|
|
{ |
246
|
|
|
return $this->extractX($this->group_role, 'grantedby', $this->groupDataStore, $role); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function addRoleToModule(Role $role, Module $module) |
250
|
|
|
{ |
251
|
|
|
$this->role_module[$role->getId()][$module->getId()] = [$role, $module]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function removeRoleFromModule(Role $role, Module $module) |
255
|
|
|
{ |
256
|
|
|
unset($this->role_module[$role->getId()][$module->getId()]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function addModuleToRole(Module $module, Role $role) |
260
|
|
|
{ |
261
|
|
|
$this->role_module[$role->getId()][$module->getId()] = [$role, $module]; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function removeModuleFromRole(Module $module, Role $role) |
265
|
|
|
{ |
266
|
|
|
unset($this->role_module[$role->getId()][$module->getId()]); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function getModulesFromRole(Role $role): array |
270
|
|
|
{ |
271
|
|
|
return $this->extractY($this->role_module, 'canrun', $this->moduleDataStore, $role); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function getRolesFromModule(Module $module): array |
275
|
|
|
{ |
276
|
|
|
return $this->extractX($this->role_module, 'authorizedby', $this->roleDataStore, $module); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/* |
280
|
|
|
* Privates |
281
|
|
|
*/ |
282
|
|
|
|
283
|
|
|
private function extractX($from, $who, $available, $entityY) |
284
|
|
|
{ |
285
|
|
|
$r = [ |
286
|
|
|
$who => [], |
287
|
|
|
'available' => $available |
288
|
|
|
]; |
289
|
|
|
|
290
|
|
|
foreach($from as $xKey => $xs) |
291
|
|
|
foreach($xs as $yKey => $pair) |
292
|
|
|
if( $yKey === $entityY->getId() ) { |
293
|
|
|
$r[$who][$xKey] = $pair[0]; |
294
|
|
|
unset($r['available'][$xKey]); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return $r; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
private function extractY($from, $who, $available, $entityX) |
301
|
|
|
{ |
302
|
|
|
$r = [ |
303
|
|
|
$who => [], |
304
|
|
|
'available' => $available |
305
|
|
|
]; |
306
|
|
|
|
307
|
|
|
foreach($from as $xKey => $xs) |
308
|
|
|
foreach($xs as $yKey => $pair) |
309
|
|
|
if ($xKey === $entityX->getId()) { |
310
|
|
|
$r[$who][$yKey] = $pair[1]; |
311
|
|
|
unset($r['available'][$yKey]); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $r; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
private function failIfNotExists($entity, $id) |
318
|
|
|
{ |
319
|
|
|
$datastore = strtolower($entity) . 'DataStore'; |
320
|
|
|
if( ! isset($this->$datastore[$id]) ){ |
321
|
|
|
throw new InvalidArgumentException(ucfirst($entity)." with id = {$id} not found"); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|