Passed
Push — develop ( e67197...1795b3 )
by BENARD
09:30
created

User::getIsOnline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A User::setPlainPassword() 0 4 1
1
<?php
2
3
namespace ProjetNormandie\UserBundle\Entity;
4
5
use DateTime;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
9
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
10
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
11
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
12
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
13
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
14
use Symfony\Component\Security\Core\User\UserInterface;
15
use ApiPlatform\Core\Annotation\ApiResource;
16
use ApiPlatform\Core\Annotation\ApiFilter;
17
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
18
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
19
20
/**
21
 * User
22
 *
23
 * @ORM\Table(name="user")
24
 * @ORM\Entity(repositoryClass="ProjetNormandie\UserBundle\Repository\UserRepository")
25
 * @ORM\EntityListeners({"ProjetNormandie\UserBundle\EventListener\Entity\UserListener"})
26
 * @DoctrineAssert\UniqueEntity(fields={"email"})
27
 * @DoctrineAssert\UniqueEntity(fields={"username"})
28
 * @ApiResource(attributes={"order"={"username": "ASC"}})
29
 * @ApiFilter(DateFilter::class, properties={"lastLogin": DateFilter::EXCLUDE_NULL})
30
 * @ApiFilter(
31
 *     GroupFilter::class,
32
 *     arguments={
33
 *          "parameterName": "groups",
34
 *          "overrideDefaultGroups": true,
35
 *          "whitelist": {"user.read.mini"}
36
 *     }
37
 * )
38
 */
