Completed
Push — master ( 287393...7ff50d )
by Raffael
18:27 queued 14:12
created

v1/AttributeDecorator/RoleDecorator.php (13 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\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
    public function __construct(Server $server)
41
    {
42
        $this->server = $server;
43
    }
44
45
    /**
46
     * Decorate attributes.
47
     *
48
     * @param array $attributes
49
     */
50
    public function decorate(RoleInterface $role, ?array $attributes = null): array
51
    {
52
        if (null === $attributes) {
53
            $attributes = [];
54
        }
55
56
        $role_attributes = $role->getAttributes();
57
58
        $attrs = array_merge(
59
            $this->getAttributes($role, $role_attributes),
60
            $this->getUserAttributes($role, $role_attributes),
61
            $this->getGroupAttributes($role, $role_attributes),
62
            $this->custom
63
        );
64
65
        if (0 === count($attributes)) {
66
            return $this->translateAttributes($role, $attrs, $attributes);
67
        }
68
69
        return $this->translateAttributes($role, array_intersect_key($attrs, array_flip($attributes)), $attributes);
70
    }
71
72
    /**
73
     * Add decorator.
74
     *
75
     *
76
     * @return AttributeDecorator
77
     */
78
    public function addDecorator(string $attribute, Closure $decorator): self
79
    {
80
        $this->custom[$attribute] = $decorator;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Get Attributes.
87
     *
88
     * @param RoleInterface
89
     */
90
    protected function getAttributes(RoleInterface $role, array $attributes): array
0 ignored issues
show
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...
91
    {
92
        $user = $this->server->getIdentity();
93
        if ($user === null || $attributes['_id'] != $user->getId() && !$user->isAdmin()) {
94
            return [];
95
        }
96
97
        return [
98
            'created' => function ($role, $requested) use ($attributes) {
0 ignored issues
show
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...
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...
99
                return $attributes['created']->toDateTime()->format('c');
100
            },
101
            'changed' => function ($role, $requested) use ($attributes) {
0 ignored issues
show
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...
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...
102
                return $attributes['changed']->toDateTime()->format('c');
103
            },
104
            'deleted' => function ($role, $requested) use ($attributes) {
0 ignored issues
show
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...
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
                if (false === $attributes['deleted']) {
106
                    return null;
107
                }
108
109
                return $attributes['deleted']->toDateTime()->format('c');
110
            },
111
        ];
112
    }
113
114
    /**
115
     * Get group Attributes.
116
     *
117
     * @param RoleInterface
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' => $attributes['namespace'],
129
        ];
130
    }
131
132
    /**
133
     * Get user Attributes.
134
     *
135
     * @param RoleInterface
136
     */
137
    protected function getUserAttributes(RoleInterface $role, array $attributes): array
138
    {
139
        if (!($role instanceof User)) {
140
            return [];
141
        }
142
143
        $user = $this->server->getIdentity();
144
145
        return [
146
            'id' => (string) $attributes['_id'],
147
            'username' => (string) $attributes['username'],
148
            'name' => (string) $attributes['username'],
149
            'namespace' => (string) $attributes['namespace'],
150
            'mail' => (string) $attributes['mail'],
151
            'avatar' => function ($role, $requested) use ($attributes) {
0 ignored issues
show
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...
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...
152
                if ($attributes['avatar'] instanceof Binary) {
153
                    return base64_encode($attributes['avatar']->getData());
154
                }
155
156
                return null;
157
            },
158
            'soft_quota' => function ($role, $requested) use ($attributes, $user) {
0 ignored issues
show
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...
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...
159
                if ($user === null) {
160
                    return null;
161
                }
162
163
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
164
                    return $attributes['soft_quota'];
165
                }
166
167
                return null;
168
            },
169
            'hard_quota' => function ($role, $requested) use ($attributes, $user) {
0 ignored issues
show
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...
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...
170
                if ($user === null) {
171
                    return null;
172
                }
173
174
                if ($attributes['_id'] == $user->getId() || $user->isAdmin()) {
175
                    return $attributes['hard_quota'];
176
                }
177
178
                return null;
179
            },
180
        ];
181
    }
182
183
    /**
184
     * Execute closures.
185
     *
186
     * @param RoleInterface
187
     */
188
    protected function translateAttributes(RoleInterface $role, array $attributes, array $requested): array
189
    {
190
        foreach ($attributes as $key => &$value) {
191
            if ($value instanceof Closure) {
192
                $result = $value($role, $requested);
193
194
                if (null === $result) {
195
                    unset($attributes[$key]);
196
                } else {
197
                    $value = $result;
198
                }
199
            } elseif ($value === null) {
200
                unset($attributes[$key]);
201
            }
202
        }
203
204
        return $attributes;
205
    }
206
}
207