Passed
Push — main ( 3fc6af...0545d9 )
by Proyecto
08:13
created

InMemoryRepository::clearUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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