Completed
Push — master ( c6728e...bf498d )
by Raffael
14:18 queued 04:37
created

AttributeDecorator::getUserAttributes()   C

Complexity

Conditions 15
Paths 17

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 0
cts 33
cp 0
rs 5.9166
c 0
b 0
f 0
cc 15
nc 17
nop 2
crap 240

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $quota = null;
142
143
        $result = [
144
            'id' => (string) $attributes['_id'],
145
            'username' => (string) $attributes['username'],
146
            'name' => (string) $attributes['username'],
147
            'admin' => (bool) $attributes['admin'],
148
            'namespace' => isset($attributes['namespace']) ? (string) $attributes['namespace'] : null,
149
            '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...
150
                if (!isset($attributes['mail'])) {
151
                    return null;
152
                }
153
154
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
155
                    return (string) $attributes['mail'];
156
                }
157
158
                return null;
159
            },
160
            'locale' => isset($attributes['locale']) ? (string) $attributes['locale'] : null,
161
            'hard_quota' => isset($attributes['hard_quota']) ? (int) $attributes['hard_quota'] : null,
162
            'soft_quota' => isset($attributes['soft_quota']) ? (int) $attributes['soft_quota'] : null,
163
            'available' => function ($role) use (&$quota, $attributes, $user) {
164
                $quota === null ? $quota = $role->getQuotaUsage() : null;
165
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
166
                    return $quota['available'];
167
                }
168
169
                return null;
170
            },
171
            'used' => function ($role) use (&$quota, $attributes, $user) {
172
                $quota === null ? $quota = $role->getQuotaUsage() : null;
173
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
174
                    return $quota['used'];
175
                }
176
177
                return null;
178
            },
179
        ];
180
181
        return $result;
182
    }
183
184
    /**
185
     * Execute closures.
186
     *
187
     * @param RoleInterface
188
     */
189
    protected function translateAttributes(RoleInterface $role, array $attributes): array
190
    {
191
        foreach ($attributes as $key => &$value) {
192
            if ($value instanceof Closure) {
193
                $result = $value($role);
194
195
                if (null === $result) {
196
                    unset($attributes[$key]);
197
                } else {
198
                    $value = $result;
199
                }
200
            } elseif ($value === null) {
201
                unset($attributes[$key]);
202
            }
203
        }
204
205
        return $attributes;
206
    }
207
}
208