Completed
Pull Request — master (#394)
by
unknown
28:13 queued 25s
created

DefaultRoleEditingService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 16

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 16
dl 0
loc 190
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 10 1
A rename() 0 9 1
A addPermission() 0 9 1
A removePermission() 0 9 1
A addUser() 0 9 1
A removeUser() 0 9 1
A setConstraint() 0 9 1
A addConstraint() 0 9 1
A updateConstraint() 0 9 1
A addLabel() 0 9 1
A removeLabel() 0 9 1
A delete() 0 8 1
1
<?php
2
3
namespace CultuurNet\UDB3\Role\Services;
4
5
use Broadway\CommandHandling\CommandBusInterface;
6
use Broadway\Repository\RepositoryInterface;
7
use Broadway\UuidGenerator\UuidGeneratorInterface;
8
use CultuurNet\UDB3\Role\Commands\AddConstraint;
9
use CultuurNet\UDB3\Role\Commands\AddLabel;
10
use CultuurNet\UDB3\Role\Commands\AddPermission;
11
use CultuurNet\UDB3\Role\Commands\AddUser;
12
use CultuurNet\UDB3\Role\Commands\DeleteRole;
13
use CultuurNet\UDB3\Role\Commands\RemoveLabel;
14
use CultuurNet\UDB3\Role\Commands\RemovePermission;
15
use CultuurNet\UDB3\Role\Commands\RemoveUser;
16
use CultuurNet\UDB3\Role\Commands\RenameRole;
17
use CultuurNet\UDB3\Role\Commands\SetConstraint;
18
use CultuurNet\UDB3\Role\Commands\UpdateConstraint;
19
use CultuurNet\UDB3\Role\Role;
20
use CultuurNet\UDB3\Role\ValueObjects\Permission;
21
use CultuurNet\UDB3\Role\ValueObjects\Query;
22
use CultuurNet\UDB3\ValueObject\SapiVersion;
23
use ValueObjects\Identity\UUID;
24
use ValueObjects\StringLiteral\StringLiteral;
25
26
class DefaultRoleEditingService implements RoleEditingServiceInterface
27
{
28
    /**
29
     * @var CommandBusInterface
30
     */
31
    private $commandBus;
32
33
    /**
34
     * @var UuidGeneratorInterface
35
     */
36
    private $uuidGenerator;
37
38
    /**
39
     * @var RepositoryInterface
40
     */
41
    private $writeRepository;
42
43
    /**
44
     * DefaultRoleEditingService constructor.
45
     *
46
     * @param CommandBusInterface $commandBus
47
     * @param UuidGeneratorInterface $uuidGenerator
48
     * @param RepositoryInterface $writeRepository
49
     */
50
    public function __construct(
51
        CommandBusInterface $commandBus,
52
        UuidGeneratorInterface $uuidGenerator,
53
        RepositoryInterface $writeRepository
54
    ) {
55
        $this->commandBus = $commandBus;
56
        $this->uuidGenerator = $uuidGenerator;
57
        $this->writeRepository = $writeRepository;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function create(StringLiteral $name)
64
    {
65
        $uuid = new UUID($this->uuidGenerator->generate());
66
67
        $role = Role::create($uuid, $name);
68
69
        $this->writeRepository->save($role);
70
71
        return $uuid->toNative();
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function rename(UUID $uuid, StringLiteral $name)
78
    {
79
        $command = new RenameRole(
80
            $uuid,
81
            $name
82
        );
83
84
        return $this->commandBus->dispatch($command);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function addPermission(UUID $uuid, Permission $permission)
91
    {
92
        $command = new AddPermission(
93
            $uuid,
94
            $permission
95
        );
96
97
        return $this->commandBus->dispatch($command);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function removePermission(UUID $uuid, Permission $permission)
104
    {
105
        $command = new RemovePermission(
106
            $uuid,
107
            $permission
108
        );
109
110
        return $this->commandBus->dispatch($command);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function addUser(UUID $uuid, StringLiteral $userId)
117
    {
118
        $command = new AddUser(
119
            $uuid,
120
            $userId
121
        );
122
123
        return $this->commandBus->dispatch($command);
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function removeUser(UUID $uuid, StringLiteral $userId)
130
    {
131
        $command = new RemoveUser(
132
            $uuid,
133
            $userId
134
        );
135
136
        return $this->commandBus->dispatch($command);
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function setConstraint(UUID $uuid, StringLiteral $query)
143
    {
144
        $command = new SetConstraint(
145
            $uuid,
146
            $query
147
        );
148
149
        return $this->commandBus->dispatch($command);
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    public function addConstraint(UUID $uuid, SapiVersion $sapiVersion, Query $query) {
156
        $command = new AddConstraint(
157
            $uuid,
158
            $sapiVersion,
159
            $query
160
        );
161
162
        return $this->commandBus->dispatch($command);
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168
    public function updateConstraint(UUID $uuid, SapiVersion $sapiVersion, Query $query) {
169
        $command = new UpdateConstraint(
170
            $uuid,
171
            $sapiVersion,
172
            $query
173
        );
174
175
        return $this->commandBus->dispatch($command);
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181
    public function addLabel(UUID $uuid, UUID $labelId)
182
    {
183
        $command = new AddLabel(
184
            $uuid,
185
            $labelId
186
        );
187
188
        return $this->commandBus->dispatch($command);
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function removeLabel(UUID $uuid, UUID $labelId)
195
    {
196
        $command = new RemoveLabel(
197
            $uuid,
198
            $labelId
199
        );
200
201
        return $this->commandBus->dispatch($command);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function delete(UUID $uuid)
208
    {
209
        $command = new DeleteRole(
210
            $uuid
211
        );
212
213
        return $this->commandBus->dispatch($command);
214
    }
215
}
216