Test Failed
Branch master (e769ca)
by Stone
05:52
created

User::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.9332
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
use Symfony\Component\HttpFoundation\File\File;
11
use Vich\UploaderBundle\Mapping\Annotation as Vich;
12
13
/**
14
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
15
 * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
16
 * @UniqueEntity(fields={"userName"}, message="There is already an account with this username")
17
 * @UniqueEntity(fields={"verifiedHash"}, message="Hash already exists")
18
 * @Vich\Uploadable
19
 */
20
class User extends AppEntity implements UserInterface, \Serializable
21
{
22
23
    const HASH_VALIDATION_TIME_LIMIT = 1; //number of days that the validation link is active
24
25
    /**
26
     * @ORM\Id()
27
     * @ORM\GeneratedValue()
28
     * @ORM\Column(type="integer")
29
     */
30
    private $id;
31
32
    /**
33
     * @ORM\Column(type="string", length=180, unique=true)
34
     */
35
    private $email;
36
37
    /**
38
     * @ORM\Column(type="json")
39
     */
40
    private $roles = [];
41
42
    /**
43
     * @var string The hashed password
44
     * @ORM\Column(type="string")
45
     */
46
    private $password;
47
48
    /**
49
     * @ORM\Column(type="string", length=255, unique=true)
50
     * @Assert\Type(type="alnum")
51
     */
52
    private $userName;
53
54
    /**
55
     * @ORM\Column(type="string", length=255)
56
     * @var string
57
     */
58
    private $image = "";
59
60
    /**
61
     * @Vich\UploadableField(mapping="user_images", fileNameProperty="image")
62
     * @var File
63
     */
64
    private $imageFile;
65
66
    /**
67
     * @ORM\Column(type="boolean", options={"default":"0"})
68
     */
69
    private $verified = false;
70
71
    /**
72
     * @ORM\Column(type="string", length=255, unique=true)
73
     */
74
    private $verifiedHash;
75
76
    /**
77
     * @ORM\Column(type="datetime")
78
     * @Gedmo\Timestampable(on="create")
79
     */
80
    private $verifiedDateTime;
81
82
    /**
83
     * @ORM\Column(type="datetime")
84
     * @Gedmo\Timestampable(on="update")
85
     */
86
    private $updatedAt;
87
88
    public function getId(): ?int
89
    {
90
        return $this->id;
91
    }
92
93
    public function getEmail(): ?string
94
    {
95
        return $this->email;
96
    }
97
98
    public function setEmail(string $email): self
99
    {
100
        $this->email = $email;
101
102
        return $this;
103
    }
104
105
    /**
106
     * A visual identifier that represents this user.
107
     *
108
     * @see UserInterface
109
     */
110
    public function getUsername(): string
111
    {
112
        return (string)$this->userName;
113
    }
114
115
    /**
116
     * @see UserInterface
117
     */
118
    public function getRoles(): array
119
    {
120
        $roles = $this->roles;
121
        // guarantee every user at least has ROLE_USER
122
        $roles[] = 'ROLE_USER';
123
124
        return array_unique($roles);
125
    }
126
127
    public function setRoles(array $roles): self
128
    {
129
        $this->roles = $roles;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @see UserInterface
136
     */
137
    public function getPassword(): string
138
    {
139
        return (string)$this->password;
140
    }
141
142
    public function setPassword(string $password): self
143
    {
144
        $this->password = $password;
145
146
        return $this;
147
    }
148
149
    /**
150
     * @see UserInterface
151
     */
152
    public function getSalt()
153
    {
154
        // not needed when using the "bcrypt" algorithm in security.yaml
155
    }
156
157
    /**
158
     * @see UserInterface
159
     */
160
    public function eraseCredentials()
161
    {
162
        // If you store any temporary, sensitive data on the user, clear it here
163
        // $this->plainPassword = null;
164
    }
165
166
    public function setUserName(string $userName): self
167
    {
168
        $this->userName = $userName;
169
170
        return $this;
171
    }
172
173
    public function getImage()
174
    {
175
        return $this->image;
176
    }
177
178
    public function setImage($image): self
179
    {
180
        $this->image = $image;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return File|null
187
     */
188
    public function getImageFile(): ?File
189
    {
190
        return $this->imageFile;
191
    }
192
193
    /**
194
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $imageFile
195
     * @return User
196
     * @throws \Exception
197
     */
198
    public function setImageFile(File $imageFile = null): self
199
    {
200
        $this->imageFile = $imageFile;
201
202
        // VERY IMPORTANT:
203
        // It is required that at least one field changes if you are using Doctrine,
204
        // otherwise the event listeners won't be called and the file is lost
205
        if ($imageFile) {
206
            // if 'updatedAt' is not defined in your entity, use another property
207
//            $this->updatedAt = new \DateTime('now');
208
            $this->updatedAt = new \DateTimeImmutable();
209
        }
210
211
        return $this;
212
    }
213
214
    /**
215
     * @return \DateTimeInterface|null
216
     */
217
    public function getUpdatedAt(): ?\DateTimeInterface
218
    {
219
        return $this->updatedAt;
220
    }
221
222
    /**
223
     * @param mixed $updatedAt
224
     * @return User
225
     */
226
    public function setUpdatedAt($updatedAt): self
227
    {
228
        $this->updatedAt = $updatedAt;
229
230
        return $this;
231
    }
232
233
    public function getVerified(): ?bool
234
    {
235
        return $this->verified;
236
    }
237
238
    public function setVerified(bool $verified): self
239
    {
240
        $this->verified = $verified;
241
242
        return $this;
243
    }
244
245
    public function getVerifiedHash(): ?string
246
    {
247
        return $this->verifiedHash;
248
    }
249
250
    public function setVerifiedHash(string $verifiedHash): self
251
    {
252
        $this->verifiedHash = $verifiedHash;
253
254
        return $this;
255
    }
256
257
    public function getVerifiedDateTime(): ?\DateTimeInterface
258
    {
259
        return $this->verifiedDateTime;
260
    }
261
262
    public function setVerifiedDateTime(\DateTimeInterface $verifiedDateTime): self
263
    {
264
        $this->verifiedDateTime = $verifiedDateTime;
265
266
        return $this;
267
    }
268
269
    /**
270
     * @return bool
271
     * @throws \Exception
272
     */
273
    public function isVerifiedDateTimeValid(): bool
274
    {
275
        $now = new \DateTime();
276
        return $now->getTimestamp() - $this->getVerifiedDateTime()->getTimestamp() <= self::HASH_VALIDATION_TIME_LIMIT * 60 * 60 * 24;
277
    }
278
279
    /**
280
     * @param string $hash
281
     * @return bool
282
     */
283
    public function isHashValid(string $hash): bool
284
    {
285
        return $this->getVerifiedHash() === $hash;
286
    }
287
288
289
    /**
290
     * String representation of object
291
     * @link https://php.net/manual/en/serializable.serialize.php
292
     * @return string the string representation of the object or null
293
     * @since 5.1.0
294
     */
295
    public function serialize()
296
    {
297
        return serialize([
298
            $this->id,
299
            $this->email,
300
            $this->roles,
301
            $this->password,
302
            $this->userName,
303
            $this->image,
304
            $this->verified,
305
            $this->verifiedDateTime,
306
            $this->updatedAt,
307
        ]);
308
    }
309
310
    /**
311
     * Constructs the object
312
     * @link https://php.net/manual/en/serializable.unserialize.php
313
     * @param string $serialized <p>
314
     * The string representation of the object.
315
     * </p>
316
     * @return void
317
     * @since 5.1.0
318
     */
319
    public function unserialize($serialized)
320
    {
321
        list(
322
            $this->id,
323
            $this->email,
324
            $this->roles,
325
            $this->password,
326
            $this->userName,
327
            $this->image,
328
            $this->verified,
329
            $this->verifiedDateTime,
330
            $this->updatedAt,
331
            )=unserialize($serialized, ['allowed_classes' => false]);
332
    }
333
}
334