Completed
Pull Request — master (#394)
by
unknown
15:17
created

Role::updateConstraint()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
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));
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
    /*public function removeConstraint(SapiVersion $sapiVersion)
144
    {
145
        $this->apply(new ConstraintRemoved($this->uuid, $sapiVersion));
146
    }*/
147
148
    /**
149
     * @param SapiVersion $sapiVersion
150
     * @return bool
151
     */
152
    private function queryEmpty(SapiVersion $sapiVersion): bool
153
    {
154
        return empty($this->queries[$sapiVersion->toNative()]);
155
    }
156
157
    /**
158
     * @param SapiVersion $sapiVersion
159
     * @param Query $query
160
     * @return bool
161
     */
162
    private function querySameValue(SapiVersion $sapiVersion, Query $query): bool
163
    {
164
        return $this->queries[$sapiVersion->toNative()]->sameValueAs($query);
165
    }
166
167
    /**
168
     * Add a permission to the role.
169
     *
170
     * @param UUID $uuid
171
     * @param Permission $permission
172
     */
173
    public function addPermission(
174
        UUID $uuid,
175
        Permission $permission
176
    ) {
177
        if (!in_array($permission, $this->permissions)) {
178
            $this->apply(new PermissionAdded($uuid, $permission));
179
        }
180
    }
181
182
    /**
183
     * Remove a permission from the role.
184
     *
185
     * @param UUID $uuid
186
     * @param Permission $permission
187
     */
188
    public function removePermission(
189
        UUID $uuid,
190
        Permission $permission
191
    ) {
192
        if (in_array($permission, $this->permissions)) {
193
            $this->apply(new PermissionRemoved($uuid, $permission));
194
        }
195
    }
196
197
    /**
198
     * @param UUID $labelId
199
     */
200
    public function addLabel(
201
        UUID $labelId
202
    ) {
203
        if (!in_array($labelId, $this->labelIds)) {
204
            $this->apply(new LabelAdded($this->uuid, $labelId));
205
        }
206
    }
207
208
    /**
209
     * @param \ValueObjects\Identity\UUID $labelId
210
     */
211
    public function removeLabel(
212
        UUID $labelId
213
    ) {
214
        if (in_array($labelId, $this->labelIds)) {
215
            $this->apply(new LabelRemoved($this->uuid, $labelId));
216
        }
217
    }
218
219
    /**
220
     * @param StringLiteral $userId
221
     */
222
    public function addUser(
223
        StringLiteral $userId
224
    ) {
225
        if (!in_array($userId, $this->userIds)) {
226
            $this->apply(new UserAdded($this->uuid, $userId));
227
        }
228
    }
229
230
    /**
231
     * @param StringLiteral $userId
232
     */
233
    public function removeUser(
234
        StringLiteral $userId
235
    ) {
236
        if (in_array($userId, $this->userIds)) {
237
            $this->apply(new UserRemoved($this->uuid, $userId));
238
        }
239
    }
240
241
    /**
242
     * Delete a role.
243
     *
244
     * @param UUID $uuid
245
     */
246
    public function delete(
247
        UUID $uuid
248
    ) {
249
        $this->apply(new RoleDeleted($uuid));
250
    }
251
252
    /**
253
     * @param RoleCreated $roleCreated
254
     */
255
    public function applyRoleCreated(RoleCreated $roleCreated)
256
    {
257
        $this->uuid = $roleCreated->getUuid();
258
        $this->name = $roleCreated->getName();
259
    }
260
261
    /**
262
     * @param RoleRenamed $roleRenamed
263
     */
264
    public function applyRoleRenamed(RoleRenamed $roleRenamed)
265
    {
266
        $this->name = $roleRenamed->getName();
267
    }
268
269
    /**
270
     * @param ConstraintAdded $constraintAdded
271
     */
272
    public function applyConstraintAdded(ConstraintAdded $constraintAdded)
273
    {
274
        $this->queries[$constraintAdded->getSapiVersion()->toNative()] = $constraintAdded->getQuery();
275
    }
276
277
    /**
278
     * @param ConstraintUpdated $constraintUpdated
279
     */
280
    public function applyConstraintUpdated(ConstraintUpdated $constraintUpdated)
281
    {
282
        $this->queries[$constraintUpdated->getSapiVersion()->toNative()] = $constraintUpdated->getQuery();
283
    }
284
285
    /**
286
     * @param ConstraintRemoved $constraintRemoved
287
     */
288
    public function applyConstraintRemoved(ConstraintRemoved $constraintRemoved)
0 ignored issues
show
Unused Code introduced by
The parameter $constraintRemoved is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
289
    {
290
        $this->queries[SapiVersion::V2] = null;
291
    }
292
293
    /**
294
     * @param PermissionAdded $permissionAdded
295
     */
296
    public function applyPermissionAdded(PermissionAdded $permissionAdded)
297
    {
298
        $permission = $permissionAdded->getPermission();
299
300
        $this->permissions[$permission->getName()] = $permission;
301
    }
302
303
    /**
304
     * @param PermissionRemoved $permissionRemoved
305
     */
306
    public function applyPermissionRemoved(PermissionRemoved $permissionRemoved)
307
    {
308
        $permission = $permissionRemoved->getPermission();
309
310
        unset($this->permissions[$permission->getName()]);
311
    }
312
313
    /**
314
     * @param LabelAdded $labelAdded
315
     */
316
    public function applyLabelAdded(LabelAdded $labelAdded)
317
    {
318
        $labelId = $labelAdded->getLabelId();
319
        $this->labelIds[] = $labelId;
320
    }
321
322
    /**
323
     * @param LabelRemoved $labelRemoved
324
     */
325
    public function applyLabelRemoved(LabelRemoved $labelRemoved)
326
    {
327
        $labelId = $labelRemoved->getLabelId();
328
        $this->labelIds = array_diff($this->labelIds, [$labelId]);
329
    }
330
331
    /**
332
     * @param UserAdded $userAdded
333
     */
334
    public function applyUserAdded(UserAdded $userAdded)
335
    {
336
        $userId = $userAdded->getUserId();
337
        $this->userIds[] = $userId;
338
    }
339
340
    /**
341
     * @param UserRemoved $userRemoved
342
     */
343
    public function applyUserRemoved(UserRemoved $userRemoved)
344
    {
345
        $userId = $userRemoved->getUserId();
346
347
        if (($index = array_search($userId, $this->userIds)) !== false) {
348
            unset($this->userIds[$index]);
349
        }
350
    }
351
}
352