|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\Filesystem; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\Filesystem; |
|
15
|
|
|
use Balloon\Filesystem\Node\AttributeDecorator as NodeAttributeDecorator; |
|
16
|
|
|
use Balloon\Filesystem\Node\NodeInterface; |
|
17
|
|
|
use Balloon\Server; |
|
18
|
|
|
use Balloon\Server\AttributeDecorator as RoleAttributeDecorator; |
|
19
|
|
|
use Closure; |
|
20
|
|
|
|
|
21
|
|
|
class EventAttributeDecorator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Server. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Server |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $server; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Filesystem. |
|
32
|
|
|
* |
|
33
|
|
|
* @var Filesystem |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $fs; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Node decorator. |
|
39
|
|
|
* |
|
40
|
|
|
* @var NodeAttributeDecorator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $node_decorator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Role decorator. |
|
46
|
|
|
* |
|
47
|
|
|
* @var RoleAttributeDecorator |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $role_decorator; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Custom attributes. |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $custom = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Init. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Server $server |
|
62
|
|
|
* @param NodeAttributeDecorator $node_decorator |
|
63
|
|
|
* @param RoleAttributeDecorator $role_decorator; |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(Server $server, NodeAttributeDecorator $node_decorator, RoleAttributeDecorator $role_decorator) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->server = $server; |
|
68
|
|
|
$this->fs = $server->getFilesystem(); |
|
69
|
|
|
$this->node_decorator = $node_decorator; |
|
70
|
|
|
$this->role_decorator = $role_decorator; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Decorate attributes. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $event |
|
77
|
|
|
* @param array $attributes |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function decorate(array $event, array $attributes = []): array |
|
82
|
|
|
{ |
|
83
|
|
|
$requested = $attributes; |
|
84
|
|
|
|
|
85
|
|
|
$attrs = array_merge( |
|
86
|
|
|
$this->getAttributes($event), |
|
87
|
|
|
$this->custom |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
if (0 === count($requested)) { |
|
91
|
|
|
return $this->translateAttributes($event, $attrs); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->translateAttributes($event, array_intersect_key($attrs, array_flip($requested))); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Add decorator. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $attribute |
|
101
|
|
|
* @param Closure $decorator |
|
102
|
|
|
* |
|
103
|
|
|
* @return AttributeDecorator |
|
104
|
|
|
*/ |
|
105
|
|
|
public function addDecorator(string $attribute, Closure $decorator): self |
|
106
|
|
|
{ |
|
107
|
|
|
$this->custom[$attribute] = $decorator; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get Attributes. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $event |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getAttributes(array $event): array |
|
120
|
|
|
{ |
|
121
|
|
|
return [ |
|
122
|
|
|
'event' => (string) $event['_id'], |
|
123
|
|
|
'timestamp' => $event['timestamp']->toDateTime()->format('c'), |
|
124
|
|
|
'operation' => $event['operation'], |
|
125
|
|
|
'name' => $event['name'], |
|
126
|
|
|
'client' => isset($event['client']) ? $event['client'] : null, |
|
127
|
|
|
'previous' => $this->getPrevious($event), |
|
128
|
|
|
'node' => $this->getNode($event), |
|
129
|
|
|
'share' => $this->getShare($event), |
|
130
|
|
|
'parent' => $this->getParent($event), |
|
131
|
|
|
'user' => $this->getUser($event), |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get Attributes. |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $event |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getPrevious(array $event): ?array |
|
143
|
|
|
{ |
|
144
|
|
|
if (!isset($event['previous'])) { |
|
145
|
|
|
return null; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$previous = $event['previous']; |
|
149
|
|
|
|
|
150
|
|
|
if (isset($previous['parent'])) { |
|
151
|
|
|
if ($previous['parent'] === null) { |
|
152
|
|
|
$previous['parent'] = [ |
|
153
|
|
|
'id' => null, |
|
154
|
|
|
'name' => null, |
|
155
|
|
|
]; |
|
156
|
|
|
} else { |
|
157
|
|
|
try { |
|
158
|
|
|
$node = $this->fs->findNodeById($previous['parent'], null, NodeInterface::DELETED_INCLUDE); |
|
159
|
|
|
$previous['parent'] = $this->node_decorator->decorate($node, ['id', 'name', '_links']); |
|
160
|
|
|
} catch (\Exception $e) { |
|
161
|
|
|
$previous['parent'] = null; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $previous; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get Attributes. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $event |
|
173
|
|
|
* |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function getNode(array $event): ?array |
|
177
|
|
|
{ |
|
178
|
|
|
try { |
|
179
|
|
|
$node = $this->fs->findNodeById($event['node'], null, NodeInterface::DELETED_INCLUDE); |
|
180
|
|
|
|
|
181
|
|
|
return $this->node_decorator->decorate($node, ['id', 'name', 'deleted', '_links']); |
|
182
|
|
|
} catch (\Exception $e) { |
|
183
|
|
|
return null; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get Attributes. |
|
189
|
|
|
* |
|
190
|
|
|
* @param array $event |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function getParent(array $event): ?array |
|
195
|
|
|
{ |
|
196
|
|
|
try { |
|
197
|
|
|
if (null === $event['parent']) { |
|
198
|
|
|
return [ |
|
199
|
|
|
'id' => null, |
|
200
|
|
|
'name' => null, |
|
201
|
|
|
]; |
|
202
|
|
|
} |
|
203
|
|
|
$node = $this->fs->findNodeById($event['parent'], null, NodeInterface::DELETED_INCLUDE); |
|
204
|
|
|
|
|
205
|
|
|
return $this->node_decorator->decorate($node, ['id', 'name', 'deleted', '_links']); |
|
206
|
|
|
} catch (\Exception $e) { |
|
207
|
|
|
return null; |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get Attributes. |
|
213
|
|
|
* |
|
214
|
|
|
* @param array $event |
|
215
|
|
|
* |
|
216
|
|
|
* @return array |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function getUser(array $event): ?array |
|
219
|
|
|
{ |
|
220
|
|
|
try { |
|
221
|
|
|
$user = $this->fs->getServer()->getUserById($event['owner']); |
|
222
|
|
|
|
|
223
|
|
|
return $this->role_decorator->decorate($user, ['id', 'name', '_links']); |
|
224
|
|
|
} catch (\Exception $e) { |
|
225
|
|
|
return null; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get Attributes. |
|
231
|
|
|
* |
|
232
|
|
|
* @param array $event |
|
233
|
|
|
* |
|
234
|
|
|
* @return array |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function getShare(array $event): ?array |
|
237
|
|
|
{ |
|
238
|
|
|
try { |
|
239
|
|
|
if (isset($event['share']) && false === $event['share'] || !isset($event['share'])) { |
|
240
|
|
|
return null; |
|
241
|
|
|
} |
|
242
|
|
|
$node = $this->fs->findNodeById($event['share'], null, NodeInterface::DELETED_INCLUDE); |
|
243
|
|
|
|
|
244
|
|
|
return $this->node_decorator->decorate($node, ['id', 'name', 'deleted']); |
|
245
|
|
|
} catch (\Exception $e) { |
|
246
|
|
|
return null; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Execute closures. |
|
252
|
|
|
* |
|
253
|
|
|
* @param NodeInterface |
|
254
|
|
|
* @param array $attributes |
|
255
|
|
|
* |
|
256
|
|
|
* @return array |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function translateAttributes(array $event, array $attributes): array |
|
259
|
|
|
{ |
|
260
|
|
|
foreach ($attributes as $key => &$value) { |
|
261
|
|
|
if ($value instanceof Closure) { |
|
262
|
|
|
$result = $value->call($this, $event); |
|
263
|
|
|
|
|
264
|
|
|
if (null === $result) { |
|
265
|
|
|
unset($attributes[$key]); |
|
266
|
|
|
} else { |
|
267
|
|
|
$value = $result; |
|
268
|
|
|
} |
|
269
|
|
|
} elseif ($value === null) { |
|
270
|
|
|
unset($attributes[$key]); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return $attributes; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.