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

DefaultRoleEditingService::addUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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