Completed
Push — master ( b046cb...82444a )
by Luis Ramón
02:44
created

User   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 553
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 40
c 6
b 0
f 0
lcom 3
cbo 4
dl 0
loc 553
rs 8.2608

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A __construct() 0 4 1
A setUserName() 0 6 1
A getUserName() 0 4 1
A setPassword() 0 6 1
A getPassword() 0 4 1
A setGlobalAdministrator() 0 6 1
A isGlobalAdministrator() 0 4 1
A setToken() 0 6 1
A getToken() 0 4 1
A setTokenExpiration() 0 6 1
A getTokenExpiration() 0 4 1
A setLastAccess() 0 6 1
A getLastAccess() 0 4 1
A setBlockedUntil() 0 6 1
A getBlockedUntil() 0 4 1
A removeMembership() 0 4 1
A getMemberships() 0 4 1
A validate() 0 12 3
A isAccountNonExpired() 0 4 1
A isAccountNonLocked() 0 4 2
A isCredentialsNonExpired() 0 4 1
A isEnabled() 0 4 1
A getRoles() 0 4 1
A getSalt() 0 4 1
A eraseCredentials() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Entity;
22
23
use Doctrine\Common\Collections\Collection;
24
use Doctrine\ORM\Mapping as ORM;
25
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
26
use Symfony\Component\Validator\Constraints as Assert;
27
use Symfony\Component\Validator\Context\ExecutionContextInterface;
28
29
/**
30
 * @ORM\Entity(repositoryClass="UserRepository")
31
 */
