Passed
Push — master ( 786b21...4a8f5f )
by Stone
06:47 queued 42s
created

User::setVerifiedHash()   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
namespace App\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
use Symfony\Component\Validator\Constraints as Assert;
9
use Gedmo\Mapping\Annotation as Gedmo;
10
11
/**
12
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
13
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
14
 * @UniqueEntity(fields={"userName"}, message="There is already an account with this username")
15
 * @UniqueEntity(fields={"verifiedHash"}, message="Hash already exists")
16
 */
17
class User extends AppEntity implements UserInterface
18
{
19
20
    const HASH_VALIDATION_TIME_LIMIT = 1; //number of days that the validation link is active
21
22
    /**
23
     * @ORM\Id()
24
     * @ORM\GeneratedValue()
25
     * @ORM\Column(type="integer")
26
     */
27
    private $id;
28
29
    /**
30
     * @ORM\Column(type="string", length=180, unique=true)
31
     */
32
    private $email;
33
34
    /**
35
     * @ORM\Column(type="json")
36
     */
37
    private $roles = [];
38
39
    /**
40
     * @var string The hashed password
41
     * @ORM\Column(type="string")
42
     */
43
    private $password;
44
45
    /**
46
     * @ORM\Column(type="string", length=255, unique=true)
47
     * @Assert\Type(type="alnum")
48
     */
49
    private $userName;
50
51
    /**
52
     * @ORM\Column(type="string", length=255, nullable=true)
53
     */
54
    private $image;
55
56
    /**
57
     * @ORM\Column(type="boolean", options={"default":"0"})
58
     */
59
    private $verified = false;
60
61
    /**
62
     * @ORM\Column(type="string", length=255, unique=true)
63
     */
64
    private $verifiedHash;
65
66
    /**
67
     * @ORM\Column(type="datetime")
68
     * @Gedmo\Timestampable(on="create")
69
     */
70
    private $verifiedDateTime;
71
72
    public function getId(): ?int
73
    {
74
        return $this->id;
75
    }
76
77
    public function getEmail(): ?string
78
    {
79
        return $this->email;
80
    }
81
82
    public function setEmail(string $email): self
83
    {
84
        $this->email = $email;
85
86
        return $this;
87
    }
88
89
    /**
90
     * A visual identifier that represents this user.
91
     *
92
     * @see UserInterface
93
     */
94
    public function getUsername(): string
95
    {
96
        return (string)$this->userName;
97
    }
98
99
    /**
100
     * @see UserInterface
101
     */
102
    public function getRoles(): array
103
    {
104
        $roles = $this->roles;
105
        // guarantee every user at least has ROLE_USER
106
        $roles[] = 'ROLE_USER';
107
108
        return array_unique($roles);
109
    }
110
111
    public function setRoles(array $roles): self
112
    {
113
        $this->roles = $roles;
114
115
        return $this;
116
    }
117
118
    /**
119
     * @see UserInterface
120
     */
121
    public function getPassword(): string
122
    {
123
        return (string)$this->password;
124
    }
125
126
    public function setPassword(string $password): self
127
    {
128
        $this->password = $password;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @see UserInterface
135
     */
136
    public function getSalt()
137
    {
138
        // not needed when using the "bcrypt" algorithm in security.yaml
139
    }
140
141
    /**
142
     * @see UserInterface
143
     */
144
    public function eraseCredentials()
145
    {
146
        // If you store any temporary, sensitive data on the user, clear it here
147
        // $this->plainPassword = null;
148
    }
149
150
    public function setUserName(string $userName): self
151
    {
152
        $this->userName = $userName;
153
154
        return $this;
155
    }
156
157
    public function getImage(): ?string
158
    {
159
        return $this->image;
160
    }
161
162
    public function setImage(?string $image): self
163
    {
164
        $this->image = $image;
165
166
        return $this;
167
    }
168
169
    public function getVerified(): ?bool
170
    {
171
        return $this->verified;
172
    }
173
174
    public function setVerified(bool $verified): self
175
    {
176
        $this->verified = $verified;
177
178
        return $this;
179
    }
180
181
    public function getVerifiedHash(): ?string
182
    {
183
        return $this->verifiedHash;
184
    }
185
186
    public function setVerifiedHash(string $verifiedHash): self
187
    {
188
        $this->verifiedHash = $verifiedHash;
189
190
        return $this;
191
    }
192
193
    public function getVerifiedDateTime(): ?\DateTimeInterface
194
    {
195
        return $this->verifiedDateTime;
196
    }
197
198
    public function setVerifiedDateTime(\DateTimeInterface $verifiedDateTime): self
199
    {
200
        $this->verifiedDateTime = $verifiedDateTime;
201
202
        return $this;
203
    }
204
205
    /**
206
     * @return bool
207
     * @throws \Exception
208
     */
209
    public function isVerifiedDateTimeValid():bool
210
    {
211
        $now = new \DateTime();
212
        return $now->getTimestamp() - $this->getVerifiedDateTime()->getTimestamp() <= self::HASH_VALIDATION_TIME_LIMIT * 60 * 60 * 24;
213
    }
214
215
    /**
216
     * @param string $hash
217
     * @return bool
218
     */
219
    public function isHashValid(string $hash):bool
220
    {
221
        return $this->getVerifiedHash() === $hash;
222
    }
223
}
224