Completed
Push — master ( 287393...7ff50d )
by Raffael
18:27 queued 14:12
created

src/app/Balloon.App.Convert/AttributeDecorator.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 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
    public function __construct(Server $server, NodeAttributeDecorator $node_decorator)
46
    {
47
        $this->server = $server;
48
        $this->node_decorator = $node_decorator;
49
    }
50
51
    /**
52
     * Decorate attributes.
53
     */
54 View Code Duplication
    public function decorate(array $slave, array $attributes = []): array
0 ignored issues
show
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...
55
    {
56
        $requested = $attributes;
57
        $attrs = $this->getAttributes($slave);
58
59
        if (0 === count($requested)) {
60
            return $this->translateAttributes($slave, $attrs);
61
        }
62
63
        return $this->translateAttributes($slave, array_intersect_key($attrs, array_flip($requested)));
64
    }
65
66
    /**
67
     * Add decorator.
68
     *
69
     *
70
     * @return AttributeDecorator
71
     */
72
    public function addDecorator(string $attribute, Closure $decorator): self
73
    {
74
        $this->custom[$attribute] = $decorator;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Get Attributes.
81
     */
82
    protected function getAttributes(array $slave): array
83
    {
84
        $fs = $this->server->getFilesystem();
85
        $node_decorator = $this->node_decorator;
86
87
        return [
88
            'id' => (string) $slave['_id'],
89
            'format' => (string) $slave['format'],
90
            'master' => function ($slave) use ($fs, $node_decorator) {
91
                try {
92
                    return $node_decorator->decorate($fs->findNodeById($slave['master']), ['_links', 'id', 'name']);
93
                } catch (\Exception $e) {
94
                    return null;
95
                }
96
            },
97 View Code Duplication
            'slave' => function ($slave) use ($fs, $node_decorator) {
0 ignored issues
show
This code seems to be duplicated across 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...
98
                if (!isset($slave['slave'])) {
99
                    return null;
100
                }
101
102
                try {
103
                    return $node_decorator->decorate($fs->findNodeById($slave['slave']), ['_links', 'id', 'name']);
104
                } catch (\Exception $e) {
105
                    return null;
106
                }
107
            },
108
        ];
109
    }
110
111
    /**
112
     * Execute closures.
113
     */
114 View Code Duplication
    protected function translateAttributes(array $slave, array $attributes): array
0 ignored issues
show
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...
115
    {
116
        foreach ($attributes as $key => &$value) {
117
            if ($value instanceof Closure) {
118
                $result = $value->call($this, $slave);
119
120
                if (null === $result) {
121
                    unset($attributes[$key]);
122
                } else {
123
                    $value = $result;
124
                }
125
            } elseif ($value === null) {
126
                unset($attributes[$key]);
127
            }
128
        }
129
130
        return $attributes;
131
    }
132
}
133