32
class User implements AdvancedUserInterface
33
{
34
    const GENDER_NEUTRAL = 0;
35
    const GENDER_MALE = 1;
36
    const GENDER_FEMALE = 2;
37
38
    /**
39
     * @ORM\Id
40
     * @ORM\GeneratedValue(strategy="AUTO")
41
     * @ORM\Column(type="integer")
42
     * @var int
43
     */
44
    private $id;
45
46
    /**
47
     * @ORM\Column(type="string", unique=true, nullable=true)
48
     * @var string
49
     */
50
    private $userName;
51
52
    /**
53
     * @ORM\Column(type="string", unique=true, nullable=true)
54
     * @var string
55
     */
56
    private $password;
57
58
    /**
59
     * @ORM\Column(type="string")
60
     * @var string
61
     */
62
    private $firstName;
63
64
    /**
65
     * @ORM\Column(type="string")
66
     * @var string
67
     */
68
    private $lastName;
69
70
    /**
71
     * @ORM\Column(type="boolean")
72
     * @var bool
73
     */
74
    private $enabled;
75
76
    /**
77
     * @ORM\Column(type="boolean")
78
     * @var bool
79
     */
80
    private $globalAdministrator;
81
82
    /**
83
     * @ORM\Column(type="string", unique=true, nullable=true)
84
     * @Assert\Email
85
     * @var string
86
     */
87
    private $emailAddress;
88
89
    /**
90
     * @ORM\Column(type="integer")
91
     * @var int
92
     */
93
    private $gender;
94
95
    /**
96
     * @ORM\Column(type="string", nullable=true)
97
     * @var string
98
     */
99
    private $token;
100
101
    /**
102
     * @ORM\Column(type="datetime", nullable=true)
103
     * @var \DateTime
104
     */
105
    private $tokenExpiration;
106
107
    /**
108
     * @ORM\Column(type="datetime", nullable=true)
109
     * @var \DateTime
110
     */
111
    private $lastAccess;
112
113
    /**
114
     * @ORM\Column(type="datetime", nullable=true)
115
     * @var \DateTime
116
     */
117
    private $blockedUntil;
118
119
    /**
120
     * @ORM\OneToMany(targetEntity="Membership", mappedBy="user")
121
     * @var Collection
122
     */
123
    private $memberships;
124
125
    /**
126
     * Convertir usuario en cadena
127
     */
128
    public function __toString()
129
    {
130
        return $this->getFirstName().' '.$this->getLastName();
131
    }
132
133
    /**
134
     * Constructor
135
     */
136
    public function __construct()
137
    {
138
        $this->memberships = new \Doctrine\Common\Collections\ArrayCollection();
139
    }
140
141
    /**
142
     * Get id
143
     *
144
     * @return integer
145
     */
146
    public function getId()
147
    {
148
        return $this->id;
149
    }
150
151
    /**
152
     * Set userName
153
     *
154
     * @param string $userName
155
     *
156
     * @return User
157
     */
158
    public function setUserName($userName)
159
    {
160
        $this->userName = $userName;
161
162
        return $this;
163
    }
164
165
    /**
166
     * Get userName
167
     *
168
     * @return string
169
     */
170
    public function getUserName()
171
    {
172
        return $this->userName;
173
    }
174
175
    /**
176
     * Set password
177
     *
178
     * @param string $password
179
     *
180
     * @return User
181
     */
182
    public function setPassword($password)
183
    {
184
        $this->password = $password;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Get password
191
     *
192
     * @return string
193
     */
194
    public function getPassword()
195
    {
196
        return $this->password;
197
    }
198
199
    /**
200
     * Set firstName
201
     *
202
     * @param string $firstName
203
     *
204
     * @return User
205
     */
206
    public function setFirstName($firstName)
207
    {
208
        $this->firstName = $firstName;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Get firstName
215
     *
216
     * @return string
217
     */
218
    public function getFirstName()
219
    {
220
        return $this->firstName;
221
    }
222
223
    /**
224
     * Set lastName
225
     *
226
     * @param string $lastName
227
     *
228
     * @return User
229
     */
230
    public function setLastName($lastName)
231
    {
232
        $this->lastName = $lastName;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Get lastName
239
     *
240
     * @return string
241
     */
242
    public function getLastName()
243
    {
244
        return $this->lastName;
245
    }
246
247
    /**
248
     * Set enabled
249
     *
250
     * @param boolean $enabled
251
     *
252
     * @return User
253
     */
254
    public function setEnabled($enabled)
255
    {
256
        $this->enabled = $enabled;
257
258
        return $this;
259
    }
260
261
262
    /**
263
     * Set globalAdmin
264
     *
265
     * @param boolean $globalAdministrator
266
     *
267
     * @return User
268
     */
269
    public function setGlobalAdministrator($globalAdministrator)
270
    {
271
        $this->globalAdministrator = $globalAdministrator;
272
273
        return $this;
274
    }
275
276
    /**
277
     * Get globalAdmin
278
     *
279
     * @return boolean
280
     */
281
    public function isGlobalAdministrator()
282
    {
283
        return $this->globalAdministrator;
284
    }
285
286
    /**
287
     * Set emailAddress
288
     *
289
     * @param string $emailAddress
290
     *
291
     * @return User
292
     */
293
    public function setEmailAddress($emailAddress)
294
    {
295
        $this->emailAddress = $emailAddress;
296
297
        return $this;
298
    }
299
300
    /**
301
     * Get emailAddress
302
     *
303
     * @return string
304
     */
305
    public function getEmailAddress()
306
    {
307
        return $this->emailAddress;
308
    }
309
310
    /**
311
     * Set gender
312
     *
313
     * @param integer $gender
314
     *
315
     * @return User
316
     */
317
    public function setGender($gender)
318
    {
319
        $this->gender = $gender;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Get gender
326
     *
327
     * @return integer
328
     */
329
    public function getGender()
330
    {
331
        return $this->gender;
332
    }
333
334
    /**
335
     * Set token
336
     *
337
     * @param string $token
338
     *
339
     * @return User
340
     */
341
    public function setToken($token)
342
    {
343
        $this->token = $token;
344
345
        return $this;
346
    }
347
348
    /**
349
     * Get token
350
     *
351
     * @return string
352
     */
353
    public function getToken()
354
    {
355
        return $this->token;
356
    }
357
358
359
    /**
360
     * Set tokenExpiration
361
     *
362
     * @param \DateTime $tokenExpiration
363
     *
364
     * @return User
365
     */
366
    public function setTokenExpiration($tokenExpiration)
367
    {
368
        $this->tokenExpiration = $tokenExpiration;
369
370
        return $this;
371
    }
372
373
    /**
374
     * Get tokenExpiration
375
     *
376
     * @return \DateTime
377
     */
378
    public function getTokenExpiration()
379
    {
380
        return $this->tokenExpiration;
381
    }
382
383
    /**
384
     * Set lastAccess
385
     *
386
     * @param \DateTime $lastAccess
387
     *
388
     * @return User
389
     */
390
    public function setLastAccess($lastAccess)
391
    {
392
        $this->lastAccess = $lastAccess;
393
394
        return $this;
395
    }
396
397
    /**
398
     * Get lastAccess
399
     *
400
     * @return \DateTime
401
     */
402
    public function getLastAccess()
403
    {
404
        return $this->lastAccess;
405
    }
406
407
    /**
408
     * Set blockedUntil
409
     *
410
     * @param \DateTime $blockedUntil
411
     *
412
     * @return User
413
     */
414
    public function setBlockedUntil($blockedUntil)
415
    {
416
        $this->blockedUntil = $blockedUntil;
417
418
        return $this;
419
    }
420
421
    /**
422
     * Get blockedUntil
423
     *
424
     * @return \DateTime
425
     */
426
    public function getBlockedUntil()
427
    {
428
        return $this->blockedUntil;
429
    }
430
431
    /**
432
     * Add membership
433
     *
434
     * @param Membership $membership
435
     *
436
     * @return User
437
     */
438
    public function addMembership(Membership $membership)
439
    {
440
        $this->memberships[] = $membership;
441
442
        return $this;
443
    }
444
445
    /**
446
     * Remove membership
447
     *
448
     * @param Membership $membership
449
     */
450
    public function removeMembership(Membership $membership)
451
    {
452
        $this->memberships->removeElement($membership);
453
    }
454
455
    /**
456
     * Get memberships
457
     *
458
     * @return \Doctrine\Common\Collections\Collection
459
     */
460
    public function getMemberships()
461
    {
462
        return $this->memberships;
463
    }
464
465
466
    /**
467
     * @Assert\Callback
468
     */
469
    public function validate(ExecutionContextInterface $context)
470
    {
471
        // comprobar si se ha especificado al menos el nombre de usuario o el correo electrónico
472
        if (!$this->getUsername() && !$this->getEmailAddress()) {
473
            $context->buildViolation('user.id.not_found')
474
                ->atPath('loginUsername')
475
                ->addViolation();
476
            $context->buildViolation('user.id.not_found')
477
                ->atPath('email')
478
                ->addViolation();
479
        }
480
    }
481
482
    /**
483
     * Checks whether the user's account has expired.
484
     *
485
     * Internally, if this method returns false, the authentication system
486
     * will throw an AccountExpiredException and prevent login.
487
     *
488
     * @return bool true if the user's account is non expired, false otherwise
489
     *
490
     * @see AccountExpiredException
491
     */
492
    public function isAccountNonExpired()
493
    {
494
        return true;
495
    }
496
497
    /**
498
     * Checks whether the user is locked.
499
     *
500
     * Internally, if this method returns false, the authentication system
501
     * will throw a LockedException and prevent login.
502
     *
503
     * @return bool true if the user is not locked, false otherwise
504
     *
505
     * @see LockedException
506
     */
507
    public function isAccountNonLocked()
508
    {
509
        return $this->getBlockedUntil() ? ($this->getBlockedUntil() <= new \DateTime()) : true;
510
    }
511
512
    /**
513
     * Checks whether the user's credentials (password) has expired.
514
     *
515
     * Internally, if this method returns false, the authentication system
516
     * will throw a CredentialsExpiredException and prevent login.
517
     *
518
     * @return bool true if the user's credentials are non expired, false otherwise
519
     *
520
     * @see CredentialsExpiredException
521
     */
522
    public function isCredentialsNonExpired()
523
    {
524
        return true;
525
    }
526
527
    /**
528
     * Checks whether the user is enabled.
529
     *
530
     * Internally, if this method returns false, the authentication system
531
     * will throw a DisabledException and prevent login.
532
     *
533
     * @return bool true if the user is enabled, false otherwise
534
     *
535
     * @see DisabledException
536
     */
537
    public function isEnabled()
538
    {
539
        return $this->enabled;
540
    }
541
542
    /**
543
     * Returns the roles granted to the user.
544
     *
545
     * <code>
546
     * public function getRoles()
547
     * {
548
     *     return array('ROLE_USER');
549
     * }
550
     * </code>
551
     *
552
     * Alternatively, the roles might be stored on a ``roles`` property,
553
     * and populated in any number of different ways when the user object
554
     * is created.
555
     *
556
     * @return string[] The user roles
557
     */
558
    public function getRoles()
559
    {
560
        return ['ROLE_USER'];
561
    }
562
563
    /**
564
     * Returns the salt that was originally used to encode the password.
565
     *
566
     * This can return null if the password was not encoded using a salt.
567
     *
568
     * @return string|null The salt
569
     */
570
    public function getSalt()
571
    {
572
        return null;
573
    }
574
575
    /**
576
     * Removes sensitive data from the user.
577
     *
578
     * This is important if, at any given point, sensitive information like
579
     * the plain-text password is stored on this object.
580
     */
581
    public function eraseCredentials()
582
    {
583
    }
584
}
585