Passed
Push — master ( a9f547...49cca7 )
by Raffael
04:18
created

AttributeDecorator   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
dl 0
loc 175
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A decorate() 0 21 3
A addDecorator() 0 6 1
B getAttributes() 0 23 5
A getGroupAttributes() 0 12 3
D getUserAttributes() 0 34 9
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\Server;
13
14
use Balloon\AttributeDecorator\AttributeDecoratorInterface;
15
use Balloon\Server;
16
use Closure;
17
18
class AttributeDecorator implements AttributeDecoratorInterface
19
{
20
    /**
21
     * Server.
22
     *
23
     * @var Server
24
     */
25
    protected $server;
26
27
    /**
28
     * Custom attributes.
29
     *
30
     * @var array
31
     */
32
    protected $custom = [];
33
34
    /**
35
     * Init.
36
     */
37
    public function __construct(Server $server)
38
    {
39
        $this->server = $server;
40
    }
41
42
    /**
43
     * Decorate attributes.
44
     *
45
     * @param array $attributes
46
     */
47
    public function decorate(RoleInterface $role, ?array $attributes = null): array
48
    {
49
        if (null === $attributes) {
50
            $attributes = [];
51
        }
52
53
        $role_attributes = $role->getAttributes();
54
55
        $attrs = array_merge(
56
            $this->getAttributes($role, $role_attributes),
57
            $this->getUserAttributes($role, $role_attributes),
58
            $this->getGroupAttributes($role, $role_attributes),
59
            $this->custom
60
        );
61
62
        if (0 === count($attributes)) {
63
            return $this->translateAttributes($role, $attrs);
64
        }
65
66
        return $this->translateAttributes($role, array_intersect_key($attrs, array_flip($attributes)));
67
    }
68
69
    /**
70
     * Add decorator.
71
     *
72
     *
73
     * @return AttributeDecorator
74
     */
75
    public function addDecorator(string $attribute, Closure $decorator): self
76
    {
77
        $this->custom[$attribute] = $decorator;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Get Attributes.
84
     *
85
     * @param RoleInterface
86
     */
87
    protected function getAttributes(RoleInterface $role, array $attributes): array
0 ignored issues
show
Unused Code introduced by
The parameter $role 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...
88
    {
89
        $user = $this->server->getIdentity();
90
        if ($user === null || $attributes['_id'] != $user->getId() && !$user->isAdmin()) {
91
            return [];
92
        }
93
94
        return [
95
            'created' => function ($role) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $role 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...
96
                return $attributes['created']->toDateTime()->format('c');
97
            },
98
            'changed' => function ($role) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $role 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...
99
                return $attributes['changed']->toDateTime()->format('c');
100
            },
101
            'deleted' => function ($role) use ($attributes) {
0 ignored issues
show
Unused Code introduced by
The parameter $role 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...
102
                if (false === $attributes['deleted']) {
103
                    return null;
104
                }
105
106
                return $attributes['deleted']->toDateTime()->format('c');
107
            },
108
        ];
109
    }
110
111
    /**
112
     * Get group Attributes.
113
     *
114
     * @param RoleInterface
115
     */
116
    protected function getGroupAttributes(RoleInterface $role, array $attributes): array
117
    {
118
        if (!($role instanceof Group)) {
119
            return [];
120
        }
121
122
        return [
123
            'id' => (string) $attributes['_id'],
124
            'name' => $attributes['name'],
125
            'namespace' => isset($attributes['namespace']) ? (string) $attributes['namespace'] : null,
126
        ];
127
    }
128
129
    /**
130
     * Get user Attributes.
131
     *
132
     * @param RoleInterface
133
     */
134
    protected function getUserAttributes(RoleInterface $role, array $attributes): array
135
    {
136
        if (!($role instanceof User)) {
137
            return [];
138
        }
139
140
        $user = $this->server->getIdentity();
141
142
        return [
143
            'id' => (string) $attributes['_id'],
144
            'username' => (string) $attributes['username'],
145
            'name' => (string) $attributes['username'],
146
            'namespace' => isset($attributes['namespace']) ? (string) $attributes['namespace'] : null,
147
            'mail' => function ($role) use ($attributes, $user) {
0 ignored issues
show
Unused Code introduced by
The parameter $role 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...
148
                if (!isset($attributes['mail'])) {
149
                    return null;
150
                }
151
152
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
153
                    return (string) $attributes['mail'];
154
                }
155
156
                return null;
157
            },
158
            'locale' => isset($attributes['locale']) ? (string) $attributes['locale'] : null,
159
            'quota' => function ($role) use ($attributes, $user) {
160
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
161
                    return $role->getQuotaUsage();
162
                }
163
164
                return null;
165
            },
166
        ];
167
    }
168
169
    /**
170
     * Execute closures.
171
     *
172
     * @param RoleInterface
173
     */
174
    protected function translateAttributes(RoleInterface $role, array $attributes): array
175
    {
176
        foreach ($attributes as $key => &$value) {
177
            if ($value instanceof Closure) {
178
                $result = $value($role);
179
180
                if (null === $result) {
181
                    unset($attributes[$key]);
182
                } else {
183
                    $value = $result;
184
                }
185
            } elseif ($value === null) {
186
                unset($attributes[$key]);
187
            }
188
        }
189
190
        return $attributes;
191
    }
192
}
193