1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Role\ReadModel\Detail; |
4
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\Role\Events\ConstraintAdded; |
6
|
|
|
use CultuurNet\UDB3\Role\Events\ConstraintRemoved; |
7
|
|
|
use CultuurNet\UDB3\Role\Events\ConstraintUpdated; |
8
|
|
|
use CultuurNet\UDB3\Role\Events\PermissionAdded; |
9
|
|
|
use CultuurNet\UDB3\Role\Events\PermissionRemoved; |
10
|
|
|
use CultuurNet\UDB3\Role\Events\RoleCreated; |
11
|
|
|
use CultuurNet\UDB3\Role\Events\RoleDeleted; |
12
|
|
|
use CultuurNet\UDB3\Role\Events\RoleRenamed; |
13
|
|
|
use CultuurNet\UDB3\Role\ReadModel\RoleProjector; |
14
|
|
|
|
15
|
|
|
class Projector extends RoleProjector |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param RoleCreated $roleCreated |
19
|
|
|
*/ |
20
|
|
|
protected function applyRoleCreated(RoleCreated $roleCreated) |
21
|
|
|
{ |
22
|
|
|
$this->saveNewDocument( |
23
|
|
|
$roleCreated->getUuid()->toNative(), |
24
|
|
|
function (\stdClass $json) use ($roleCreated) { |
25
|
|
|
$json->uuid = $roleCreated->getUuid()->toNative(); |
26
|
|
|
$json->name = $roleCreated->getName()->toNative(); |
27
|
|
|
$json->permissions = []; |
28
|
|
|
return $json; |
29
|
|
|
} |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RoleRenamed $roleRenamed |
35
|
|
|
*/ |
36
|
|
|
protected function applyRoleRenamed(RoleRenamed $roleRenamed) |
37
|
|
|
{ |
38
|
|
|
$document = $this->loadDocumentFromRepositoryByUuid( |
39
|
|
|
$roleRenamed->getUuid()->toNative() |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$json = $document->getBody(); |
43
|
|
|
$json->name = $roleRenamed->getName()->toNative(); |
44
|
|
|
|
45
|
|
|
$this->repository->save($document->withBody($json)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param RoleDeleted $roleDeleted |
50
|
|
|
*/ |
51
|
|
|
protected function applyRoleDeleted(RoleDeleted $roleDeleted) |
52
|
|
|
{ |
53
|
|
|
$this->repository->remove($roleDeleted->getUuid()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ConstraintAdded $constraintAdded |
58
|
|
|
*/ |
59
|
|
|
protected function applyConstraintAdded( |
60
|
|
|
ConstraintAdded $constraintAdded |
61
|
|
|
) { |
62
|
|
|
$document = $this->loadDocumentFromRepositoryByUuid( |
63
|
|
|
$constraintAdded->getUuid()->toNative() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$json = $document->getBody(); |
67
|
|
|
$json->constraint = $constraintAdded->getQuery()->toNative(); |
68
|
|
|
if (empty($json->constraints)) { |
69
|
|
|
$json->constraints = new \stdClass(); |
70
|
|
|
} |
71
|
|
|
$json->constraints->{$constraintAdded->getSapiVersion()->toNative()} = $constraintAdded->getQuery()->toNative(); |
72
|
|
|
|
73
|
|
|
$this->repository->save($document->withBody($json)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ConstraintUpdated $constraintUpdated |
78
|
|
|
*/ |
79
|
|
|
protected function applyConstraintUpdated( |
80
|
|
|
ConstraintUpdated $constraintUpdated |
81
|
|
|
) { |
82
|
|
|
$document = $this->loadDocumentFromRepositoryByUuid( |
83
|
|
|
$constraintUpdated->getUuid()->toNative() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$json = $document->getBody(); |
87
|
|
|
$json->constraint = $constraintUpdated->getQuery()->toNative(); |
88
|
|
|
$json->constraints->{$constraintUpdated->getSapiVersion()->toNative()} = $constraintUpdated->getQuery()->toNative(); |
89
|
|
|
|
90
|
|
|
$this->repository->save($document->withBody($json)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param ConstraintRemoved $constraintRemoved |
95
|
|
|
*/ |
96
|
|
|
protected function applyConstraintRemoved( |
97
|
|
|
ConstraintRemoved $constraintRemoved |
98
|
|
|
) { |
99
|
|
|
$document = $this->loadDocumentFromRepositoryByUuid( |
100
|
|
|
$constraintRemoved->getUuid()->toNative() |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$json = $document->getBody(); |
104
|
|
|
$json->constraint = null; |
105
|
|
|
$json->constraints->{$constraintRemoved->getSapiVersion()->toNative()} = null; |
106
|
|
|
|
107
|
|
|
$this->repository->save($document->withBody($json)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param PermissionAdded $permissionAdded |
112
|
|
|
*/ |
113
|
|
|
public function applyPermissionAdded(PermissionAdded $permissionAdded) |
114
|
|
|
{ |
115
|
|
|
$document = $this->loadDocumentFromRepositoryByUuid( |
116
|
|
|
$permissionAdded->getUuid()->toNative() |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$permission = $permissionAdded->getPermission(); |
120
|
|
|
|
121
|
|
|
$json = $document->getBody(); |
122
|
|
|
|
123
|
|
|
$permissions = property_exists($json, 'permissions') ? $json->permissions : []; |
124
|
|
|
array_push($permissions, $permission->getName()); |
125
|
|
|
|
126
|
|
|
$json->permissions = array_unique($permissions); |
127
|
|
|
|
128
|
|
|
$this->repository->save($document->withBody($json)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param PermissionRemoved $permissionRemoved |
133
|
|
|
*/ |
134
|
|
|
public function applyPermissionRemoved(PermissionRemoved $permissionRemoved) |
135
|
|
|
{ |
136
|
|
|
$document = $this->loadDocumentFromRepositoryByUuid( |
137
|
|
|
$permissionRemoved->getUuid()->toNative() |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$permission = $permissionRemoved->getPermission(); |
141
|
|
|
$permissionName = $permission->getName(); |
142
|
|
|
|
143
|
|
|
$json = $document->getBody(); |
144
|
|
|
$json->permissions = array_values( |
145
|
|
|
array_filter( |
146
|
|
|
$json->permissions, |
147
|
|
|
function ($item) use ($permissionName) { |
148
|
|
|
return $item !== $permissionName; |
149
|
|
|
} |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->repository->save($document->withBody($json)); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|