Completed
Push — SF4 ( 2d2f19...1dd003 )
by Laurent
03:26
created

User::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * Entity User.
5
 *
6
 * PHP Version 7
7
 *
8
 * @author    Quétier Laurent <[email protected]>
9
 * @copyright 2018 Dev-Int GLSR
10
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
11
 *
12
 * @version GIT: $Id$
13
 *
14
 * @link https://github.com/Dev-Int/glsr
15
 */
16
17
namespace App\Entity\Staff;
18
19
use Doctrine\ORM\Mapping as ORM;
20
use FOS\UserBundle\Model\User as BaseUser;
21
22
/**
23
 * User Entity.
24
 *
25
 * @category Entity
26
 *
27
 * @ORM\Table(name="fos_users")
28
 * @ORM\Entity(repositoryClass="App\Repository\Staff\UserRepository")
29
 */
30
class User extends BaseUser implements \Symfony\Component\Security\Core\User\UserInterface, \Symfony\Component\Security\Core\User\EquatableInterface
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 148 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
31
{
32
    /**
33
     * @ORM\Id
34
     * @ORM\Column(type="integer")
35
     * @ORM\GeneratedValue(strategy="AUTO")
36
     */
37
    protected $id;
38
39
    /**
40
     * User constructor.
41
     */
42
    public function __construct()
43
    {
44
        parent::__construct();
45
    }
46
47
    /**
48
     * This method lets you do "echo $user".
49
     * <p> So, to "show" $user,
50
     * PHP will actually show the return of this method. <br />
51
     * Here, the username, so "echo $user"
52
     * is equivalent to "echo $user->getUsername()" </p>.
53
     *
54
     * @return string
55
     */
56
    public function __toString(): string
57
    {
58
        return (string) $this->getUsername();
59
    }
60
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function addRole($role)
66
    {
67
        $role = strtoupper($role);
68
        if ($role === static::ROLE_DEFAULT) {
69
            return $this;
70
        }
71
72
        if (!in_array($role, $this->roles, true)) {
73
            $this->roles[] = $role;
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function serialize()
83
    {
84
        return serialize(array(
85
            $this->password,
86
            $this->salt,
87
            $this->usernameCanonical,
88
            $this->username,
89
            $this->enabled,
90
            $this->id,
91
            $this->email,
92
            $this->emailCanonical,
93
        ));
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function unserialize($serialized)
100
    {
101
        $data = unserialize($serialized);
102
103
        if (13 === count($data)) {
104
            // Unserializing a User object from 1.3.x
105
            unset($data[4], $data[5], $data[6], $data[9], $data[10]);
106
            $data = array_values($data);
107
        } elseif (11 === count($data)) {
108
            // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
109
            unset($data[4], $data[7], $data[8]);
110
            $data = array_values($data);
111
        }
112
113
        list(
114
            $this->password,
115
            $this->salt,
116
            $this->usernameCanonical,
117
            $this->username,
118
            $this->enabled,
119
            $this->id,
120
            $this->email,
121
            $this->emailCanonical
122
        ) = $data;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function eraseCredentials()
129
    {
130
        $this->plainPassword = null;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getId()
137
    {
138
        return $this->id;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getUsername()
145
    {
146
        return $this->username;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getUsernameCanonical()
153
    {
154
        return $this->usernameCanonical;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getSalt()
161
    {
162
        return $this->salt;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getEmail()
169
    {
170
        return $this->email;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function getEmailCanonical()
177
    {
178
        return $this->emailCanonical;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function getPassword()
185
    {
186
        return $this->password;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function getPlainPassword()
193
    {
194
        return $this->plainPassword;
195
    }
196
197
    /**
198
     * Gets the last login time.
199
     *
200
     * @return \DateTime|null
201
     */
202
    public function getLastLogin()
203
    {
204
        return $this->lastLogin;
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function getConfirmationToken()
211
    {
212
        return $this->confirmationToken;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function getRoles()
219
    {
220
        $roles = $this->roles;
221
222
//        foreach ($this->getGroups() as $group) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
223
//            $roles = array_merge($roles, $group->getRoles());
224
//        }
225
226
        // we need to make sure to have at least one role
227
        $roles[] = static::ROLE_DEFAULT;
228
229
        return array_unique($roles);
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function hasRole($role)
236
    {
237
        return in_array(strtoupper($role), $this->getRoles(), true);
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function isAccountNonExpired()
244
    {
245
        return true;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function isAccountNonLocked()
252
    {
253
        return true;
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function isCredentialsNonExpired()
260
    {
261
        return true;
262
    }
263
264
    public function isEnabled()
265
    {
266
        return $this->enabled;
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272
    public function isSuperAdmin()
273
    {
274
        return $this->hasRole(static::ROLE_SUPER_ADMIN);
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280
    public function removeRole($role)
281
    {
282
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
283
            unset($this->roles[$key]);
284
            $this->roles = array_values($this->roles);
285
        }
286
287
        return $this;
288
    }
289
290
    /**
291
     * {@inheritdoc}
292
     */
293
    public function setUsername($username)
294
    {
295
        $this->username = $username;
296
297
        return $this;
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303
    public function setUsernameCanonical($userCanonical)
304
    {
305
        $this->usernameCanonical = $userCanonical;
306
307
        return $this;
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313
    public function setSalt($salt)
314
    {
315
        $this->salt = $salt;
316
317
        return $this;
318
    }
319
320
    /**
321
     * {@inheritdoc}
322
     */
323
    public function setEmail($email)
324
    {
325
        $this->email = $email;
326
327
        return $this;
328
    }
329
330
    /**
331
     * {@inheritdoc}
332
     */
333
    public function setEmailCanonical($emailCanonical)
334
    {
335
        $this->emailCanonical = $emailCanonical;
336
337
        return $this;
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function setEnabled($boolean)
344
    {
345
        $this->enabled = (bool) $boolean;
346
347
        return $this;
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function setPassword($password)
354
    {
355
        $this->password = $password;
356
357
        return $this;
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function setSuperAdmin($boolean)
364
    {
365
        if (true === $boolean) {
366
            $this->addRole(static::ROLE_SUPER_ADMIN);
367
        } else {
368
            $this->removeRole(static::ROLE_SUPER_ADMIN);
369
        }
370
371
        return $this;
372
    }
373
374
    /**
375
     * {@inheritdoc}
376
     */
377
    public function setPlainPassword($password)
378
    {
379
        $this->plainPassword = $password;
380
381
        return $this;
382
    }
383
384
    /**
385
     * {@inheritdoc}
386
     */
387
    public function setLastLogin(\DateTime $time = null)
388
    {
389
        $this->lastLogin = $time;
390
391
        return $this;
392
    }
393
394
    /**
395
     * {@inheritdoc}
396
     */
397
    public function setConfirmationToken($confirmationToken)
398
    {
399
        $this->confirmationToken = $confirmationToken;
400
401
        return $this;
402
    }
403
404
    /**
405
     * {@inheritdoc}
406
     */
407
    public function setPasswordRequestedAt(\DateTime $date = null)
408
    {
409
        $this->passwordRequestedAt = $date;
410
411
        return $this;
412
    }
413
414
    /**
415
     * Gets the timestamp that the user requested a password reset.
416
     *
417
     * @return null|\DateTime
418
     */
419
    public function getPasswordRequestedAt()
420
    {
421
        return $this->passwordRequestedAt;
422
    }
423
424
    /**
425
     * {@inheritdoc}
426
     */
427
    public function isPasswordRequestNonExpired($ttl)
428
    {
429
        return $this->getPasswordRequestedAt() instanceof \DateTime &&
430
               $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
431
    }
432
433
    /**
434
     * {@inheritdoc}
435
     */
436
    public function setRoles(array $roles)
437
    {
438
        $this->roles = array();
439
440
        foreach ($roles as $role) {
441
            $this->addRole($role);
442
        }
443
444
        return $this;
445
    }
446
447
    public function isEqualTo(\Symfony\Component\Security\Core\User\UserInterface $user)
448
    {
449
        $return = true;
450
451
        if ($this->password !== $user->getPassword()) {
452
            $return = false;
453
        }
454
455
        if ($this->username !== $user->getUsername()) {
456
            $return = false;
457
        }
458
459
        return $return;
460
    }
461
}
462