Completed
Push — master ( 92e050...06a065 )
by
unknown
19:39
created

RoleService::addPolicyByRoleDraft()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Event;
10
11
use eZ\Publish\API\Repository\RoleService as RoleServiceInterface;
12
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation;
13
use eZ\Publish\API\Repository\Values\User\Policy;
14
use eZ\Publish\API\Repository\Values\User\PolicyCreateStruct;
15
use eZ\Publish\API\Repository\Values\User\PolicyDraft;
16
use eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct;
17
use eZ\Publish\API\Repository\Values\User\Role;
18
use eZ\Publish\API\Repository\Values\User\RoleAssignment;
19
use eZ\Publish\API\Repository\Values\User\RoleCreateStruct;
20
use eZ\Publish\API\Repository\Values\User\RoleDraft;
21
use eZ\Publish\API\Repository\Values\User\RoleUpdateStruct;
22
use eZ\Publish\API\Repository\Values\User\User;
23
use eZ\Publish\API\Repository\Values\User\UserGroup;
24
use eZ\Publish\Core\Event\Role\AddPolicyByRoleDraftEvent;
25
use eZ\Publish\Core\Event\Role\AddPolicyEvent;
26
use eZ\Publish\Core\Event\Role\AssignRoleToUserEvent;
27
use eZ\Publish\Core\Event\Role\AssignRoleToUserGroupEvent;
28
use eZ\Publish\Core\Event\Role\BeforeAddPolicyByRoleDraftEvent;
29
use eZ\Publish\Core\Event\Role\BeforeAddPolicyEvent;
30
use eZ\Publish\Core\Event\Role\BeforeAssignRoleToUserEvent;
31
use eZ\Publish\Core\Event\Role\BeforeAssignRoleToUserGroupEvent;
32
use eZ\Publish\Core\Event\Role\BeforeCreateRoleDraftEvent;
33
use eZ\Publish\Core\Event\Role\BeforeCreateRoleEvent;
34
use eZ\Publish\Core\Event\Role\BeforeDeletePolicyEvent;
35
use eZ\Publish\Core\Event\Role\BeforeDeleteRoleDraftEvent;
36
use eZ\Publish\Core\Event\Role\BeforeDeleteRoleEvent;
37
use eZ\Publish\Core\Event\Role\BeforePublishRoleDraftEvent;
38
use eZ\Publish\Core\Event\Role\BeforeRemovePolicyByRoleDraftEvent;
39
use eZ\Publish\Core\Event\Role\BeforeRemoveRoleAssignmentEvent;
40
use eZ\Publish\Core\Event\Role\BeforeUnassignRoleFromUserEvent;
41
use eZ\Publish\Core\Event\Role\BeforeUnassignRoleFromUserGroupEvent;
42
use eZ\Publish\Core\Event\Role\BeforeUpdatePolicyByRoleDraftEvent;
43
use eZ\Publish\Core\Event\Role\BeforeUpdatePolicyEvent;
44
use eZ\Publish\Core\Event\Role\BeforeUpdateRoleDraftEvent;
45
use eZ\Publish\Core\Event\Role\BeforeUpdateRoleEvent;
46
use eZ\Publish\Core\Event\Role\CreateRoleDraftEvent;
47
use eZ\Publish\Core\Event\Role\CreateRoleEvent;
48
use eZ\Publish\Core\Event\Role\DeletePolicyEvent;
49
use eZ\Publish\Core\Event\Role\DeleteRoleDraftEvent;
50
use eZ\Publish\Core\Event\Role\DeleteRoleEvent;
51
use eZ\Publish\Core\Event\Role\PublishRoleDraftEvent;
52
use eZ\Publish\Core\Event\Role\RemovePolicyByRoleDraftEvent;
53
use eZ\Publish\Core\Event\Role\RemoveRoleAssignmentEvent;
54
use eZ\Publish\Core\Event\Role\UnassignRoleFromUserEvent;
55
use eZ\Publish\Core\Event\Role\UnassignRoleFromUserGroupEvent;
56
use eZ\Publish\Core\Event\Role\UpdatePolicyByRoleDraftEvent;
57
use eZ\Publish\Core\Event\Role\UpdatePolicyEvent;
58
use eZ\Publish\Core\Event\Role\UpdateRoleDraftEvent;
59
use eZ\Publish\Core\Event\Role\UpdateRoleEvent;
60
use eZ\Publish\SPI\Repository\Decorator\RoleServiceDecorator;
61
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
62
63
class RoleService extends RoleServiceDecorator
64
{
65
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
66
    protected $eventDispatcher;
67
68
    public function __construct(
69
        RoleServiceInterface $innerService,
70
        EventDispatcherInterface $eventDispatcher
71
    ) {
72
        parent::__construct($innerService);
73
74
        $this->eventDispatcher = $eventDispatcher;
75
    }
76
77
    public function createRole(RoleCreateStruct $roleCreateStruct): RoleDraft
78
    {
79
        $eventData = [$roleCreateStruct];
80
81
        $beforeEvent = new BeforeCreateRoleEvent(...$eventData);
82
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
83
            return $beforeEvent->getRoleDraft();
84
        }
85
86
        $roleDraft = $beforeEvent->hasRoleDraft()
87
            ? $beforeEvent->getRoleDraft()
88
            : $this->innerService->createRole($roleCreateStruct);
89
90
        $this->eventDispatcher->dispatch(new CreateRoleEvent($roleDraft, ...$eventData));
91
92
        return $roleDraft;
93
    }
