AbstractUser::getToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Document;
4
5
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
6
use Symfony\Component\Security\Core\User\UserInterface;
7
use Symfony\Component\Serializer\Annotation\Groups;
8
9
/**
10
 * @MongoDB\MappedSuperclass
11
 * @MongoDB\HasLifecycleCallbacks
12
 */
13
abstract class AbstractUser implements UserInterface
14
{
15
    const ROLE_ADMIN = 'ROLE_ADMIN';
16
    const ROLE_MANAGER = 'ROLE_MANAGER';
17
    const ROLE_USER = 'ROLE_USER';
18
19
    /**
20
     * @MongoDB\Id
21
     * @Groups({"user_default", "user_full", "user_autocomplete", "environment_default", "environment_full"})
22
     */
23
    protected $id;
24
25
    /**
26
     * @MongoDB\Field(type="date")
27
     *
28
     * @Groups({"user_default", "user_full", "user_profile", "environment_default", "environment_full"})
29
     */
30
    protected $lastLogin;
31
32
    /**
33
     * @MongoDB\Field(type="date")
34
     *
35
     * @var \DateTime
36
     *
37
     * @Groups({"user_default", "user_full", "user_profile", "environment_default", "environment_full"})
38
     */
39
    protected $createdAt;
40
41
    /**
42
     * @MongoDB\Field(type="string")
43
     *
44
     * @Groups({"user_profile", "environment_full"})
45
     */
46
    protected $token;
47
48
    abstract public function getRoles();
49
50
    abstract public function getUsername();
51
52
    abstract public function getPassword();
53
54
    /**
55
     * @MongoDB\PrePersist
56
     */
57
    public function onPersist()
58
    {
59
        $this
60
            ->setCreatedAt(new \DateTime())
61
            ->generateToken();
62
    }
63
64
    public function generateToken(): self
65
    {
66
        $this->setToken(sha1(random_bytes(50)));
67
68
        return $this;
69
    }
70
71
    public function getSalt()
72
    {
73
        return null;
74
    }
75
76
    public function eraseCredentials()
77
    {
78
    }
79
80
    public function getId(): string
81
    {
82
        return $this->id;
83
    }
84
85
    public function getLastLogin(): ?\DateTime
86
    {
87
        return $this->lastLogin;
88
    }
89
90
    public function setLastLogin(\DateTime $lastLogin): self
91
    {
92
        $this->lastLogin = $lastLogin;
93
94
        return $this;
95
    }
96
97
    public function getCreatedAt(): \DateTime
98
    {
99
        return $this->createdAt;
100
    }
101
102
    public function setCreatedAt(\DateTime $createdAt): self
103
    {
104
        $this->createdAt = $createdAt;
105
106
        return $this;
107
    }
108
109
    public function getToken(): string
110
    {
111
        return $this->token;
112
    }
113
114
    public function setToken(string $token): self
115
    {
116
        $this->token = $token;
117
118
        return $this;
119
    }
120
}
121