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

Group::getResolvedMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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 Generator;
16
use MongoDB\BSON\ObjectId;
17
use MongoDB\BSON\UTCDateTime;
18
use MongoDB\Database;
19
use Psr\Log\LoggerInterface;
20
21
class Group implements RoleInterface
22
{
23
    /**
24
     * User unique id.
25
     *
26
     * @var ObjectId
27
     */
28
    protected $_id;
29
30
    /**
31
     * Name.
32
     *
33
     * @var string
34
     */
35
    protected $name;
36
37
    /**
38
     * Member.
39
     *
40
     * @var array
41
     */
42
    protected $member = [];
43
44
    /**
45
     * Optional group attributes.
46
     *
47
     * @var array
48
     */
49
    protected $optional = [];
50
51
    /**
52
     * Is group deleted?
53
     *
54
     * @var bool|UTCDateTime
55
     */
56
    protected $deleted = false;
57
58
    /**
59
     * Admin.
60
     *
61
     * @var bool
62
     */
63
    protected $admin = false;
64
65
    /**
66
     * Created.
67
     *
68
     * @var UTCDateTime
69
     */
70
    protected $created;
71
72
    /**
73
     * Changed.
74
     *
75
     * @var UTCDateTime
76
     */
77
    protected $changed;
78
79
    /**
80
     * Namespace.
81
     *
82
     * @var string
83
     */
84
    protected $namespace;
85
86
    /**
87
     * Mail.
88
     *
89
     * @var string
90
     */
91
    protected $mail;
92
93
    /**
94
     * Db.
95
     *
96
     * @var Database
97
     */
98
    protected $db;
99
100
    /**
101
     * LoggerInterface.
102
     *
103
     * @var LoggerInterface
104
     */
105
    protected $logger;
106
107
    /**
108
     * Server.
109
     *
110
     * @var Server
111
     */
112
    protected $server;
113
114
    /**
115
     * Init.
116
     *
117
     * @param array           $attributes
118
     * @param Server          $server
119
     * @param Database        $db
120
     * @param LoggerInterface $logger
121
     */
122
    public function __construct(array $attributes, Server $server, Database $db, LoggerInterface $logger)
123
    {
124
        $this->server = $server;
125
        $this->db = $db;
126
        $this->logger = $logger;
127
128
        foreach ($attributes as $attr => $value) {
129
            $this->{$attr} = $value;
130
        }
131
    }
132
133
    /**
134
     * Return name as string.
135
     *
136
     * @return string
137
     */
138
    public function __toString(): string
139
    {
140
        return $this->name;
141
    }
142
143
    /**
144
     * Get Attributes.
145
     *
146
     * @return array
147
     */
148
    public function getAttributes(): array
149
    {
150
        return [
151
            '_id' => $this->_id,
152
            'name' => $this->name,
153
            'namespace' => $this->namespace,
154
            'created' => $this->created,
155
            'changed' => $this->changed,
156
            'deleted' => $this->deleted,
157
            'optional' => $this->optional,
158
        ];
159
    }
160
161
    /**
162
     * Get unique id.
163
     *
164
     * @return ObjectId
165
     */
166
    public function getId(): ObjectId
167
    {
168
        return $this->_id;
169
    }
170
171
    /**
172
     * Set group attributes.
173
     *
174
     * @param array $attributes
175
     *
176
     * @return bool
177
     */
178
    public function setAttributes(array $attributes = []): bool
179
    {
180
        $attributes = $this->server->validateGroupAttributes($attributes);
181
182
        foreach ($attributes as $attr => $value) {
183
            $this->{$attr} = $value;
184
        }
185
186
        return $this->save(array_keys($attributes));
187
    }
188
189
    /**
190
     * Save.
191
     *
192
     * @param array $attributes
193
     *
194
     * @return bool
195
     */
196
    public function save(array $attributes = []): bool
197
    {
198
        $set = [];
199
        foreach ($attributes as $attr) {
200
            $set[$attr] = $this->{$attr};
201
        }
202
203
        $result = $this->db->group->updateOne([
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
204
            '_id' => $this->_id,
205
        ], [
206
            '$set' => $set,
207
        ]);
208
209
        return true;
210
    }
211
212
    /**
213
     * Delete user.
214
     *
215
     * @param bool $force
216
     *
217
     * @return bool
218
     */
219
    public function delete(bool $force = false): bool
0 ignored issues
show
Unused Code introduced by
The parameter $force 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...
220
    {
221
    }
222
223
    /**
224
     * Undelete user.
225
     *
226
     * @return bool
227
     */
228
    public function undelete(): bool
229
    {
230
        $this->deleted = false;
231
232
        return $this->save(['deleted']);
233
    }
234
235
    /**
236
     * Check if user is deleted.
237
     *
238
     * @return bool
239
     */
240
    public function isDeleted(): bool
241
    {
242
        return $this->deleted instanceof UTCDateTime;
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\UTCDateTime 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...
243
    }
244
245
    /**
246
     * Get Username.
247
     *
248
     * @return string
249
     */
250
    public function getName(): string
251
    {
252
        return $this->name;
253
    }
254
255
    /**
256
     * Get member.
257
     *
258
     * @return array
259
     */
260
    public function getMembers(): array
261
    {
262
        return $this->member;
263
    }
264
265
    /**
266
     * Get resolved member.
267
     *
268
     * @return Generator
269
     */
270
    public function getResolvedMembers(?int $offset = null, ?int $limit = null): ?Generator
271
    {
272
        return $this->server->getUsers([
273
            '_id' => ['$in' => $this->member],
274
        ], $offset, $limit);
275
    }
276
}
277