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

CommandHandler::handleRemoveConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Role;
4
5
use Broadway\Repository\RepositoryInterface;
6
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler as AbstractCommandHandler;
7
use CultuurNet\UDB3\Role\Commands\AddConstraint;
8
use CultuurNet\UDB3\Role\Commands\AddLabel;
9
use CultuurNet\UDB3\Role\Commands\AddPermission;
10
use CultuurNet\UDB3\Role\Commands\AddUser;
11
use CultuurNet\UDB3\Role\Commands\CreateRole;
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 ValueObjects\Identity\UUID;
21
22
class CommandHandler extends AbstractCommandHandler
23
{
24
    /**
25
     * @var RepositoryInterface
26
     */
27
    private $repository;
28
29
    /**
30
     * CommandHandler constructor.
31
     * @param RepositoryInterface $repository
32
     */
33
    public function __construct(RepositoryInterface $repository)
34
    {
35
        $this->repository = $repository;
36
    }
37
38
    /**
39
     * @param CreateRole $createRole
40
     */
41
    public function handleCreateRole(CreateRole $createRole)
42
    {
43
        $role = Role::create(
44
            $createRole->getUuid(),
45
            $createRole->getName()
46
        );
47
48
        $this->save($role);
49
    }
50
51
    /**
52
     * @param RenameRole $renameRole
53
     */
54
    public function handleRenameRole(RenameRole $renameRole)
55
    {
56
        $role = $this->load($renameRole->getUuid());
57
58
        $role->rename(
59
            $renameRole->getUuid(),
60
            $renameRole->getName()
61
        );
62
63
        $this->save($role);
64
    }
65
66
    /**
67
     * @param SetConstraint $setConstraint
68
     */
69
    public function handleSetConstraint(SetConstraint $setConstraint)
70
    {
71
        $role = $this->load($setConstraint->getUuid());
72
73
        $role->setConstraint(
74
            $setConstraint->getUuid(),
75
            $setConstraint->getQuery()
76
        );
77
78
        $this->save($role);
79
    }
80
81
    /**
82
     * @param AddConstraint $addConstraint
83
     */
84
    public function handleAddConstraint(AddConstraint $addConstraint): void
85
    {
86
        $role = $this->load($addConstraint->getUuid());
87
88
        $role->addConstraint(
89
            $addConstraint->getSapiVersion(),
90
            $addConstraint->getQuery()
91
        );
92
93
        $this->save($role);
94
    }
95
96
    /**
97
     * @param UpdateConstraint $updateConstraint
98
     */
99
    public function handleUpdateConstraint(UpdateConstraint $updateConstraint): void
100
    {
101
        $role = $this->load($updateConstraint->getUuid());
102
103
        $role->updateConstraint(
104
            $updateConstraint->getSapiVersion(),
105
            $updateConstraint->getQuery()
106
        );
107
108
        $this->save($role);
109
    }
110
111
    /**
112
     * @param RemoveConstraint $removeConstraint
113
     */
114
    public function handleRemoveConstraint(RemoveConstraint $removeConstraint): void
115
    {
116
        $role = $this->load($removeConstraint->getUuid());
117
118
        $role->removeConstraint(
119
            $removeConstraint->getSapiVersion()
120
        );
121
122
        $this->save($role);
123
    }
124
125
    /**
126
     * @param AddPermission $addPermission
127
     */
128
    public function handleAddPermission(AddPermission $addPermission)
129
    {
130
        $role = $this->load($addPermission->getUuid());
131
132
        $role->addPermission(
133
            $addPermission->getUuid(),
134
            $addPermission->getRolePermission()
135
        );
136
137
        $this->save($role);
138
    }
139
140
    /**
141
     * @param RemovePermission $removePermission
142
     */
143
    public function handleRemovePermission(RemovePermission $removePermission)
144
    {
145
        $role = $this->load($removePermission->getUuid());
146
147
        $role->removePermission(
148
            $removePermission->getUuid(),
149
            $removePermission->getRolePermission()
150
        );
151
152
        $this->save($role);
153
    }
154
155
    /**
156
     * @param AddUser $addUser
157
     */
158
    public function handleAddUser(AddUser $addUser)
159
    {
160
        $role = $this->load($addUser->getUuid());
161
162
        $role->addUser(
163
            $addUser->getUserId()
164
        );
165
166
        $this->save($role);
167
    }
168
169
    /**
170
     * @param RemoveUser $removeUser
171
     */
172
    public function handleRemoveUser(RemoveUser $removeUser)
173
    {
174
        $role = $this->load($removeUser->getUuid());
175
176
        $role->removeUser(
177
            $removeUser->getUserId()
178
        );
179
180
        $this->save($role);
181
    }
182
183
    /**
184
     * @param DeleteRole $deleteRole
185
     */
186
    public function handleDeleteRole(DeleteRole $deleteRole)
187
    {
188
        $role = $this->load($deleteRole->getUuid());
189
190
        //@TODO Check linked users and labels once added.
191
192
        $role->delete($deleteRole->getUuid());
193
194
        $this->save($role);
195
    }
196
197
    /**
198
     * @param AddLabel $addLabel
199
     */
200
    public function handleAddLabel(AddLabel $addLabel)
201
    {
202
        $role = $this->load($addLabel->getUuid());
203
204
        $role->addLabel(
205
            $addLabel->getLabelId()
206
        );
207
208
        $this->save($role);
209
    }
210
211
    /**
212
     * @param RemoveLabel $removeLabel
213
     */
214
    public function handleRemoveLabel(RemoveLabel $removeLabel)
215
    {
216
        $role = $this->load($removeLabel->getUuid());
217
218
        $role->removeLabel(
219
            $removeLabel->getLabelId()
220
        );
221
222
        $this->save($role);
223
    }
224
225
    /**
226
     * @param UUID $uuid
227
     * @return Role
228
     */
229
    private function load(UUID $uuid)
230
    {
231
        /** @var Role $role */
232
        $role = $this->repository->load($uuid);
233
        return $role;
234
    }
235
236
    /**
237
     * @param Role $role
238
     */
239
    private function save(Role $role)
240
    {
241
        $this->repository->save($role);
242
    }
243
}
244