Completed
Branch dev (d5d70c)
by Raffael
11:00
created

AttributeDecorator::decorate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
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\Node;
13
14
use Balloon\Filesystem;
15
use Balloon\Filesystem\Acl;
16
use Balloon\Server;
17
use Balloon\Server\AttributeDecorator as RoleAttributeDecorator;
18
use Closure;
19
20
class AttributeDecorator
21
{
22
    /**
23
     * Server.
24
     *
25
     * @var Server
26
     */
27
    protected $server;
28
29
    /**
30
     * Filesystem.
31
     *
32
     * @var Filesystem
33
     */
34
    protected $fs;
35
36
    /**
37
     * Acl.
38
     *
39
     * @var Acl
40
     */
41
    protected $acl;
42
43
    /**
44
     * Role decorator.
45
     *
46
     * @var RoleAttributeDecorator
47
     */
48
    protected $role_decorator;
49
50
    /**
51
     * Custom attributes.
52
     *
53
     * @var array
54
     */
55
    protected $custom = [];
56
57
    /**
58
     * Init.
59
     *
60
     * @param Server    $server
61
     * @param Acl       $acl
62
     * @param Decorator $role_decorator
63
     */
64
    public function __construct(Server $server, Acl $acl, RoleAttributeDecorator $role_decorator)
65
    {
66
        $this->server = $server;
67
        $this->acl = $acl;
68
        $this->role_decorator = $role_decorator;
69
    }
70
71
    /**
72
     * Decorate attributes.
73
     *
74
     * @param NodeInterface $node
75
     * @param array         $attributes
76
     *
77
     * @return array
78
     */
79
    public function decorate(NodeInterface $node, array $attributes = []): array
80
    {
81
        $requested = $attributes;
82
        $attributes = $node->getAttributes();
83
84
        $attrs = array_merge(
85
            $this->getAttributes($node, $attributes),
86
            $this->getTimeAttributes($node, $attributes),
87
            $this->getTypeAttributes($node, $attributes),
88
            $this->custom
89
        );
90
91
        if (0 === count($requested)) {
92
            return $this->translateAttributes($node, $attrs);
93
        }
94
95
        return $this->translateAttributes($node, array_intersect_key($attrs, array_flip($requested)));
96
    }
97
98
    /**
99
     * Add decorator.
100
     *
101
     * @param string  $attribute
102
     * @param Closure $decorator
103
     *
104
     * @return AttributeDecorator
105
     */
106
    public function addDecorator(string $attribute, Closure $decorator): self
107
    {
108
        $this->custom[$attribute] = $decorator;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Get Attributes.
115
     *
116
     * @param NodeInterface
117
     *
118
     * @return array
119
     */
120
    protected function getAttributes(NodeInterface $node, array $attributes): array
121
    {
122
        $acl = $this->acl;
123
        $server = $this->server;
124
        $fs = $this->server->getFilesystem();
125
        $decorator = $this->role_decorator;
126
127
        return [
128
            'id' => (string) $attributes['_id'],
129
            'name' => (string) $attributes['name'],
130
            'mime' => (string) $attributes['mime'],
131
            'readonly' => (bool) $attributes['readonly'],
132
            'directory' => $node instanceof Collection,
133
            'meta' => function ($node) {
134
                return (object) $node->getMetaAttributes();
135
            },
136
            'size' => function ($node) {
137
                return $node->getSize();
138
            },
139
            'path' => function ($node) {
140
                try {
141
                    return $node->getPath();
142
                } catch (\Exception $e) {
143
                    return null;
144
                }
145
            },
146
            'parent' => function ($node) {
147
                $parent = $node->getParent();
148
149
                if (null === $parent || $parent->isRoot()) {
150
                    return null;
151
                }
152
153
                return $this->decorate($node->getParent(), ['id', 'name', '_links']);
154
            },
155
            'access' => function ($node) use ($acl) {
156
                return $acl->getAclPrivilege($node);
157
            },
158
            'share' => function ($node) {
159
                if ($node->isShared() || !$node->isSpecial()) {
160
                    return null;
161
                }
162
163
                try {
164
                    return $this->decorate($node->getShareNode(), ['id', 'name', '_links']);
165
                } catch (\Exception $e) {
166
                    return null;
167
                }
168
            },
169
            'sharename' => function ($node) {
170
                if (!$node->isShared()) {
171
                    return null;
172
                }
173
174
                try {
175
                    return $node->getShareName();
176
                } catch (\Exception $e) {
177
                    return null;
178
                }
179
            },
180
            'shareowner' => function ($node) use ($server, $fs, $decorator) {
181
                if (!$node->isSpecial()) {
182
                    return null;
183
                }
184
185
                try {
186
                    return $decorator->decorate(
187
                        $server->getUserById($fs->findRawNode($node->getShareId())['owner']),
188
                        ['id', 'name', '_links']
189
                    );
190
                } catch (\Exception $e) {
191
                    return null;
192
                }
193
            },
194
            'owner' => function ($node) use ($server, $decorator) {
195
                try {
196
                    return $decorator->decorate(
197
                        $server->getUserById($node->getOwner()),
198
                        ['id', 'name', '_links']
199
                    );
200
                } catch (\Exception $e) {
201
                    return null;
202
                }
203
            },
204
        ];
205
    }
206
207
    /**
208
     * Get Attributes.
209
     *
210
     * @param NodeInterface
211
     * @param array $attributes
212
     *
213
     * @return array
214
     */
215
    protected function getTimeAttributes(NodeInterface $node, array $attributes): array
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
216
    {
217
        return [
218
            'created' => function ($node) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
                return $attributes['created']->toDateTime()->format('c');
220
            },
221
            'changed' => function ($node) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
222
                return $attributes['changed']->toDateTime()->format('c');
223
            },
224
            'deleted' => function ($node) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
                if (false === $attributes['deleted']) {
226
                    return null;
227
                }
228
229
                return $attributes['deleted']->toDateTime()->format('c');
230
            },
231
            'destroy' => function ($node) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
232
                if (null === $attributes['destroy']) {
233
                    return null;
234
                }
235
236
                return $attributes['destroy']->toDateTime()->format('c');
237
            },
