Completed
Pull Request — master (#394)
by Kristof
04:20
created

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