Passed
Push — master ( fd01be...a8be6a )
by Gerard
02:31
created

User::getConfirmationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gbere\SimpleAuth\Entity;
6
7
use DateTime;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Collection;
10
use Doctrine\ORM\Mapping as ORM;
11
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * @ORM\Table(name="gbere_auth_user")
17
 * @ORM\HasLifecycleCallbacks()
18
 * @ORM\Entity(repositoryClass="Gbere\SimpleAuth\Repository\UserRepository")
19
 * @UniqueEntity("email")
20
 */
21
class User implements UserInterface
22
{
23
    /**
24
     * @var int
25
     * @ORM\Id()
26
     * @ORM\GeneratedValue()
27
     * @ORM\Column(type="integer")
28
     */
29
    private $id;
30
31
    /**
32
     * @var string
33
     * @ORM\Column(type="string", length=180, unique=true)
34
     * @Assert\NotBlank()
35
     * @Assert\Email()
36
     */
37
    private $email;
38
39
    /**
40
     * @var Collection
41
     * @ORM\ManyToMany(targetEntity="Gbere\SimpleAuth\Entity\Role", inversedBy="users")
42
     * @ORM\JoinTable(name="gbere_auth_user_role")
43
     */
44
    private $roles;
45
46
    /**
47
     * @var string The hashed password
48
     * @ORM\Column(type="string")
49
     * @Assert\NotBlank()
50
     */
51
    private $password;
52
53
    /**
54
     * @var DateTime
55
     * @ORM\Column(type="datetime")
56
     */
57
    private $createdAt;
58
59
    /**
60
     * @var bool
61
     * @ORM\Column(type="boolean")
62
     */
63
    private $enabled;
64
65
    /**
66
     * @var null|string
67
     * @ORM\Column(type="string", length=100, nullable=true, unique=true)
68
     */
69
    private $confirmationToken;
70
71
    public function __construct()
72
    {
73
        $this->enabled = false;
74
        $this->roles = new ArrayCollection();
75
    }
76
77
    public function getId(): ?int
78
    {
79
        return $this->id;
80
    }
81
82
    public function getEmail(): ?string
83
    {
84
        return $this->email;
85
    }
86
87
    public function setEmail(string $email): self
88
    {
89
        $this->email = $email;
90
91
        return $this;
92
    }
93
94
    /**
95
     * A visual identifier that represents this user.
96
     *
97
     * @see UserInterface
98
     */
99
    public function getUsername(): string
100
    {
101
        return $this->email;
102
    }
103
104
    /**
105
     * @return array<Role|string>
106
     */
107
    public function getRoles(): array
108
    {
109
        $roles = [];
110
        /** @var Role $role */
111
        foreach ($this->roles as $role) {
112
            $roles[] = $role->getName();
113
        }
114
115
        return $roles;
116
    }
117
118
    public function getRolesCollection(): Collection
119
    {
120
        return $this->roles;
121
    }
122
123
    public function addRoleEntity(Role $role): self
124
    {
125
        if (false === $this->roles->contains($role)) {
126
            $this->roles[] = $role;
127
        }
128
129
        return $this;
130
    }
131
132
    public function removeRoleEntity(Role $role): self
133
    {
134
        if ($this->roles->contains($role)) {
135
            $this->roles->removeElement($role);
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * @see UserInterface
143
     */
144
    public function getPassword(): ?string
145
    {
146
        return $this->password;
147
    }
148
149
    public function setPassword(string $password): self
150
    {
151
        $this->password = $password;
152
153
        return $this;
154
    }
155
156
    public function getCreatedAt(): ?DateTime
157
    {
158
        return $this->createdAt;
159
    }
160
161
    public function setCreatedAt(DateTime $createdAt): self
162
    {
163
        $this->createdAt = $createdAt;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @ORM\PrePersist
170
     */
171
    public function setCreatedAtValue(): void
172
    {
173
        $this->createdAt = new DateTime();
174
    }
175
176
    /**
177
     * @see UserInterface
178
     */
179
    public function getSalt(): ?string
180
    {
181
        // not needed when using the "bcrypt" algorithm in security.yaml
182
        return null;
183
    }
184
185
    /**
186
     * @see UserInterface
187
     */
188
    public function eraseCredentials(): void
189
    {
190
        // If you store any temporary, sensitive data on the user, clear it here
191
        // $this->plainPassword = null;
192
    }
193
194
    public function isEnabled(): ?bool
195
    {
196
        return $this->enabled;
197
    }
198
199
    public function hasEnabled(bool $enabled): self
200
    {
201
        $this->enabled = $enabled;
202
203
        return $this;
204
    }
205
206
    public function getConfirmationToken(): ?string
207
    {
208
        return $this->confirmationToken;
209
    }
210
211
    public function setConfirmationToken(?string $confirmationToken): self
212
    {
213
        $this->confirmationToken = $confirmationToken;
214
215
        return $this;
216
    }
217
218
    /**
219
     * @throws \Exception
220
     */
221
    public function generateToken(): void
222
    {
223
        $this->setConfirmationToken(bin2hex(random_bytes(50)));
224
    }
225
}
226