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

DeltaDecorator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 2
dl 0
loc 107
rs 10
c 0
b 0
f 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A decorate() 0 8 2
A addDecorator() 0 6 1
A getAttributes() 0 10 1
A dateTimeToUnix() 0 13 2
B translateAttributes() 0 18 5
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\Api\v1\AttributeDecorator;
13
14
use Balloon\Filesystem\Node\NodeInterface;
15
use Closure;
16
use MongoDB\BSON\UTCDateTime;
17
use stdClass;
18
19
class DeltaDecorator
20
{
21
    /**
22
     * Custom attributes.
23
     *
24
     * @var array
25
     */
26
    protected $custom = [];
27
28
    /**
29
     * Decorate attributes.
30
     *
31
     * @param array $node
32
     * @param array $attributes
33
     *
34
     * @return array
35
     */
36
    public function decorate($node, array $attributes = []): array
37
    {
38
        if (0 === count($attributes)) {
39
            return $this->translateAttributes($node, $this->getAttributes($node));
40
        }
41
42
        return $this->translateAttributes($node, array_intersect_key($this->getAttributes($node), array_flip($attributes)));
43
    }
44
45
    /**
46
     * Add decorator.
47
     *
48
     * @param string  $attribute
49
     * @param Closure $decorator
50
     *
51
     * @return AttributeDecorator
52
     */
53
    public function addDecorator(string $attribute, Closure $decorator): self
54
    {
55
        $this->custom[$attribute] = $decorator;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get Attributes.
62
     *
63
     * @param NodeInterface
64
     *
65
     * @return array
66
     */
67
    protected function getAttributes(array $node): array
68
    {
69
        return [
70
            'id' => (string) $node['id'],
71
            'deleted' => $node['deleted'],
72
            'changed' => $this->dateTimeToUnix($node['changed']),
73
            'path' => $node['path'],
74
            'directory' => $node['directory'],
75
        ];
76
    }
77
78
    /**
79
     * Convert UTCDateTime to unix ts.
80
     *
81
     * @param UTCDateTime $date
82
     *
83
     * @return stdClass
84
     */
85
    protected function dateTimeToUnix(?UTCDateTime $date): ?stdClass
86
    {
87
        if (null === $date) {
88
            return null;
89
        }
90
91
        $date = $date->toDateTime();
92
        $ts = new stdClass();
93
        $ts->sec = $date->format('U');
94
        $ts->usec = $date->format('u');
95
96
        return $ts;
97
    }
98
99
    /**
100
     * Execute closures.
101
     *
102
     * @param array $node
103
     * @param array $attributes
104
     *
105
     * @return array
106
     */
107
    protected function translateAttributes(array $node, array $attributes): array
108
    {
109
        foreach ($attributes as $key => &$value) {
110
            if ($value instanceof Closure) {
111
                $result = $value->call($this, $node);
112
113
                if (null === $result) {
114
                    unset($attributes[$key]);
115
                } else {
116
                    $value = $result;
117
                }
118
            } elseif ($value === null) {
119
                unset($attributes[$key]);
120
            }
121
        }
122
123
        return $attributes;
124
    }
125
}
126