Passed
Push — main ( 0545d9...29d231 )
by Proyecto
03:14
created

InMemoryRepository::getModulesFromRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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