Completed
Pull Request — master (#394)
by Luc
24:49
created

CommandHandler::handleUpdateConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
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)
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)
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
    public function handleRemoveConstraint(RemoveConstraint $removeConstraint)
112
    {
113
        $role = $this->load($removeConstraint->getUuid());
114
115
        $role->removeConstraint(
116
            $removeConstraint->getSapiVersion()
117
        );
118
119
        $this->save($role);
120
    }
121
122
    /**
123
     * @param AddPermission $addPermission
124
     */
125
    public function handleAddPermission(AddPermission $addPermission)
126
    {
127
        $role = $this->load($addPermission->getUuid());
128
129
        $role->addPermission(
130
            $addPermission->getUuid(),
131
            $addPermission->getRolePermission()
132
        );
133
134
        $this->save($role);
135
    }
136
137
    /**
138
     * @param RemovePermission $removePermission
139
     */
140
    public function handleRemovePermission(RemovePermission $removePermission)
141
    {
142
        $role = $this->load($removePermission->getUuid());
143
144
        $role->removePermission(
145
            $removePermission->getUuid(),
146
            $removePermission->getRolePermission()
147
        );
148
149
        $this->save($role);
150
    }
151
152
    /**
153
     * @param AddUser $addUser
154
     */
155
    public function handleAddUser(AddUser $addUser)
156
    {
157
        $role = $this->load($addUser->getUuid());
158
159
        $role->addUser(
160
            $addUser->getUserId()
161
        );
162
163
        $this->save($role);
164
    }
165
166
    /**
167
     * @param RemoveUser $removeUser
168
     */
169
    public function handleRemoveUser(RemoveUser $removeUser)
170
    {
171
        $role = $this->load($removeUser->getUuid());
172
173
        $role->removeUser(
174
            $removeUser->getUserId()
175
        );
176
177
        $this->save($role);
178
    }
179
180
    /**
181
     * @param DeleteRole $deleteRole
182
     */
183
    public function handleDeleteRole(DeleteRole $deleteRole)
184
    {
185
        $role = $this->load($deleteRole->getUuid());
186
187
        //@TODO Check linked users and labels once added.
188
189
        $role->delete($deleteRole->getUuid());
190
191
        $this->save($role);
192
    }
193
194
    /**
195
     * @param AddLabel $addLabel
196
     */
197
    public function handleAddLabel(AddLabel $addLabel)
198
    {
199
        $role = $this->load($addLabel->getUuid());
200
201
        $role->addLabel(
202
            $addLabel->getLabelId()
203
        );
204
205
        $this->save($role);
206
    }
207
208
    /**
209
     * @param RemoveLabel $removeLabel
210
     */
211
    public function handleRemoveLabel(RemoveLabel $removeLabel)
212
    {
213
        $role = $this->load($removeLabel->getUuid());
214
215
        $role->removeLabel(
216
            $removeLabel->getLabelId()
217
        );
218
219
        $this->save($role);
220
    }
221
222
    /**
223
     * @param UUID $uuid
224
     * @return Role
225
     */
226
    private function load(UUID $uuid)
227
    {
228
        /** @var Role $role */
229
        $role = $this->repository->load($uuid);
230
        return $role;
231
    }
232
233
    /**
234
     * @param Role $role
235
     */
236
    private function save(Role $role)
237
    {
238
        $this->repository->save($role);
239
    }
240
}
241