Completed
Push — master ( 4407aa...f0730d )
by ANTHONIUS
22s queued 14s
created

User::addGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the DoyoUserBundle project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\UserBundle\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
18
abstract class User implements UserInterface, GroupableInterface
19
{
20
    /**
21
     * @var string|null
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string|null
27
     */
28
    protected $username;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $usernameCanonical;
34
35
    /**
36
     * @var string|null
37
     */
38
    protected $email;
39
40
    /**
41
     * @var string|null
42
     */
43
    protected $emailCanonical;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $enabled;
49
50
    /**
51
     * @var array|null
52
     */
53
    protected $roles;
54
55
    /**
56
     * @var string|null
57
     */
58
    protected $plainPassword;
59
60
    /**
61
     * @var string|null
62
     */
63
    protected $password;
64
65
    /**
66
     * @var string|null
67
     */
68
    protected $salt;
69
70
    /**
71
     * @var \DateTimeImmutable|null
72
     */
73
    protected $lastLogin;
74
75
    /**
76
     * @var string|null
77
     */
78
    protected $confirmationToken;
79
80
    /**
81
     * @var GroupInterface[]|ArrayCollection
82
     */
83
    protected $groups;
84
85
    /**
86
     * @var \DateTimeImmutable|null
87
     */
88
    protected $passwordRequestedAt;
89
90 13
    public function __construct()
91
    {
92 13
        $this->roles   = ['ROLE_USER'];
93 13
        $this->enabled = false;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 17
    public function getGroups()
100
    {
101 17
        return $this->groups ?: $this->groups = new ArrayCollection();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function getGroupNames()
108
    {
109 1
        $names = [];
110 1
        foreach ($this->getGroups() as $group) {
111 1
            $names[] = $group->getName();
112
        }
113
114 1
        return $names;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 2
    public function hasGroup($group)
121
    {
122 2
        if ($group instanceof GroupInterface) {
123 1
            return $this->getGroups()->contains($group);
124
        }
125
126 1
        return \in_array($group, $this->getGroupNames(), true);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 4
    public function addGroup(GroupInterface $group)
133
    {
134 4
        if (!$this->getGroups()->contains($group)) {
135 4
            $this->getGroups()->add($group);
136
        }
137
138 4
        return $this;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function removeGroup(GroupInterface $group)
145
    {
146 1
        if ($this->getGroups()->contains($group)) {
147 1
            $this->getGroups()->removeElement($group);
148
        }
149
150 1
        return $this;
151
    }
152
153
    /**
154
     * @return int|string|null
155
     */
156 9
    public function getId()
157
    {
158 9
        return $this->id;
159
    }
160
161
    /**
162
     * @param string $role
163
     *
164
     * @return static
165
     */
166 13
    public function addRole($role)
167
    {
168 13
        $role = strtoupper($role);
169
170 13
        if (!$this->hasRole($role)) {
171 3
            $this->roles[] = $role;
172
        }
173
174 13
        return $this;
175
    }
176
177
    /**
178
     * @param string $role
179
     */
180 13
    public function hasRole($role): bool
181
    {
182 13
        $role = strtoupper($role);
183
184 13
        return \in_array($role, $this->roles, true);
0 ignored issues
show
Bug introduced by
It seems like $this->roles can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
        return \in_array($role, /** @scrutinizer ignore-type */ $this->roles, true);
Loading history...
185
    }
186
187
    /**
188
     * @param string $role
189
     *
190
     * @return static
191
     */
192 1
    public function removeRole($role)
193
    {
194 1
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
0 ignored issues
show
Bug introduced by
It seems like $this->roles can also be of type null; however, parameter $haystack of array_search() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
        if (false !== $key = array_search(strtoupper($role), /** @scrutinizer ignore-type */ $this->roles, true)) {
Loading history...
195 1
            unset($this->roles[$key]);
196 1
            $this->roles = array_values($this->roles);
0 ignored issues
show
Bug introduced by
It seems like $this->roles can also be of type null; however, parameter $input of array_values() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
            $this->roles = array_values(/** @scrutinizer ignore-type */ $this->roles);
Loading history...
197
        }
198
199 1
        return $this;
200
    }
201
202
    /**
203
     * @return static
204
     */
205 1
    public function setRoles(?array $roles)
206
    {
207 1
        $this->roles = [];
208
209 1
        foreach ($roles as $role) {
210 1
            $this->addRole($role);
211
        }
212
213 1
        return $this;
214
    }
215
216 13
    public function getSalt(): ?string
217
    {
218 13
        return $this->salt;
219
    }
220
221
    /**
222
     * @return static
223
     */
224 12
    public function setSalt(?string $salt)
225
    {
226 12
        $this->salt = $salt;
227
228 12
        return $this;
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234 14
    public function eraseCredentials()
235
    {
236 14
        $this->plainPassword = null;
237
    }
238
239 6
    public function isEnabled(): bool
240
    {
241 6
        return $this->enabled;
242
    }
243
244
    /**
245
     * @return static
246
     */
247 5
    public function setEnabled(bool $enabled)
248
    {
249 5
        $this->enabled = $enabled;
250
251 5
        return $this;
252
    }
253
254 14
    public function getUsername(): ?string
255
    {
256 14
        return $this->username;
257
    }
258
259
    /**
260
     * @return static
261
     */
262 12
    public function setUsername(?string $username)
263
    {
264 12
        $this->username = $username;
265
266 12
        return $this;
267
    }
268
269 1
    public function getUsernameCanonical(): ?string
270
    {
271 1
        return $this->usernameCanonical;
272
    }
273
274
    /**
275
     * @return static
276
     */
277 12
    public function setUsernameCanonical(?string $usernameCanonical)
278
    {
279 12
        $this->usernameCanonical = $usernameCanonical;
280
281 12
        return $this;
282
    }
283
284 13
    public function getEmail(): ?string
285
    {
286 13
        return $this->email;
287
    }
288
289
    /**
290
     * @return static
291
     */
292 12
    public function setEmail(?string $email)
293
    {
294 12
        $this->email = $email;
295
296 12
        return $this;
297
    }
298
299 1
    public function getEmailCanonical(): ?string
300
    {
301 1
        return $this->emailCanonical;
302
    }
303
304
    /**
305
     * @return static
306
     */
307 12
    public function setEmailCanonical(?string $emailCanonical)
308
    {
309 12
        $this->emailCanonical = $emailCanonical;
310
311 12
        return $this;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317 16
    public function getRoles()
318
    {
319 16
        $roles = $this->roles;
320
321 16
        foreach ($this->getGroups() as $group) {
322 2
            $roles = array_merge($roles, $group->getRoles());
0 ignored issues
show
Bug introduced by
It seems like $roles can also be of type null; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

322
            $roles = array_merge(/** @scrutinizer ignore-type */ $roles, $group->getRoles());
Loading history...
323
        }
324
325
        // we need to make sure to have at least one role
326 16
        $roles[] = static::ROLE_DEFAULT;
327
328 16
        return array_unique($roles);
0 ignored issues
show
Bug introduced by
It seems like $roles can also be of type null; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

328
        return array_unique(/** @scrutinizer ignore-type */ $roles);
Loading history...
329
    }
330
331 13
    public function getPlainPassword(): ?string
332
    {
333 13
        return $this->plainPassword;
334
    }
335
336
    /**
337
     * @return static
338
     */
339 13
    public function setPlainPassword(?string $plainPassword)
340
    {
341 13
        $this->plainPassword = $plainPassword;
342
343 13
        return $this;
344
    }
345
346 2
    public function getPassword(): ?string
347
    {
348 2
        return $this->password;
349
    }
350
351
    /**
352
     * @return static
353
     */
354 12
    public function setPassword(?string $password)
355
    {
356 12
        $this->password = $password;
357
358 12
        return $this;
359
    }
360
361 6
    public function getLastLogin(): ?\DateTimeImmutable
362
    {
363 6
        return $this->lastLogin;
364
    }
365
366
    /**
367
     * @return static
368
     */
369 1
    public function setLastLogin(?\DateTimeImmutable $lastLogin)
370
    {
371 1
        $this->lastLogin = $lastLogin;
372
373 1
        return $this;
374
    }
375
376 1
    public function getConfirmationToken(): ?string
377
    {
378 1
        return $this->confirmationToken;
379
    }
380
381
    /**
382
     * @return static
383
     */
384 1
    public function setConfirmationToken(?string $confirmationToken)
385
    {
386 1
        $this->confirmationToken = $confirmationToken;
387
388 1
        return $this;
389
    }
390
391 1
    public function getPasswordRequestedAt(): ?\DateTimeImmutable
392
    {
393 1
        return $this->passwordRequestedAt;
394
    }
395
396
    /**
397
     * @return static
398
     */
399 1
    public function setPasswordRequestedAt(?\DateTimeImmutable $passwordRequestedAt)
400
    {
401 1
        $this->passwordRequestedAt = $passwordRequestedAt;
402
403 1
        return $this;
404
    }
405
}
406