Completed
Pull Request — master (#394)
by
unknown
09:40
created

Role::applyConstraintUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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 ValueObjects\Identity\UUID;
20
use ValueObjects\StringLiteral\StringLiteral;
21
22
class Role extends EventSourcedAggregateRoot
23
{
24
    /**
25
     * @var UUID
26
     */
27
    private $uuid;
28
29
    /**
30
     * @var StringLiteral
31
     */
32
    private $name;
33
34
    /**
35
     * @var StringLiteral
36
     */
37
    private $query;
38
39
    /**
40
     * @var Permission[]
41
     */
42
    private $permissions = [];
43
44
    /**
45
     * @var UUID[]
46
     */
47
    private $labelIds = [];
48
49
    /**
50
     * @var StringLiteral[]
51
     */
52
    private $userIds = [];
53
54
    /**
55
     * @return string
56
     */
57
    public function getAggregateRootId()
58
    {
59
        return $this->uuid;
60
    }
61
62
    /**
63
     * @param UUID $uuid
64
     * @param StringLiteral $name
65
     * @return Role
66
     */
67
    public static function create(
68
        UUID $uuid,
69
        StringLiteral $name
70
    ) {
71
        $role = new Role();
72
73
        $role->apply(new RoleCreated(
74
            $uuid,
75
            $name
76
        ));
77
78
        return $role;
79
    }
80
81
    /**
82
     * Rename the role.
83
     *
84
     * @param UUID $uuid
85
     * @param StringLiteral $name
86
     */
87
    public function rename(
88
        UUID $uuid,
89
        StringLiteral $name
90
    ) {
91
        $this->apply(new RoleRenamed($uuid, $name));
92
    }
93
94
    /**
95
     * Set a constraint on the role.
96
     *
97
     * @param UUID $uuid
98
     * @param StringLiteral $query
99
     */
100
    public function setConstraint(
101
        UUID $uuid,
102
        StringLiteral $query
103
    ) {
104
        if (empty($this->query)) {
105
            if (!empty($query) && !$query->isEmpty()) {
106
                $this->apply(new ConstraintAdded($uuid, $query));
107
            }
108
        } else {
109
            if (!empty($query) && !$query->isEmpty()) {
110
                $this->apply(new ConstraintUpdated($uuid, $query));
111
            } else {
112
                $this->apply(new ConstraintRemoved($uuid));
113
            }
114
        }
115
    }
116
117
    /**
118
     * Add a permission to the role.
119
     *
120
     * @param UUID $uuid
121
     * @param Permission $permission
122
     */
123
    public function addPermission(
124
        UUID $uuid,
125
        Permission $permission
126
    ) {
127
        if (!in_array($permission, $this->permissions)) {
128
            $this->apply(new PermissionAdded($uuid, $permission));
129
        }
130
    }
131
132
    /**
133
     * Remove a permission from the role.
134
     *
135
     * @param UUID $uuid
136
     * @param Permission $permission
137
     */
138
    public function removePermission(
139
        UUID $uuid,
140
        Permission $permission
141
    ) {
142
        if (in_array($permission, $this->permissions)) {
143
            $this->apply(new PermissionRemoved($uuid, $permission));
144
        }
145
    }
146
147
    /**
148
     * @param UUID $labelId
149
     */
150
    public function addLabel(
151
        UUID $labelId
152
    ) {
153
        if (!in_array($labelId, $this->labelIds)) {
154
            $this->apply(new LabelAdded($this->uuid, $labelId));
155
        }
156
    }
157
158
    /**
159
     * @param \ValueObjects\Identity\UUID $labelId
160
     */
161
    public function removeLabel(
162
        UUID $labelId
163
    ) {
164
        if (in_array($labelId, $this->labelIds)) {
165
            $this->apply(new LabelRemoved($this->uuid, $labelId));
166
        }
167
    }
168
169
    /**
170
     * @param StringLiteral $userId
171
     */
172
    public function addUser(
173
        StringLiteral $userId
174
    ) {
175
        if (!in_array($userId, $this->userIds)) {
176
            $this->apply(new UserAdded($this->uuid, $userId));
177
        }
178
    }
179
180
    /**
181
     * @param StringLiteral $userId
182
     */
183
    public function removeUser(
184
        StringLiteral $userId
185
    ) {
186
        if (in_array($userId, $this->userIds)) {
187
            $this->apply(new UserRemoved($this->uuid, $userId));
188
        }
189
    }
190
191
    /**
192
     * Delete a role.
193
     *
194
     * @param UUID $uuid
195
     */
196
    public function delete(
197
        UUID $uuid
198
    ) {
199
        $this->apply(new RoleDeleted($uuid));
200
    }
201
202
    /**
203
     * @param RoleCreated $roleCreated
204
     */
205
    public function applyRoleCreated(RoleCreated $roleCreated)
206
    {
207
        $this->uuid = $roleCreated->getUuid();
208
        $this->name = $roleCreated->getName();
209
    }
210
211
    /**
212
     * @param RoleRenamed $roleRenamed
213
     */
214
    public function applyRoleRenamed(RoleRenamed $roleRenamed)
215
    {
216
        $this->name = $roleRenamed->getName();
217
    }
218
219
    /**
220
     * @param ConstraintAdded $constraintAdded
221
     */
222
    public function applyConstraintAdded(ConstraintAdded $constraintAdded)
223
    {
224
        $this->query = $constraintAdded->getQuery();
225
    }
226
227
    /**
228
     * @param ConstraintUpdated $constraintUpdated
229
     */
230
    public function applyConstraintUpdated(ConstraintUpdated $constraintUpdated)
231
    {
232
        $this->query = $constraintUpdated->getQuery();
233
    }
234
235
    /**
236
     * @param ConstraintRemoved $constraintRemoved
237
     */
238
    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...
239
    {
240
        $this->query = new StringLiteral('');
241
    }
242
243
    /**
244
     * @param PermissionAdded $permissionAdded
245
     */
246
    public function applyPermissionAdded(PermissionAdded $permissionAdded)
247
    {
248
        $permission = $permissionAdded->getPermission();
249
250
        $this->permissions[$permission->getName()] = $permission;
251
    }
252
253
    /**
254
     * @param PermissionRemoved $permissionRemoved
255
     */
256
    public function applyPermissionRemoved(PermissionRemoved $permissionRemoved)
257
    {
258
        $permission = $permissionRemoved->getPermission();
259
260
        unset($this->permissions[$permission->getName()]);
261
    }
262
263
    /**
264
     * @param LabelAdded $labelAdded
265
     */
266
    public function applyLabelAdded(LabelAdded $labelAdded)
267
    {
268
        $labelId = $labelAdded->getLabelId();
269
        $this->labelIds[] = $labelId;
270
    }
271
272
    /**
273
     * @param LabelRemoved $labelRemoved
274
     */
275
    public function applyLabelRemoved(LabelRemoved $labelRemoved)
276
    {
277
        $labelId = $labelRemoved->getLabelId();
278
        $this->labelIds = array_diff($this->labelIds, [$labelId]);
279
    }
280
281
    /**
282
     * @param UserAdded $userAdded
283
     */
284
    public function applyUserAdded(UserAdded $userAdded)
285
    {
286
        $userId = $userAdded->getUserId();
287
        $this->userIds[] = $userId;
288
    }
289
290
    /**
291
     * @param UserRemoved $userRemoved
292
     */
293
    public function applyUserRemoved(UserRemoved $userRemoved)
294
    {
295
        $userId = $userRemoved->getUserId();
296
297
        if (($index = array_search($userId, $this->userIds)) !== false) {
298
            unset($this->userIds[$index]);
299
        }
300
    }
301
}
302