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

RoleDecorator::getUserAttributes()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 4.909
c 0
b 0
f 0
cc 9
eloc 25
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\App\Api\v1\AttributeDecorator;
13
14
use Balloon\Server;
15
use Balloon\Server\Group;
16
use Balloon\Server\RoleInterface;
17
use Balloon\Server\User;
18
use Closure;
19
use MongoDB\BSON\Binary;
20
21
class RoleDecorator
22
{
23
    /**
24
     * Server.
25
     *
26
     * @var Server
27
     */
28
    protected $server;
29
30
    /**
31
     * Custom attributes.
32
     *
33
     * @var array
34
     */
35
    protected $custom = [];
36
37
    /**
38
     * Init.
39
     *
40
     * @param Server $server
41
     */
42
    public function __construct(Server $server)
43
    {
44
        $this->server = $server;
45
    }
46
47
    /**
48
     * Decorate attributes.
49
     *
50
     * @param RoleInterface $role
51
     * @param array         $attributes
52
     *
53
     * @return array
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
        if (0 === count($attributes)) {
71
            return $this->translateAttributes($role, $attrs, $attributes);
72
        }
73
74
        return $this->translateAttributes($role, array_intersect_key($attrs, array_flip($attributes)), $attributes);
75
    }
76
77
    /**
78
     * Add decorator.
79
     *
80
     * @param string  $attribute
81
     * @param Closure $decorator
82
     *
83
     * @return AttributeDecorator
84
     */
85
    public function addDecorator(string $attribute, Closure $decorator): self
86
    {
87
        $this->custom[$attribute] = $decorator;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Get Attributes.
94
     *
95
     * @param RoleInterface
96
     * @param array $attributes
97
     *
98
     * @return array
99
     */
100
    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...
101
    {
102
        $user = $this->server->getIdentity();
103
        if ($user === null || $attributes['id'] != $user->getId() && !$user->isAdmin()) {
104
            return [];
105
        }
106
107
        return [
108
            '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...
109
                return $attributes['created']->toDateTime()->format('c');
110
            },
111
            '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...
112
                return $attributes['changed']->toDateTime()->format('c');
113
            },
114
            '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...
115
                if (false === $attributes['deleted']) {
116
                    return null;
117
                }
118
119
                return $attributes['deleted']->toDateTime()->format('c');
120
            },
121
        ];
122
    }
123
124
    /**
125
     * Get group Attributes.
126
     *
127
     * @param RoleInterface
128
     * @param array $attributes
129
     *
130
     * @return array
131
     */
132
    protected function getGroupAttributes(RoleInterface $role, array $attributes): array
133
    {
134
        if (!($role instanceof Group)) {
135
            return [];
136
        }
137
138
        return [
139
            'id' => (string) $attributes['_id'],
140
            'name' => $attributes['name'],
141
            'namespace' => $attributes['namespace'],
142
        ];
143
    }
144
145
    /**
146
     * Get user Attributes.
147
     *
148
     * @param RoleInterface
149
     * @param array $attributes
150
     *
151
     * @return array
152
     */
153
    protected function getUserAttributes(RoleInterface $role, array $attributes): array
154
    {
155
        if (!($role instanceof User)) {
156
            return [];
157
        }
158
159
        $user = $this->server->getIdentity();
160
161
        return [
162
            'id' => (string) $attributes['_id'],
163
            'name' => (string) $attributes['username'],
164
            'namespace' => (string) $attributes['namespace'],
165
            'mail' => (string) $attributes['mail'],
166
            'avatar' => 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...
167
                if ($attributes['avatar'] instanceof Binary) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\Binary does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
168
                    return base64_encode($attributes['avatar']->getData());
169
                }
170
171
                return null;
172
            },
173
            '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...
174
                if ($user === null) {
175
                    return null;
176
                }
177
178
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
179
                    return $attributes['soft_quota'];
180
                }
181
182
                return null;
183
            },
184
            '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...
185
                if ($user === null) {
186
                    return null;
187
                }
188
189
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
190
                    return $attributes['hard_quota'];
191
                }
192
193
                return null;
194
            },
195
        ];
196
    }
197
198
    /**
199
     * Execute closures.
200
     *
201
     * @param RoleInterface
202
     * @param array $attributes
203
     * @param array $requested
204
     *
205
     * @return array
206
     */
207
    protected function translateAttributes(RoleInterface $role, array $attributes, array $requested): array
208
    {
209
        foreach ($attributes as $key => &$value) {
210
            if ($value instanceof Closure) {
211
                $result = $value($role, $requested);
212
213
                if (null === $result) {
214
                    unset($attributes[$key]);
215
                } else {
216
                    $value = $result;
217
                }
218
            } elseif ($value === null) {
219
                unset($attributes[$key]);
220
            }
221
        }
222
223
        return $attributes;
224
    }
225
}
226