Completed
Pull Request — master (#394)
by Luc
08:30 queued 01:19
created

Role::applyPermissionRemoved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Role;
4
5
use Broadway\EventSourcing\EventSourcedAggregateRoot;
6
use CultuurNet\UDB3\Role\Events\ConstraintAdded;
7
use CultuurNet\UDB3\Role\Events\ConstraintRemoved;
8
use CultuurNet\UDB3\Role\Events\ConstraintUpdated;
9
use CultuurNet\UDB3\Role\Events\LabelAdded;
10
use CultuurNet\UDB3\Role\Events\LabelRemoved;
11
use CultuurNet\UDB3\Role\Events\PermissionAdded;
12
use CultuurNet\UDB3\Role\Events\PermissionRemoved;
13
use CultuurNet\UDB3\Role\Events\RoleCreated;
14
use CultuurNet\UDB3\Role\Events\RoleDeleted;
15
use CultuurNet\UDB3\Role\Events\RoleRenamed;
16
use CultuurNet\UDB3\Role\Events\UserAdded;
17
use CultuurNet\UDB3\Role\Events\UserRemoved;
18
use CultuurNet\UDB3\Role\ValueObjects\Permission;
19
use CultuurNet\UDB3\Role\ValueObjects\Query;
20
use CultuurNet\UDB3\ValueObject\SapiVersion;
21
use ValueObjects\Identity\UUID;
22
use ValueObjects\StringLiteral\StringLiteral;
23
24
class Role extends EventSourcedAggregateRoot
25
{
26
    /**
27
     * @var UUID
28
     */
29
    private $uuid;
30
31
    /**
32
     * @var StringLiteral
33
     */
34
    private $name;
35
36
    /**
37
     * @var Query[]
38
     */
39
    private $queries = [];
40
41
    /**
42
     * @var Permission[]
43
     */
44
    private $permissions = [];
45
46
    /**
47
     * @var UUID[]
48
     */
49
    private $labelIds = [];
50
51
    /**
52
     * @var StringLiteral[]
53
     */
54
    private $userIds = [];
55
56
    /**
57
     * @return string
58
     */
59
    public function getAggregateRootId()
60
    {
61
        return $this->uuid;
62
    }
63
64
    /**
65
     * @param UUID $uuid
66
     * @param StringLiteral $name
67
     * @return Role
68
     */
69
    public static function create(
70
        UUID $uuid,
71
        StringLiteral $name
72
    ) {
73
        $role = new Role();
74
75
        $role->apply(new RoleCreated(
76
            $uuid,
77
            $name
78
        ));
79
80
        return $role;
81
    }
82
83
    /**
84
     * Rename the role.
85
     *
86
     * @param UUID $uuid
87
     * @param StringLiteral $name
88
     */
89
    public function rename(
90
        UUID $uuid,
91
        StringLiteral $name
92
    ) {
93
        $this->apply(new RoleRenamed($uuid, $name));
94
    }
95
96
    /**
97
     * Set a constraint on the role.
98
     *
99
     * @param UUID $uuid
100
     * @param StringLiteral $query
101
     */
102
    public function setConstraint(
103
        UUID $uuid,
104
        StringLiteral $query
105
    ) {
106
        $sapiVersion = SapiVersion::V2();
107
        if (empty($this->queries[$sapiVersion->toNative()])) {
108
            if (!empty($query) && !$query->isEmpty()) {
109
                $this->apply(new ConstraintAdded($uuid, $sapiVersion, new Query($query)));
110
            }
111
        } else {
112
            if (!empty($query) && !$query->isEmpty()) {
113
                $this->apply(new ConstraintUpdated($uuid, $sapiVersion, new Query($query)));
114
            } else {
115
                $this->apply(new ConstraintRemoved($uuid, $sapiVersion));
116
            }
117
        }
118
    }
119
120
    /**
121
     * @param SapiVersion $sapiVersion
122
     * @param Query $query
123
     */
124
    public function addConstraint(SapiVersion $sapiVersion, Query $query): void
125
    {
126
        if ($this->queryEmpty($sapiVersion)) {
127
            $this->apply(new ConstraintAdded($this->uuid, $sapiVersion, $query));
128
        }
129
    }
130
131
    /**
132
     * @param SapiVersion $sapiVersion
133
     * @param Query $query
134
     */
135
    public function updateConstraint(SapiVersion $sapiVersion, Query $query): void
136
    {
137
        if (!$this->queryEmpty($sapiVersion) &&
138
            !$this->querySameValue($sapiVersion, $query)) {
139
            $this->apply(new ConstraintUpdated($this->uuid, $sapiVersion, $query));
140
        }
141
    }
142
143
    /**
144
     * @param SapiVersion $sapiVersion
145
     */
146
    public function removeConstraint(SapiVersion $sapiVersion): void
147
    {
148
        if (!$this->queryEmpty($sapiVersion)) {
149
            $this->apply(new ConstraintRemoved($this->uuid, $sapiVersion));
150
        }
151
    }
152
153
    /**
154
     * @param SapiVersion $sapiVersion
155
     * @return bool
156
     */
157
    private function queryEmpty(SapiVersion $sapiVersion): bool
158
    {
159
        return empty($this->queries[$sapiVersion->toNative()]);
160
    }
161
162
    /**
163
     * @param SapiVersion $sapiVersion
164
     * @param Query $query
165
     * @return bool
166
     */
167
    private function querySameValue(SapiVersion $sapiVersion, Query $query): bool
168
    {
169
        return $this->queries[$sapiVersion->toNative()]->sameValueAs($query);
170
    }
171
172
    /**
173
     * Add a permission to the role.
174
     *
175
     * @param UUID $uuid
176
     * @param Permission $permission
177
     */
178
    public function addPermission(
179
        UUID $uuid,
180
        Permission $permission
181
    ) {
182
        if (!in_array($permission, $this->permissions)) {
183
            $this->apply(new PermissionAdded($uuid, $permission));
184
        }
185
    }
186
187
    /**
188
     * Remove a permission from the role.
189
     *
190
     * @param UUID $uuid
191
     * @param Permission $permission
192
     */
193
    public function removePermission(
194
        UUID $uuid,
195
        Permission $permission
196
    ) {
197
        if (in_array($permission, $this->permissions)) {
198
            $this->apply(new PermissionRemoved($uuid, $permission));
199
        }
200
    }
201
202
    /**
203
     * @param UUID $labelId
204
     */
205
    public function addLabel(
206
        UUID $labelId
207
    ) {
208
        if (!in_array($labelId, $this->labelIds)) {
209
            $this->apply(new LabelAdded($this->uuid, $labelId));
210
        }
211
    }
212
213
    /**
214
     * @param \ValueObjects\Identity\UUID $labelId
215
     */
216
    public function removeLabel(
217
        UUID $labelId
218
    ) {
219
        if (in_array($labelId, $this->labelIds)) {
220
            $this->apply(new LabelRemoved($this->uuid, $labelId));
221
        }
222
    }
223
224
    /**
225
     * @param StringLiteral $userId
226
     */
227
    public function addUser(
228
        StringLiteral $userId
229
    ) {
230
        if (!in_array($userId, $this->userIds)) {
231
            $this->apply(new UserAdded($this->uuid, $userId));
232
        }
233
    }
234
235
    /**
236
     * @param StringLiteral $userId
237
     */
238
    public function removeUser(
239
        StringLiteral $userId
240
    ) {
241
        if (in_array($userId, $this->userIds)) {
242
            $this->apply(new UserRemoved($this->uuid, $userId));
243
        }
244
    }
245
246
    /**
247
     * Delete a role.
248
     *
249
     * @param UUID $uuid
250
     */
251
    public function delete(
252
        UUID $uuid
253
    ) {
254
        $this->apply(new RoleDeleted($uuid));
255
    }
256
257
    /**
258
     * @param RoleCreated $roleCreated
259
     */
260
    public function applyRoleCreated(RoleCreated $roleCreated)
261
    {
262
        $this->uuid = $roleCreated->getUuid();
263
        $this->name = $roleCreated->getName();
264
    }
265
266
    /**
267
     * @param RoleRenamed $roleRenamed
268
     */
269
    public function applyRoleRenamed(RoleRenamed $roleRenamed)
270
    {
271
        $this->name = $roleRenamed->getName();
272
    }
273
274
    /**
275
     * @param ConstraintAdded $constraintAdded
276
     */
277
    public function applyConstraintAdded(ConstraintAdded $constraintAdded)
278
    {
279
        $this->queries[$constraintAdded->getSapiVersion()->toNative()] = $constraintAdded->getQuery();
280
    }
281
282
    /**
283
     * @param ConstraintUpdated $constraintUpdated
284
     */
285
    public function applyConstraintUpdated(ConstraintUpdated $constraintUpdated)
286
    {
287
        $this->queries[$constraintUpdated->getSapiVersion()->toNative()] = $constraintUpdated->getQuery();
288
    }
289
290
    /**
291
     * @param ConstraintRemoved $constraintRemoved
292
     */
293
    public function applyConstraintRemoved(ConstraintRemoved $constraintRemoved)
294
    {
295
        $this->queries[$constraintRemoved->getSapiVersion()->toNative()] = null;
296
    }
297
298
    /**
299
     * @param PermissionAdded $permissionAdded
300
     */
301
    public function applyPermissionAdded(PermissionAdded $permissionAdded)
302
    {
303
        $permission = $permissionAdded->getPermission();
304
305
        $this->permissions[$permission->getName()] = $permission;
306
    }
307
308
    /**
309
     * @param PermissionRemoved $permissionRemoved
310
     */
311
    public function applyPermissionRemoved(PermissionRemoved $permissionRemoved)
312
    {
313
        $permission = $permissionRemoved->getPermission();
314
315
        unset($this->permissions[$permission->getName()]);
316
    }
317
318
    /**
319
     * @param LabelAdded $labelAdded
320
     */
321
    public function applyLabelAdded(LabelAdded $labelAdded)
322
    {
323
        $labelId = $labelAdded->getLabelId();
324
        $this->labelIds[] = $labelId;
325
    }
326
327
    /**
328
     * @param LabelRemoved $labelRemoved
329
     */
330
    public function applyLabelRemoved(LabelRemoved $labelRemoved)
331
    {
332
        $labelId = $labelRemoved->getLabelId();
333
        $this->labelIds = array_diff($this->labelIds, [$labelId]);
334
    }
335
336
    /**
337
     * @param UserAdded $userAdded
338
     */
339
    public function applyUserAdded(UserAdded $userAdded)
340
    {
341
        $userId = $userAdded->getUserId();
342
        $this->userIds[] = $userId;
343
    }
344
345
    /**
346
     * @param UserRemoved $userRemoved
347
     */
348
    public function applyUserRemoved(UserRemoved $userRemoved)
349
    {
350
        $userId = $userRemoved->getUserId();
351
352
        if (($index = array_search($userId, $this->userIds)) !== false) {
353
            unset($this->userIds[$index]);
354
        }
355
    }
356
}
357