Completed
Pull Request — master (#394)
by
unknown
16:12
created

CommandHandler::handleAddUser()   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\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 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 SetConstraint $setConstraint
67
     */
68
    public function handleSetConstraint(SetConstraint $setConstraint)
69
    {
70
        $role = $this->load($setConstraint->getUuid());
71
72
        $role->setConstraint(
73
            $setConstraint->getUuid(),
74
            $setConstraint->getQuery()
75
        );
76
77
        $this->save($role);
78
    }
79
80
    /**
81
     * @param AddConstraint $addConstraint
82
     */
83
    public function handleAddConstraint(AddConstraint $addConstraint)
84
    {
85
        $role = $this->load($addConstraint->getUuid());
86
87
        $role->addConstraint(
88
            $addConstraint->getSapiVersion(),
89
            $addConstraint->getQuery()
90
        );
91
92
        $this->save($role);
93
    }
94
95
    /**
96
     * @param UpdateConstraint $updateConstraint
97
     */
98
    public function handleUpdateConstraint(UpdateConstraint $updateConstraint)
99
    {
100
        $role = $this->load($updateConstraint->getUuid());
101
102
        $role->updateConstraint(
103
            $updateConstraint->getSapiVersion(),
104
            $updateConstraint->getQuery()
105
        );
106
107
        $this->save($role);
108
    }
109
110
    /**
111
     * @param AddPermission $addPermission
112
     */
113
    public function handleAddPermission(AddPermission $addPermission)
114
    {
115
        $role = $this->load($addPermission->getUuid());
116
117
        $role->addPermission(
118
            $addPermission->getUuid(),
119
            $addPermission->getRolePermission()
120
        );
121
122
        $this->save($role);
123
    }
124
125
    /**
126
     * @param RemovePermission $removePermission
127
     */
128
    public function handleRemovePermission(RemovePermission $removePermission)
129
    {
130
        $role = $this->load($removePermission->getUuid());
131
132
        $role->removePermission(
133
            $removePermission->getUuid(),
134
            $removePermission->getRolePermission()
135
        );
136
137
        $this->save($role);
138
    }
139
140
    /**
141
     * @param AddUser $addUser
142
     */
143
    public function handleAddUser(AddUser $addUser)
144
    {
145
        $role = $this->load($addUser->getUuid());
146
147
        $role->addUser(
148
            $addUser->getUserId()
149
        );
150
151
        $this->save($role);
152
    }
153
154
    /**
155
     * @param RemoveUser $removeUser
156
     */
157
    public function handleRemoveUser(RemoveUser $removeUser)
158
    {
159
        $role = $this->load($removeUser->getUuid());
160
161
        $role->removeUser(
162
            $removeUser->getUserId()
163
        );
164
165
        $this->save($role);
166
    }
167
168
    /**
169
     * @param DeleteRole $deleteRole
170
     */
171
    public function handleDeleteRole(DeleteRole $deleteRole)
172
    {
173
        $role = $this->load($deleteRole->getUuid());
174
175
        //@TODO Check linked users and labels once added.
176
177
        $role->delete($deleteRole->getUuid());
178
179
        $this->save($role);
180
    }
181
182
    /**
183
     * @param AddLabel $addLabel
184
     */
185
    public function handleAddLabel(AddLabel $addLabel)
186
    {
187
        $role = $this->load($addLabel->getUuid());
188
189
        $role->addLabel(
190
            $addLabel->getLabelId()
191
        );
192
193
        $this->save($role);
194
    }
195
196
    /**
197
     * @param RemoveLabel $removeLabel
198
     */
199
    public function handleRemoveLabel(RemoveLabel $removeLabel)
200
    {
201
        $role = $this->load($removeLabel->getUuid());
202
203
        $role->removeLabel(
204
            $removeLabel->getLabelId()
205
        );
206
207
        $this->save($role);
208
    }
209
210
    /**
211
     * @param UUID $uuid
212
     * @return Role
213
     */
214
    private function load(UUID $uuid)
215
    {
216
        /** @var Role $role */
217
        $role = $this->repository->load($uuid);
218
        return $role;
219
    }
220
221
    /**
222
     * @param Role $role
223
     */
224
    private function save(Role $role)
225
    {
226
        $this->repository->save($role);
227
    }
228
}
229