Passed
Push — master ( a8be6a...01edf4 )
by Gerard
02:10
created

User::setPasswordRequestAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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 string|null
67
     * @ORM\Column(type="string", length=100, nullable=true, unique=true)
68
     */
69
    private $confirmationToken;
70
71
    /**
72
     * @var DateTime|null
73
     * @ORM\Column(type="datetime", nullable=true)
74
     */
75
    private $passwordRequestAt;
76
77
    public function __construct()
78
    {
79
        $this->enabled = false;
80
        $this->roles = new ArrayCollection();
81
    }
82
83
    public function getId(): ?int
84
    {
85
        return $this->id;
86
    }
87
88
    public function getEmail(): ?string
89
    {
90
        return $this->email;
91
    }
92
93
    public function setEmail(string $email): self
94
    {
95
        $this->email = $email;
96
97
        return $this;
98
    }
99
100
    /**
101
     * A visual identifier that represents this user.
102
     *
103
     * @see UserInterface
104
     */
105
    public function getUsername(): string
106
    {
107
        return $this->email;
108
    }
109
110
    /**
111
     * @return array<Role|string>
112
     */
113
    public function getRoles(): array
114
    {
115
        $roles = [];
116
        /** @var Role $role */
117
        foreach ($this->roles as $role) {
118
            $roles[] = $role->getName();
119
        }
120
121
        return $roles;
122
    }
123
124
    public function getRolesCollection(): Collection
125
    {
126
        return $this->roles;
127
    }
128
129
    public function addRoleEntity(Role $role): self
130
    {
131
        if (false === $this->roles->contains($role)) {
132
            $this->roles[] = $role;
133
        }
134
135
        return $this;
136
    }
137
138
    public function removeRoleEntity(Role $role): self
139
    {
140
        if ($this->roles->contains($role)) {
141
            $this->roles->removeElement($role);
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * @see UserInterface
149
     */
150
    public function getPassword(): ?string
151
    {
152
        return $this->password;
153
    }
154
155
    public function setPassword(string $password): self
156
    {
157
        $this->password = $password;
158
159
        return $this;
160
    }
161
162
    public function getCreatedAt(): ?DateTime
163
    {
164
        return $this->createdAt;
165
    }
166
167
    public function setCreatedAt(DateTime $createdAt): self
168
    {
169
        $this->createdAt = $createdAt;
170
171
        return $this;
172
    }
173
174
    /**
175
     * @ORM\PrePersist
176
     */
177
    public function setCreatedAtValue(): void
178
    {
179
        $this->createdAt = new DateTime();
180
    }
181
182
    /**
183
     * @see UserInterface
184
     */
185
    public function getSalt(): ?string
186
    {
187
        // not needed when using the "bcrypt" algorithm in security.yaml
188
        return null;
189
    }
190
191
    /**
192
     * @see UserInterface
193
     */
194
    public function eraseCredentials(): void
195
    {
196
        // If you store any temporary, sensitive data on the user, clear it here
197
        // $this->plainPassword = null;
198
    }
199
200
    public function isEnabled(): ?bool
201
    {
202
        return $this->enabled;
203
    }
204
205
    public function hasEnabled(bool $enabled): self
206
    {
207
        $this->enabled = $enabled;
208
209
        return $this;
210
    }
211
212
    public function getConfirmationToken(): ?string
213
    {
214
        return $this->confirmationToken;
215
    }
216
217
    public function setConfirmationToken(?string $confirmationToken): self
218
    {
219
        $this->confirmationToken = $confirmationToken;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @throws \Exception
226
     */
227
    public function generateToken(): void
228
    {
229
        $this->setConfirmationToken(bin2hex(random_bytes(50)));
230
    }
231
232
    public function getPasswordRequestAt(): ?DateTime
233
    {
234
        return $this->passwordRequestAt;
235
    }
236
237
    public function setPasswordRequestAt(?DateTime $passwordRequestAt): self
238
    {
239
        $this->passwordRequestAt = $passwordRequestAt;
240
241
        return $this;
242
    }
243
}
244