Completed
Push — master ( 577a52...db05bc )
by Valery
09:02
created

User::getConfirmationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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