94
95
    public function createRoleDraft(Role $role)
96
    {
97
        $eventData = [$role];
98
99
        $beforeEvent = new BeforeCreateRoleDraftEvent(...$eventData);
100
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
101
            return $beforeEvent->getRoleDraft();
102
        }
103
104
        $roleDraft = $beforeEvent->hasRoleDraft()
105
            ? $beforeEvent->getRoleDraft()
106
            : $this->innerService->createRoleDraft($role);
107
108
        $this->eventDispatcher->dispatch(new CreateRoleDraftEvent($roleDraft, ...$eventData));
109
110
        return $roleDraft;
111
    }
112
113
    public function updateRoleDraft(
114
        RoleDraft $roleDraft,
115
        RoleUpdateStruct $roleUpdateStruct
116
    ) {
117
        $eventData = [
118
            $roleDraft,
119
            $roleUpdateStruct,
120
        ];
121
122
        $beforeEvent = new BeforeUpdateRoleDraftEvent(...$eventData);
123
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
124
            return $beforeEvent->getUpdatedRoleDraft();
125
        }
126
127
        $updatedRoleDraft = $beforeEvent->hasUpdatedRoleDraft()
128
            ? $beforeEvent->getUpdatedRoleDraft()
129
            : $this->innerService->updateRoleDraft($roleDraft, $roleUpdateStruct);
130
131
        $this->eventDispatcher->dispatch(new UpdateRoleDraftEvent($updatedRoleDraft, ...$eventData));
132
133
        return $updatedRoleDraft;
134
    }
135
136
    public function addPolicyByRoleDraft(
137
        RoleDraft $roleDraft,
138
        PolicyCreateStruct $policyCreateStruct
139
    ) {
140
        $eventData = [
141
            $roleDraft,
142
            $policyCreateStruct,
143
        ];
144
145
        $beforeEvent = new BeforeAddPolicyByRoleDraftEvent(...$eventData);
146
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
147
            return $beforeEvent->getUpdatedRoleDraft();
148
        }
149
150
        $updatedRoleDraft = $beforeEvent->hasUpdatedRoleDraft()
151
            ? $beforeEvent->getUpdatedRoleDraft()
152
            : $this->innerService->addPolicyByRoleDraft($roleDraft, $policyCreateStruct);
153
154
        $this->eventDispatcher->dispatch(new AddPolicyByRoleDraftEvent($updatedRoleDraft, ...$eventData));
155
156
        return $updatedRoleDraft;
157
    }
158
159
    public function removePolicyByRoleDraft(
160
        RoleDraft $roleDraft,
161
        PolicyDraft $policyDraft
162
    ) {
163
        $eventData = [
164
            $roleDraft,
165
            $policyDraft,
166
        ];
167
168
        $beforeEvent = new BeforeRemovePolicyByRoleDraftEvent(...$eventData);
169
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
170
            return $beforeEvent->getUpdatedRoleDraft();
171
        }
172
173
        $updatedRoleDraft = $beforeEvent->hasUpdatedRoleDraft()
174
            ? $beforeEvent->getUpdatedRoleDraft()
175
            : $this->innerService->removePolicyByRoleDraft($roleDraft, $policyDraft);
176
177
        $this->eventDispatcher->dispatch(new RemovePolicyByRoleDraftEvent($updatedRoleDraft, ...$eventData));