39
class User implements UserInterface, TimestampableInterface, SluggableInterface, PasswordAuthenticatedUserInterface
40
{
41
    use TimestampableTrait;
42
    use SluggableTrait;
43
44
    const ROLE_DEFAULT = 'ROLE_USER';
45
    const ROLE_ADMIN = 'ROLE_ADMIN';
46
    const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
47
48
    /**
49
     * @ORM\Id
50
     * @ORM\Column(type="integer")
51
     * @ORM\GeneratedValue(strategy="AUTO")
52
     */
53
    protected ?int $id = null;
54
55
    /**
56
     * @ORM\Column(type="string", length=50, unique=true)
57
     */
58
    protected string $username = '';
59
60
    /**
61
     * @ORM\Column(type="string", length=180, unique=true)
62
     */
63
    private string $email = '';
64
65
    /**
66
     * @ORM\Column(type="boolean")
67
     */
68
    protected bool $enabled = false;
69
70
    /**
71
     * @ORM\Column(type="array")
72
     */
73
    private array $roles = [];
74
75
    /**
76
     * @ORM\Column(name="password", type="string")
77
     */
78
    private ?string $password;
79
80
    /**
81
     * Plain password. Used for model validation. Must not be persisted.
82
     */
83
    protected string $plainPassword = '';
84
85
    /**
86
     * @ORM\Column(name="last_login",type="datetime", nullable=true)
87
     */
88
    protected ?DateTime $lastLogin = null;
89
90
    /**
91
     * @ORM\Column(name="confirmation_token",type="string", length=180, nullable=true, unique=true)
92
     */
93
    protected ?string $confirmationToken = null;
94
95
    /**
96
     * @ORM\Column(name="password_requested_at",type="datetime", nullable=true)
97
     */
98
    protected ?DateTime $passwordRequestedAt = null;
99
100
    /**
101
     * @ORM\Column(name="nbConnexion", type="integer", nullable=false)
102
     */
103
    protected int $nbConnexion = 0;
104
105
    /**
106
     * @ORM\Column(name="nbForumMessage", type="integer", nullable=false)
107
     */
108
    protected int $nbForumMessage = 0;
109
110
    /**
111
     * @ORM\Column(name="avatar", type="string", length=100, nullable=false)
112
     */
113
    protected string $avatar = 'default.png';
114
115
    /**
116
     * @ORM\Column(name="comment", type="text", length=100, nullable=true)
117
     */
118
    protected ?string $comment = null;
119
120
    /**
121
     * @ORM\Column(name="locale", type="string", length=2, nullable=true)
122
     */
123
    protected string $locale = 'en';
124
125
    /**
126
     * @ORM\Column(name="rules_accepted", type="boolean", nullable=false)
127
     */
128
    protected bool $rules_accepted = true;
129
130
    /**
131
     * @ORM\ManyToMany(targetEntity="ProjetNormandie\UserBundle\Entity\Group")
132
     * @ORM\JoinTable(name="user_group",
133
     *      joinColumns={@ORM\JoinColumn(name="userId", referencedColumnName="id")},
134
     *      inverseJoinColumns={@ORM\JoinColumn(name="groupId", referencedColumnName="id")}
135
     * )
136
     * @var Collection
137
     */
138
    protected $groups;
139
140
    /**
141
     * @ORM\OneToMany(targetEntity="ProjetNormandie\UserBundle\Entity\UserIp", mappedBy="user")
142
     */
143
    private $userIp;
144
145
    /**
146
     * @ORM\ManyToOne(targetEntity="ProjetNormandie\UserBundle\Entity\Status")
147
     * @ORM\JoinColumns({
148
     *   @ORM\JoinColumn(name="idStatus", referencedColumnName="id", nullable=false)
149
     * })
150
     */
151
    private $status;
152
153
    /**
154
     * @return int|null
155
     */
156
    public function getId(): ?int
157
    {
158
        return $this->id;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getUsername(): string
165
    {
166
        return $this->username;
167
    }
168
169
    /**
170
     * @param $username
171
     * @return User
172
     */
173
    public function setUsername($username): User
174
    {
175
        $this->username = $username;
176
        return $this;
177
    }
178
179
    /**
180
     * @return string|null
181
     */
182
    public function getEmail(): ?string
183
    {
184
        return $this->email;
185
    }
186
187
    /**
188
     * @param string $email
189
     * @return User
190
     */
191
    public function setEmail(string $email): User
192
    {
193
        $this->email = $email;
194
195
        return $this;
196
    }
197
198
    /**
199
     * @param $boolean
200
     * @return User
201
     */
202
    public function setEnabled($boolean): User
203
    {
204
        $this->enabled = (bool) $boolean;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @return bool
211
     */
212
    public function isEnabled(): bool
213
    {
214
        return $this->enabled;
215
    }
216
217
    /**
218
     * @return array|string[]
219
     */
220
    public function getRoles(): array
221
    {
222
        $roles = $this->roles;
223
224
        foreach ($this->getGroups() as $group) {
225
            $roles = array_merge($roles, $group->getRoles());
226
        }
227
228
        // we need to make sure to have at least one role
229
        $roles[] = 'ROLE_USER';
230
231
        return array_values(array_unique($roles));
232
    }
233
234
    /**
235
     * @param array $roles
236
     * @return User
237
     */
238
    public function setRoles(array $roles): User
239
    {
240
        $this->roles = $roles;
241
242
        return $this;
243
    }
244
245
    /**
246
     * @return string
247
     */
248
    public function getPassword(): ?string
249
    {
250
        return $this->password;
251
    }
252
253
    /**
254
     * @param string $password
255
     * @return User
256
     */
257
    public function setPassword(string $password): User
258
    {
259
        $this->password = $password;
260
261
        return $this;
262
    }
263
264
    /**
265
     * @return string
266
     */
267
    public function getPlainPassword(): ?string
268
    {
269
        return $this->plainPassword;
270
    }
271
272
273
    /**
274
     * @param $role
275
     * @return bool
276
     */
277
    public function hasRole($role): bool
278
    {
279
        return in_array(strtoupper($role), $this->getRoles(), true);
280
    }
281
282
    /**
283
     *
284
     */
285
    public function eraseCredentials()
286
    {
287
        // If you store any temporary, sensitive data on the user, clear it here
288
        // $this->plainPassword = null;
289
    }
290
291
    /**
292
     * @return DateTime|null
293
     */
294
    public function getLastLogin(): ?DateTime
295
    {
296
        return $this->lastLogin;
297
    }
298
299
    /**
300
     * @param DateTime|null $time
301
     * @return User
302
     */
303
    public function setLastLogin(DateTime $time = null) : User
304
    {
305
        $lastLogin = $this->getLastLogin();
306
        if (($lastLogin === null) || ($lastLogin->format('Y-m-d') != $time->format('Y-m-d'))) {
0 ignored issues
show
Bug introduced by
The method format() does not exist on null. ( Ignorable by Annotation )

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

306
        if (($lastLogin === null) || ($lastLogin->format('Y-m-d') != $time->/** @scrutinizer ignore-call */ format('Y-m-d'))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
307
            ++$this->nbConnexion;
308
        }
309
        $this->lastLogin = $time;
310
        return $this;
311
    }
312
313
    /**
314
     * @param $confirmationToken
315
     * @return User
316
     */
317
    public function setConfirmationToken($confirmationToken): User
318
    {
319
        $this->confirmationToken = $confirmationToken;
320
        return $this;
321
    }
322
323
    /**
324
     * @return string
325
     */
326
    public function getConfirmationToken(): ?string
327
    {
328
        return $this->confirmationToken;
329
    }
330
331
    /**
332
     * @param DateTime|null $date
333
     * @return User
334
     */
335
    public function setPasswordRequestedAt(DateTime $date = null): User
336
    {
337
        $this->passwordRequestedAt = $date;
338
        return $this;
339
    }
340
341
    /**
342
     * Gets the timestamp that the user requested a password reset.
343
     * @return null|DateTime
344
     */
345
    public function getPasswordRequestedAt(): ?DateTime
346
    {
347
        return $this->passwordRequestedAt;
348
    }
349
350
    /**
351
     * @param $ttl
352
     * @return bool
353
     */
354
    public function isPasswordRequestNonExpired($ttl): bool
355
    {
356
        return $this->getPasswordRequestedAt() instanceof DateTime &&
357
               $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
358
    }
359
360
    /**
361
     * @return string
362
     */
363
    public function getLocale(): string
364
    {
365
        return $this->locale;
366
    }
367
368
    /**
369
     * @param string $locale
370
     * @return User
371
     */
372
    public function setLocale(string $locale): User
373
    {
374
        $this->locale = $locale;
375
        return $this;
376
    }
377
378
    /**
379
     * @return int
380
     */
381
    public function getNbConnexion(): int
382
    {
383
        return $this->nbConnexion;
384
    }
385
386
    /**
387
     * @param int $nbConnexion
388
     * @return User
389
     */
390
    public function setNbConnexion(int $nbConnexion): User
391
    {
392
        $this->nbConnexion = $nbConnexion;
393
        return $this;
394
    }
395
396
    /**
397
     * @return int
398
     */
399
    public function getNbForumMessage(): int
400
    {
401
        return $this->nbForumMessage;
402
    }
403
404
    /**
405
     * @param int $nbForumMessage
406
     * @return User
407
     */
408
    public function setNbForumMessage(int $nbForumMessage): User
409
    {
410
        $this->nbForumMessage = $nbForumMessage;
411
        return $this;
412
    }
413
414
    /**
415
     * @return string
416
     */
417
    public function getAvatar(): string
418
    {
419
        return $this->avatar;
420
    }
421
422
    /**
423
     * @param string $avatar
424
     * @return User
425
     */
426
    public function setAvatar(string $avatar): User
427
    {
428
        $this->avatar = $avatar;
429
        return $this;
430
    }
431
432
    /**
433
     * @return string
434
     */
435
    public function getComment(): ?string
436
    {
437
        return $this->comment;
438
    }
439
440
    /**
441
     * @param string|null $comment
442
     * @return User
443
     */
444
    public function setComment(string $comment = null) : User
445
    {
446
        $this->comment = $comment;
447
        return $this;
448
    }
449
450
451
    /**
452
     * @return mixed
453
     */
454
    public function getUserIp()
455
    {
456
        return $this->userIp;
457
    }
458
459
    /**
460
     * Set status
461
     * @param Status|object|null $status
462
     * @return User
463
     */
464
    public function setStatus(Status $status = null): User
465
    {
466
        $this->status = $status;
467
        return $this;
468
    }
469
470
    /**
471
     * Get status
472
     * @return Status
473
     */
474
    public function getStatus(): Status
475
    {
476
        return $this->status;
477
    }
478
479
    /**
480
     * Set rules_accepted
481
     * @param bool $rules_accepted
482
     * @return User
483
     */
484
    public function setRulesAccepted(bool $rules_accepted): User
485
    {
486
        $this->rules_accepted = $rules_accepted;
487
        return $this;
488
    }
489
490
    /**
491
     * Get rules_accepted
492
     * @return bool
493
     */
494
    public function getRulesAccepted(): bool
495
    {
496
        return $this->rules_accepted;
497
    }
498
499
    /**
500
     * @param $groups
501
     * @return User
502
     */
503
    public function setGroups($groups): User
504
    {
505
        $this->groups = $groups;
506
        return $this;
507
    }
508
509
    /**
510
     * @return Collection
511
     */
512
    public function getGroups(): Collection
513
    {
514
        return $this->groups;
515
    }
516
517
    /**
518
     * @param $password
519
     * @return User
520
     */
521
    public function setPlainPassword($password): User
522
    {
523
        $this->plainPassword = $password;
524
        return $this;
525
    }
526
527
    /**
528
     * @param $group
529
     * @return User
530
     */
531
    public function addGroup($group): User
532
    {
533
        $this->groups[] = $group;
534
        return $this;
535
    }
536
537
     /**
538
     * @param Group $group
539
     */
540
    public function removeGroup(Group $group)
541
    {
542
        $this->groups->removeElement($group);
543
    }
544
545
546
    /**
547
     * @return string
548
     */
549
    public function __toString()
550
    {
551
        return sprintf('%s [%d]', $this->getUsername(), $this->getId());
552
    }
553
554
    /**
555
     * @param $role
556
     * @return User
557
     */
558
    public function addRole($role): User
559
    {
560
        $role = strtoupper($role);
561
        if ($role === static::ROLE_DEFAULT) {
562
            return $this;
563
        }
564
        if (!in_array($role, $this->roles, true)) {
565
            $this->roles[] = $role;
566
        }
567
        return $this;
568
    }
569
570
    /**
571
     * @param $role
572
     * @return User
573
     */
574
    public function removeRole($role): User
575
    {
576
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
577
            unset($this->roles[$key]);
578
            $this->roles = array_values($this->roles);
579
        }
580
        return $this;
581
    }
582
583
584
    /**
585
     * Returns an array of the fields used to generate the slug.
586
     * @return string[]
587
     */
588
    public function getSluggableFields(): array
589
    {
590
        return ['username'];
591
    }
592
}
593