238
        ];
239
    }
240
241
    /**
242
     * Get Attributes.
243
     *
244
     * @param NodeInterface
245
     * @param array $attributes
246
     *
247
     * @return array
248
     */
249
    protected function getTypeAttributes(NodeInterface $node, array $attributes): array
250
    {
251
        $server = $this->server;
0 ignored issues
show
Unused Code introduced by
$server is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
252
        $fs = $this->server->getFilesystem();
0 ignored issues
show
Unused Code introduced by
$fs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
253
254
        if ($node instanceof File) {
255
            return [
256
                'version' => $attributes['version'],
257
                'hash' => $attributes['hash'],
258
            ];
259
        }
260
261
        return [
262
            'shared' => $node->isShared(),
263
            'reference' => $node->isReference(),
264
            'filter' => function ($node) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
265
                if (null === $attributes['filter']) {
266
                    return null;
267
                }
268
269
                return json_decode($attributes['filter']);
270
            },
271
        ];
272
    }
273
274
    /**
275
     * Execute closures.
276
     *
277
     * @param NodeInterface
278
     * @param array $attributes
279
     *
280
     * @return array
281
     */
282 View Code Duplication
    protected function translateAttributes(NodeInterface $node, array $attributes): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
283
    {
284
        foreach ($attributes as $key => &$value) {
285
            if ($value instanceof Closure) {
286
                $result = $value->call($this, $node);
287
288
                if (null === $result) {
289
                    unset($attributes[$key]);
290
                } else {
291
                    $value = $result;
292
                }
293
            } elseif ($value === null) {
294
                unset($attributes[$key]);
295
            }
296
        }
297
298
        return $attributes;
299
    }
300
}
301