178
179
        return $updatedRoleDraft;
180
    }
181
182
    public function updatePolicyByRoleDraft(
183
        RoleDraft $roleDraft,
184
        PolicyDraft $policy,
185
        PolicyUpdateStruct $policyUpdateStruct
186
    ) {
187
        $eventData = [
188
            $roleDraft,
189
            $policy,
190
            $policyUpdateStruct,
191
        ];
192
193
        $beforeEvent = new BeforeUpdatePolicyByRoleDraftEvent(...$eventData);
194
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
195
            return $beforeEvent->getUpdatedPolicyDraft();
196
        }
197
198
        $updatedPolicyDraft = $beforeEvent->hasUpdatedPolicyDraft()
199
            ? $beforeEvent->getUpdatedPolicyDraft()
200
            : $this->innerService->updatePolicyByRoleDraft($roleDraft, $policy, $policyUpdateStruct);
201
202
        $this->eventDispatcher->dispatch(new UpdatePolicyByRoleDraftEvent($updatedPolicyDraft, ...$eventData));
203
204
        return $updatedPolicyDraft;
205
    }
206
207 View Code Duplication
    public function deleteRoleDraft(RoleDraft $roleDraft): void
208
    {
209
        $eventData = [$roleDraft];
210
211
        $beforeEvent = new BeforeDeleteRoleDraftEvent(...$eventData);
212
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
213
            return;
214
        }
215
216
        $this->innerService->deleteRoleDraft($roleDraft);
217
218
        $this->eventDispatcher->dispatch(new DeleteRoleDraftEvent(...$eventData));
219
    }
220
221 View Code Duplication
    public function publishRoleDraft(RoleDraft $roleDraft): void
222
    {
223
        $eventData = [$roleDraft];
224
225
        $beforeEvent = new BeforePublishRoleDraftEvent(...$eventData);
226
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
227
            return;
228
        }
229
230
        $this->innerService->publishRoleDraft($roleDraft);
231
232
        $this->eventDispatcher->dispatch(new PublishRoleDraftEvent(...$eventData));
233
    }
234
235
    public function updateRole(
236
        Role $role,
237
        RoleUpdateStruct $roleUpdateStruct
238
    ) {
239
        $eventData = [
240
            $role,
241
            $roleUpdateStruct,
242
        ];
243
244
        $beforeEvent = new BeforeUpdateRoleEvent(...$eventData);
245
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
246
            return $beforeEvent->getUpdatedRole();
247
        }
248
249
        $updatedRole = $beforeEvent->hasUpdatedRole()
250
            ? $beforeEvent->getUpdatedRole()
251
            : $this->innerService->updateRole($role, $roleUpdateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...leService::updateRole() has been deprecated with message: since 6.0, use {@see updateRoleDraft}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
252
253
        $this->eventDispatcher->dispatch(new UpdateRoleEvent($updatedRole, ...$eventData));
254
255
        return $updatedRole;
256
    }
257
258
    public function addPolicy(
259
        Role $role,
260
        PolicyCreateStruct $policyCreateStruct
261
    ) {
262
        $eventData = [
263
            $role,
264
            $policyCreateStruct,
265
        ];
266
267
        $beforeEvent = new BeforeAddPolicyEvent(...$eventData);
268
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
269
            return $beforeEvent->getUpdatedRole();
270
        }
271
272
        $updatedRole = $beforeEvent->hasUpdatedRole()
273
            ? $beforeEvent->getUpdatedRole()
274
            : $this->innerService->addPolicy($role, $policyCreateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\RoleService::addPolicy() has been deprecated with message: since 6.0, use {@see addPolicyByRoleDraft}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
275
276
        $this->eventDispatcher->dispatch(new AddPolicyEvent($updatedRole, ...$eventData));
277
278
        return $updatedRole;
279
    }
280
281
    public function deletePolicy(Policy $policy): void
282
    {
283
        $eventData = [$policy];
284
285
        $beforeEvent = new BeforeDeletePolicyEvent(...$eventData);
286
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
287
            return;
288
        }
289
290
        $this->innerService->deletePolicy($policy);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::deletePolicy() has been deprecated with message: since 6.0, use {@link removePolicyByRoleDraft()} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
291
292
        $this->eventDispatcher->dispatch(new DeletePolicyEvent(...$eventData));
293
    }
