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\App\Convert; |
13
|
|
|
|
14
|
|
|
use Balloon\AttributeDecorator\AttributeDecoratorInterface; |
15
|
|
|
use Balloon\Filesystem\Node\AttributeDecorator as NodeAttributeDecorator; |
16
|
|
|
use Balloon\Server; |
17
|
|
|
use Closure; |
18
|
|
|
|
19
|
|
|
class AttributeDecorator implements AttributeDecoratorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Server. |
23
|
|
|
* |
24
|
|
|
* @var Server |
25
|
|
|
*/ |
26
|
|
|
protected $server; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Node decorator. |
30
|
|
|
* |
31
|
|
|
* @var NodeAttributeDecorator |
32
|
|
|
*/ |
33
|
|
|
protected $node_decorator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Custom attributes. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $custom = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Init. |
44
|
|
|
* |
45
|
|
|
* @param Server $server |
46
|
|
|
* @param NodeAttributeDecorator $node_decorator |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Server $server, NodeAttributeDecorator $node_decorator) |
49
|
|
|
{ |
50
|
|
|
$this->server = $server; |
51
|
|
|
$this->node_decorator = $node_decorator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Decorate attributes. |
56
|
|
|
* |
57
|
|
|
* @param array $slave |
58
|
|
|
* @param array $attributes |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function decorate(array $slave, array $attributes = []): array |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$requested = $attributes; |
65
|
|
|
$attrs = $this->getAttributes($slave); |
66
|
|
|
|
67
|
|
|
if (0 === count($requested)) { |
68
|
|
|
return $this->translateAttributes($slave, $attrs); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->translateAttributes($slave, array_intersect_key($attrs, array_flip($requested))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add decorator. |
76
|
|
|
* |
77
|
|
|
* @param string $attribute |
78
|
|
|
* @param Closure $decorator |
79
|
|
|
* |
80
|
|
|
* @return AttributeDecorator |
81
|
|
|
*/ |
82
|
|
|
public function addDecorator(string $attribute, Closure $decorator): self |
83
|
|
|
{ |
84
|
|
|
$this->custom[$attribute] = $decorator; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get Attributes. |
91
|
|
|
* |
92
|
|
|
* @param array $slave |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getAttributes(array $slave): array |
97
|
|
|
{ |
98
|
|
|
$fs = $this->server->getFilesystem(); |
99
|
|
|
$node_decorator = $this->node_decorator; |
100
|
|
|
|
101
|
|
|
return [ |
102
|
|
|
'id' => (string) $slave['_id'], |
103
|
|
|
'format' => (string) $slave['format'], |
104
|
|
|
'master' => function ($slave) use ($fs, $node_decorator) { |
105
|
|
|
try { |
106
|
|
|
return $node_decorator->decorate($fs->findNodeById($slave['master']), ['_links', 'id', 'name']); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
}, |
111
|
|
|
'slave' => function ($slave) use ($fs, $node_decorator) { |
112
|
|
|
if (!isset($slave['slave'])) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
return $node_decorator->decorate($fs->findNodeById($slave['slave']), ['_links', 'id', 'name']); |
118
|
|
|
} catch (\Exception $e) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
}, |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Execute closures. |
127
|
|
|
* |
128
|
|
|
* @param array $slave |
129
|
|
|
* @param array $attributes |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
protected function translateAttributes(array $slave, array $attributes): array |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
foreach ($attributes as $key => &$value) { |
136
|
|
|
if ($value instanceof Closure) { |
137
|
|
|
$result = $value->call($this, $slave); |
138
|
|
|
|
139
|
|
|
if (null === $result) { |
140
|
|
|
unset($attributes[$key]); |
141
|
|
|
} else { |
142
|
|
|
$value = $result; |
143
|
|
|
} |
144
|
|
|
} elseif ($value === null) { |
145
|
|
|
unset($attributes[$key]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $attributes; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.