Test Failed
Push — master ( 817d84...d52e3d )
by Raffael
05:44
created

AttributeDecorator::getUserAttributes()   D

Complexity

Conditions 18
Paths 17

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 342

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 41
cp 0
rs 4.8666
c 0
b 0
f 0
cc 18
nc 17
nop 2
crap 342

How to fix   Long Method    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-2019 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\Auth\InternalAuthInterface;
16
use Balloon\Hook;
17
use Balloon\Server;
18
use Closure;
19
20
class AttributeDecorator implements AttributeDecoratorInterface
21
{
22
    /**
23
     * Server.
24
     *
25
     * @var Server
26
     */
27
    protected $server;
28
29
    /**
30
     * Hook.
31
     *
32
     * @var Hook
33
     */
34
    protected $hook;
35
36
    /**
37
     * Custom attributes.
38
     *
39
     * @var array
40
     */
41
    protected $custom = [];
42
43
    /**
44
     * Init.
45
     */
46
    public function __construct(Server $server, Hook $hook)
47
    {
48
        $this->server = $server;
49
        $this->hook = $hook;
50
    }
51
52
    /**
53
     * Decorate attributes.
54
     */
55
    public function decorate(RoleInterface $role, ?array $attributes = null): array
56
    {
57
        if (null === $attributes) {
58
            $attributes = [];
59
        }
60
61
        $role_attributes = $role->getAttributes();
62
63
        $attrs = array_merge(
64
            $this->getAttributes($role, $role_attributes),
65
            $this->getUserAttributes($role, $role_attributes),
66
            $this->getGroupAttributes($role, $role_attributes),
67
            $this->custom
68
        );
69
70
        $this->hook->run('preDecorateRole', [$role, &$attrs]);
71
72
        if (0 === count($attributes)) {
73
            return $this->translateAttributes($role, $attrs);
74
        }
75
76
        return $this->translateAttributes($role, array_intersect_key($attrs, array_flip($attributes)));
77
    }
78
79
    /**
80
     * Add decorator.
81
     */
82
    public function addDecorator(string $attribute, Closure $decorator): self
83
    {
84
        $this->custom[$attribute] = $decorator;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Get Attributes.
91
     */
92
    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...
93
    {
94
        $user = $this->server->getIdentity();
95
        if ($user === null || $attributes['_id'] != $user->getId() && !$user->isAdmin()) {
96
            return [];
97
        }
98
99
        return [
100
            '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...
101
                return $attributes['created']->toDateTime()->format('c');
102
            },
103
            '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...
104
                return $attributes['changed']->toDateTime()->format('c');
105
            },
106
            '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...
107
                if (false === $attributes['deleted']) {
108
                    return null;
109
                }
110
111
                return $attributes['deleted']->toDateTime()->format('c');
112
            },
113
        ];
114
    }
115
116
    /**
117
     * Get group Attributes.
118
     */
119
    protected function getGroupAttributes(RoleInterface $role, array $attributes): array
120
    {
121
        if (!($role instanceof Group)) {
122
            return [];
123
        }
124
125
        return [
126
            'id' => (string) $attributes['_id'],
127
            'name' => $attributes['name'],
128
            'namespace' => isset($attributes['namespace']) ? (string) $attributes['namespace'] : null,
129
            'member' => array_map(function ($member) { return (string) $member; }, $attributes['member']),
130
        ];
131
    }
132
133
    /**
134
     * Get user Attributes.
135
     */
136
    protected function getUserAttributes(RoleInterface $role, array $attributes): array
137
    {
138
        if (!($role instanceof User)) {
139
            return [];
140
        }
141
142
        $user = $this->server->getIdentity();
143
        $quota = null;
144
145
        $result = [
146
            'id' => (string) $attributes['_id'],
147
            'username' => (string) $attributes['username'],
148
            'name' => (string) $attributes['username'],
149
            'admin' => (bool) $attributes['admin'],
150
            'namespace' => isset($attributes['namespace']) ? (string) $attributes['namespace'] : null,
151
            '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...
152
                if (!isset($attributes['mail'])) {
153
                    return null;
154
                }
155
156
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
157
                    return (string) $attributes['mail'];
158
                }
159
160
                return null;
161
            },
162
            'locale' => isset($attributes['locale']) ? (string) $attributes['locale'] : null,
163
            'hard_quota' => isset($attributes['hard_quota']) ? (int) $attributes['hard_quota'] : null,
164
            'soft_quota' => isset($attributes['soft_quota']) ? (int) $attributes['soft_quota'] : null,
165
            'available' => function ($role) use (&$quota, $attributes, $user) {
166
                $quota === null ? $quota = $role->getQuotaUsage() : null;
167
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
168
                    return $quota['available'];
169
                }
170
171
                return null;
172
            },
173
            'used' => function ($role) use (&$quota, $attributes, $user) {
174
                $quota === null ? $quota = $role->getQuotaUsage() : null;
175
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
176
                    return $quota['used'];
177
                }
178
179
                return null;
180
            },
181
            'auth' => function () use ($user) {
182
                $identity = $user->getIdentity();
183
                if ($identity === null) {
184
                    return null;
185
                }
186
187
                if ($identity->getAdapter() instanceof InternalAuthInterface) {
188
                    return $identity->getAdapter()->isInternal() ? 'internal' : 'external';
189
                }
190
191
                return 'external';
192
            },
193
        ];
194
195
        return $result;
196
    }
197
198
    /**
199
     * Execute closures.
200
     */
201
    protected function translateAttributes(RoleInterface $role, array $attributes): array
202
    {
203
        foreach ($attributes as $key => &$value) {
204
            if ($value instanceof Closure) {
205
                $result = $value($role);
206
207
                if (null === $result) {
208
                    unset($attributes[$key]);
209
                } else {
210
                    $value = $result;
211
                }
212
            } elseif ($value === null) {
213
                unset($attributes[$key]);
214
            }
215
        }
216
217
        return $attributes;
218
    }
219
}
220