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

AttributeDecorator::getUserAttributes()   C

Complexity

Conditions 8
Paths 2

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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