Completed
Branch dev (276354)
by Raffael
15:43
created

AttributeDecorator::getUserAttributes()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 14
nc 2
nop 2
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
     * @param Server $server
38
     */
39
    public function __construct(Server $server)
40
    {
41
        $this->server = $server;
42
    }
43
44
    /**
45
     * Decorate attributes.
46
     *
47
     * @param RoleInterface $role
48
     * @param array         $attributes
49
     *
50
     * @return array
51
     */
52
    public function decorate(RoleInterface $role, ?array $attributes = null): array
53
    {
54
        if (null === $attributes) {
55
            $attributes = [];
56
        }
57
58
        $role_attributes = $role->getAttributes();
59
60
        $attrs = array_merge(
61
            $this->getAttributes($role, $role_attributes),
62
            $this->getUserAttributes($role, $role_attributes),
63
            $this->getGroupAttributes($role, $role_attributes),
64
            $this->custom
65
        );
66
67
        if (0 === count($attributes)) {
68
            return $this->translateAttributes($role, $attrs);
69
        }
70
71
        return $this->translateAttributes($role, array_intersect_key($attrs, array_flip($attributes)));
72
    }
73
74
    /**
75
     * Add decorator.
76
     *
77
     * @param string  $attribute
78
     * @param Closure $decorator
79
     *
80
     * @return AttributeDecorator
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
     * @param RoleInterface
93
     * @param array $attributes
94
     *
95
     * @return array
96
     */
97
    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...
98
    {
99
        $user = $this->server->getIdentity();
100
        if ($user === null || $attributes['_id'] != $user->getId() && !$user->isAdmin()) {
101
            return [];
102
        }
103
104
        return [
105
            '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...
106
                return $attributes['created']->toDateTime()->format('c');
107
            },
108
            '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...
109
                return $attributes['changed']->toDateTime()->format('c');
110
            },
111
            '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...
112
                if (false === $attributes['deleted']) {
113
                    return null;
114
                }
115
116
                return $attributes['deleted']->toDateTime()->format('c');
117
            },
118
        ];
119
    }
120
121
    /**
122
     * Get group Attributes.
123
     *
124
     * @param RoleInterface
125
     * @param array $attributes
126
     *
127
     * @return array
128
     */
129
    protected function getGroupAttributes(RoleInterface $role, array $attributes): array
130
    {
131
        if (!($role instanceof Group)) {
132
            return [];
133
        }
134
135
        return [
136
            'id' => (string) $attributes['_id'],
137
            'name' => $attributes['name'],
138
            'namespace' => $attributes['namespace'],
139
        ];
140
    }
141
142
    /**
143
     * Get user Attributes.
144
     *
145
     * @param RoleInterface
146
     * @param array $attributes
147
     *
148
     * @return array
149
     */
150
    protected function getUserAttributes(RoleInterface $role, array $attributes): array
151
    {
152
        if (!($role instanceof User)) {
153
            return [];
154
        }
155
156
        $user = $this->server->getIdentity();
157
158
        return [
159
            'id' => (string) $attributes['_id'],
160
            'username' => (string) $attributes['username'],
161
            'name' => (string) $attributes['username'],
162
            'namespace' => (string) $attributes['namespace'],
163
            'mail' => (string) $attributes['mail'],
164
            'quota' => function ($role) use ($attributes, $user) {
165
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
166
                    return $role->getQuotaUsage();
167
                }
168
169
                return null;
170
            },
171
        ];
172
    }
173
174
    /**
175
     * Execute closures.
176
     *
177
     * @param RoleInterface
178
     * @param array $attributes
179
     *
180
     * @return array
181
     */
182
    protected function translateAttributes(RoleInterface $role, array $attributes): array
183
    {
184
        foreach ($attributes as $key => &$value) {
185
            if ($value instanceof Closure) {
186
                $result = $value($role);
187
188
                if (null === $result) {
189
                    unset($attributes[$key]);
190
                } else {
191
                    $value = $result;
192
                }
193
            } elseif ($value === null) {
194
                unset($attributes[$key]);
195
            }
196
        }
197
198
        return $attributes;
199
    }
200
}
201