Completed
Push — master ( cf207c...9dbd6f )
by Valery
08:47
created

User::isPasswordRequestNonExpired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Entity;
6
7
use App\Entity\Traits\EntityIdTrait;
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\Entity(repositoryClass="App\Repository\UserRepository")
17
 * @ORM\Table(name="users")
18
 * @UniqueEntity("email")
19
 */
20
class User implements UserInterface, \Serializable
21
{
22
    use EntityIdTrait;
23
    /**
24
     * Requests older than this many seconds will be considered expired.
25
     */
26
    public const RETRY_TTL = 3600;
27
    /**
28
     * Maximum time that the confirmation token will be valid.
29
     */
30
    public const TOKEN_TTL = 43200;
31
32
    /**
33
     * @var string
34
     *
35
     * @ORM\Column(type="string")
36
     * @Assert\NotBlank()
37
     */
38
    private $fullName;
39
    /**
40
     * @var string
41
     *
42
     * @ORM\Column(type="string", unique=true)
43
     * @Assert\NotBlank()
44
     * @Assert\Length(min=2, max=50)
45
     */
46
    private $username;
47
    /**
48
     * @var string
49
     *
50
     * @ORM\Column(type="string")
51
     * @Assert\NotBlank()
52
     */
53
    private $phone;
54
    /**
55
     * @var string
56
     *
57
     * @ORM\Column(type="string", unique=true)
58
     * @Assert\Email()
59
     */
60
    private $email;
61
    /**
62
     * @var string
63
     *
64
     * @ORM\Column(type="string")
65
     */
66
    private $password;
67
    /**
68
     * @var array
69
     *
70
     * @ORM\Column(type="json")
71
     */
72
    private $roles = [];
73
74
    /**
75
     * @ORM\OneToMany(targetEntity="App\Entity\Property", mappedBy="author")
76
     */
77
    private $properties;
78
79
    /**
80
     * @ORM\Column(type="string", length=255, nullable=true)
81
     */
82
    private $confirmation_token;
83
84
    /**
85
     * @ORM\Column(type="datetime", nullable=true)
86
     */
87
    private $password_requested_at;
88
89
    public function __construct()
90
    {
91
        $this->properties = new ArrayCollection();
92
    }
93
94
    public function setFullName(string $fullName): void
95
    {
96
        $this->fullName = $fullName;
97
    }
98
99
    public function getFullName(): ?string
100
    {
101
        return $this->fullName;
102
    }
103
104
    public function getUsername(): ?string
105
    {
106
        return $this->username;
107
    }
108
109
    public function setUsername(string $username): void
110
    {
111
        $this->username = $username;
112
    }
113
114
    public function getPhone(): ?string
115
    {
116
        return $this->phone;
117
    }
118
119
    public function setPhone(string $phone): void
120
    {
121
        $this->phone = $phone;
122
    }
123
124
    public function getEmail(): ?string
125
    {
126
        return $this->email;
127
    }
128
129
    public function setEmail(string $email): void
130
    {
131
        $this->email = $email;
132
    }
133
134
    public function getPassword(): ?string
135
    {
136
        return $this->password;
137
    }
138
139
    public function setPassword(string $password): void
140
    {
141
        $this->password = $password;
142
    }
143
144
    /**
145
     * Returns the roles or permissions granted to the user for security.
146
     */
147
    public function getRoles(): array
148
    {
149
        $roles = $this->roles;
150
        // guarantees that a user always has at least one role for security
151
        if (empty($roles)) {
152
            $roles[] = 'ROLE_USER';
153
        }
154
155
        return array_unique($roles);
156
    }
157
158
    public function setRoles(array $roles): void
159
    {
160
        $this->roles = $roles;
161
    }
162
163
    /**
164
     * Returns the salt that was originally used to encode the password.
165
     *
166
     * {@inheritdoc}
167
     */
168
    public function getSalt(): ?string
169
    {
170
        // See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html
171
        // we're using bcrypt in security.yml to encode the password, so
172
        // the salt value is built-in and you don't have to generate one
173
        return null;
174
    }
175
176
    /**
177
     * Removes sensitive data from the user.
178
     *
179
     * {@inheritdoc}
180
     */
181
    public function eraseCredentials(): void
182
    {
183
        // if you had a plainPassword property, you'd nullify it here
184
        // $this->plainPassword = null;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function serialize(): string
191
    {
192
        // add $this->salt too if you don't use Bcrypt or Argon2i
193
        return serialize([$this->id, $this->username, $this->password]);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function unserialize($serialized): void
200
    {
201
        // add $this->salt too if you don't use Bcrypt or Argon2i
202
        [$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
203
    }
204
205
    public function getProperties(): Collection
206
    {
207
        return $this->properties;
208
    }
209
210
    public function addProperty(Property $property): self
211
    {
212
        if (!$this->properties->contains($property)) {
213
            $this->properties[] = $property;
214
            $property->setAuthor($this);
215
        }
216
217
        return $this;
218
    }
219
220
    public function removeProperty(Property $property): self
221
    {
222
        if ($this->properties->contains($property)) {
223
            $this->properties->removeElement($property);
224
            // set the owning side to null (unless already changed)
225
            if ($property->getAuthor() === $this) {
226
                $property->setAuthor(null);
227
            }
228
        }
229
230
        return $this;
231
    }
232
233
    public function getConfirmationToken(): ?string
234
    {
235
        return $this->confirmation_token;
236
    }
237
238
    public function setConfirmationToken(?string $confirmation_token): self
239
    {
240
        $this->confirmation_token = $confirmation_token;
241
242
        return $this;
243
    }
244
245
    public function getPasswordRequestedAt(): ?\DateTimeInterface
246
    {
247
        return $this->password_requested_at;
248
    }
249
250
    public function setPasswordRequestedAt(?\DateTimeInterface $password_requested_at): self
251
    {
252
        $this->password_requested_at = $password_requested_at;
253
254
        return $this;
255
    }
256
257
    /**
258
     * Checks whether the password reset request has expired.
259
     */
260
    public function isPasswordRequestNonExpired(int $ttl): bool
261
    {
262
        return $this->getPasswordRequestedAt() instanceof \DateTime &&
263
            $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
264
    }
265
}
266