294
295
    public function updatePolicy(
296
        Policy $policy,
297
        PolicyUpdateStruct $policyUpdateStruct
298
    ) {
299
        $eventData = [
300
            $policy,
301
            $policyUpdateStruct,
302
        ];
303
304
        $beforeEvent = new BeforeUpdatePolicyEvent(...$eventData);
305
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
306
            return $beforeEvent->getUpdatedPolicy();
307
        }
308
309
        $updatedPolicy = $beforeEvent->hasUpdatedPolicy()
310
            ? $beforeEvent->getUpdatedPolicy()
311
            : $this->innerService->updatePolicy($policy, $policyUpdateStruct);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...Service::updatePolicy() has been deprecated with message: since 6.0, use {@link updatePolicyByRoleDraft()} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
312
313
        $this->eventDispatcher->dispatch(new UpdatePolicyEvent($updatedPolicy, ...$eventData));
314
315
        return $updatedPolicy;
316
    }
317
318
    public function deleteRole(Role $role): void
319
    {
320
        $eventData = [$role];
321
322
        $beforeEvent = new BeforeDeleteRoleEvent(...$eventData);
323
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
324
            return;
325
        }
326
327
        $this->innerService->deleteRole($role);
328
329
        $this->eventDispatcher->dispatch(new DeleteRoleEvent(...$eventData));
330
    }
331
332 View Code Duplication
    public function assignRoleToUserGroup(
333
        Role $role,
334
        UserGroup $userGroup,
335
        RoleLimitation $roleLimitation = null
336
    ): void {
337
        $eventData = [
338
            $role,
339
            $userGroup,
340
            $roleLimitation,
341
        ];
342
343
        $beforeEvent = new BeforeAssignRoleToUserGroupEvent(...$eventData);
344
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
345
            return;
346
        }
347
348
        $this->innerService->assignRoleToUserGroup($role, $userGroup, $roleLimitation);
349
350
        $this->eventDispatcher->dispatch(new AssignRoleToUserGroupEvent(...$eventData));
351
    }
352
353
    public function unassignRoleFromUserGroup(
354
        Role $role,
355
        UserGroup $userGroup
356
    ): void {
357
        $eventData = [
358
            $role,
359
            $userGroup,
360
        ];
361
362
        $beforeEvent = new BeforeUnassignRoleFromUserGroupEvent(...$eventData);
363
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
364
            return;
365
        }
366
367
        $this->innerService->unassignRoleFromUserGroup($role, $userGroup);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...signRoleFromUserGroup() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
368
369
        $this->eventDispatcher->dispatch(new UnassignRoleFromUserGroupEvent(...$eventData));
370
    }
371
372 View Code Duplication
    public function assignRoleToUser(
373
        Role $role,
374
        User $user,
375
        RoleLimitation $roleLimitation = null
376
    ): void {
377
        $eventData = [
378
            $role,
379
            $user,
380
            $roleLimitation,
381
        ];
382
383
        $beforeEvent = new BeforeAssignRoleToUserEvent(...$eventData);
384
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
385
            return;
386
        }
387
388
        $this->innerService->assignRoleToUser($role, $user, $roleLimitation);
389
390
        $this->eventDispatcher->dispatch(new AssignRoleToUserEvent(...$eventData));
391
    }
392
393
    public function unassignRoleFromUser(
394
        Role $role,
395
        User $user
396
    ): void {
397
        $eventData = [
398
            $role,
399
            $user,
400
        ];
401
402
        $beforeEvent = new BeforeUnassignRoleFromUserEvent(...$eventData);
403
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
404
            return;
405
        }
406
407
        $this->innerService->unassignRoleFromUser($role, $user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...:unassignRoleFromUser() has been deprecated with message: since 6.0, use {@see removeRoleAssignment} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
408
409
        $this->eventDispatcher->dispatch(new UnassignRoleFromUserEvent(...$eventData));
410
    }
411
412
    public function removeRoleAssignment(RoleAssignment $roleAssignment): void
413
    {
414
        $eventData = [$roleAssignment];
415
416
        $beforeEvent = new BeforeRemoveRoleAssignmentEvent(...$eventData);
417
        if ($this->eventDispatcher->dispatch($beforeEvent)->isPropagationStopped()) {
418
            return;
419
        }
420
421
        $this->innerService->removeRoleAssignment($roleAssignment);
422
423
        $this->eventDispatcher->dispatch(new RemoveRoleAssignmentEvent(...$eventData));
424
    }
425
}
426