User   C
last analyzed

Complexity

Total Complexity 63

Size/Duplication

Total Lines 618
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 63
c 3
b 0
f 0
lcom 2
cbo 1
dl 0
loc 618
rs 5.7533

55 Methods

Rating   Name   Duplication   Size   Complexity  
A getPassword() 0 4 1
A setPassword() 0 6 1
A getEmail() 0 4 1
A getLastAction() 0 4 1
A setLastAction() 0 6 1
A getUserAction() 0 4 1
A setUserAction() 0 6 1
A getUserActionId() 0 4 1
A setUserActionId() 0 6 1
A getLocationVector() 0 4 1
A setUserLocationVector() 0 6 1
A getMailCount() 0 4 1
A setMailCount() 0 6 1
A getUserMailId() 0 4 1
A setUserMailId() 0 6 1
A getK() 0 4 1
A setK() 0 6 1
A getKWallet() 0 4 1
A setKWallet() 0 6 1
A getListingAmount() 0 4 1
A setListingAmount() 0 6 1
A getListingOrder() 0 4 1
A setListingOrder() 0 6 1
A getHeaderId() 0 4 1
A setHeaderId() 0 6 1
A getHash() 0 4 1
A setHash() 0 6 1
A getAccLockout() 0 4 1
A setAccLockout() 0 6 1
A getPasswordChangePeriod() 0 4 1
A setPasswordChangePeriod() 0 6 1
A getLoginRetry() 0 4 1
A setLoginRetry() 0 6 1
A getDateLastLogin() 0 4 1
A setDateLastLogin() 0 6 1
A getDatePasswordChanged() 0 4 1
A setDatePasswordChanged() 0 6 1
A getDateLoginFailed() 0 4 1
A setDateLoginFailed() 0 6 1
A getMailNotify() 0 4 1
A setMailNotify() 0 6 1
A getNode() 0 4 1
A getMails() 0 4 1
A setMails() 0 6 1
A getUsername() 0 4 1
A getRoles() 0 6 1
A getSalt() 0 4 1
A eraseCredentials() 0 4 1
A isAccountNonExpired() 0 4 1
A isAccountNonLocked() 0 6 2
A isCredentialsNonExpired() 0 4 1
A isEnabled() 0 5 1
A getEncoderName() 0 10 4
A serialize() 0 9 1
A unserialize() 0 9 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
namespace AppBundle\Entity;
3
4
use DateTime;
5
use Symfony\Component\Security\Core\Encoder\EncoderAwareInterface;
6
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
7
8
/**
9
 * User
10
 */
11
class User extends AbstractEntity implements AdvancedUserInterface, EncoderAwareInterface, \Serializable
12
{
13
    const ENCODER_BCRYPT    = 'bcrypt';
14
    const ENCODER_MD5       = 'md5';
15
    const ENCODER_SHA       = 'salted_sha';
16
17
    //region Column properties
18
19
    /**
20
     * @var integer
21
     */
22
    protected $id;
23
24
    /**
25
     * @var string
26
     */
27
    protected $login;
28
29
    /**
30
     * @var string
31
     */
32
    protected $password;
33
34
    /**
35
     * @var string
36
     */
37
    protected $email;
38
39
    /**
40
     * @var DateTime
41
     */
42
    protected $lastAction = 'CURRENT_TIMESTAMP';
43
44
    /**
45
     * @var string
46
     */
47
    protected $userAction;
48
49
    /**
50
     * @var integer
51
     */
52
    protected $userActionId;
53
54
    /**
55
     * @var string
56
     */
57
    protected $locationVector;
58
59
    /**
60
     * @var integer
61
     */
62
    protected $mailCount = 0;
63
64
    /**
65
     * @var integer
66
     */
67
    protected $userMailId;
68
69
    /**
70
     * @var integer
71
     */
72
    protected $k;
73
74
    /**
75
     * @var integer
76
     */
77
    protected $kWallet = 0;
78
79
    /**
80
     * @var integer
81
     */
82
    protected $listingAmount = 32;
83
84
    /**
85
     * @var string
86
     */
87
    protected $listingOrder = 'desc';
88
89
    /**
90
     * @var integer
91
     */
92
    protected $headerId;
93
94
    /**
95
     * @var string
96
     */
97
    protected $hash;
98
99
    /**
100
     * @var DateTime
101
     */
102
    protected $accLockout;
103
104
    /**
105
     * @var integer
106
     */
107
    protected $passwordChangePeriod;
108
109
    /**
110
     * @var integer
111
     */
112
    protected $loginRetry = 0;
113
114
    /**
115
     * @var DateTime
116
     */
117
    protected $dateLastLogin = '0000-00-00 00:00:00';
118
119
    /**
120
     * @var DateTime
121
     */
122
    protected $datePasswordChanged;
123
124
    /**
125
     * @var DateTime
126
     */
127
    protected $dateLoginFailed;
128
129
    /**
130
     * @var integer
131
     */
132
    protected $mailNotify = 0;
133
134
    /**
135
     * @var integer
136
     */
137
    protected $bookstyle = 0;
138
139
    /**
140
     * @var string
141
     */
142
    protected $settingMetadata;
143
144
    //endregion
145
146
    //region Association properties
147
148
    /** @var Node */
149
    protected $node;
150
151
    /** @var Mail[] */
152
    protected $mails;
153
154
    //endregion
155
156
    //region Column methods
157
158
    public function getId()
159
    {
160
        return $this->id;
161
    }
162
163
    public function getLogin()
164
    {
165
        return $this->login;
166
    }
167
168
    public function setLogin($login)
169
    {
170
        $this->login = $login;
171
172
        return $this;
173
    }
174
175
    /**
176
     * Returns the password used to authenticate the user.
177
     *
178
     * @return string
179
     */
180
    public function getPassword()
181
    {
182
        return $this->password;
183
    }
184
185
    public function setPassword($password)
186
    {
187
        $this->password = $password;
188
189
        return $this;
190
    }
191
192
    public function getEmail()
193
    {
194
        return $this->email;
195
    }
196
197
    public function setEmail($email)
198
    {
199
        $this->email = $email;
200
201
        return $this;
202
    }
203
204
    public function getLastAction()
205
    {
206
        return $this->lastAction;
207
    }
208
209
    public function setLastAction($lastAction)
210
    {
211
        $this->lastAction = $lastAction;
212
213
        return $this;
214
    }
215
216
    public function getUserAction()
217
    {
218
        return $this->userAction;
219
    }
220
221
    public function setUserAction($userAction)
222
    {
223
        $this->userAction = $userAction;
224
225
        return $this;
226
    }
227
228
    public function getUserActionId()
229
    {
230
        return $this->userActionId;
231
    }
232
233
    public function setUserActionId($userActionId)
234
    {
235
        $this->userActionId = $userActionId;
236
237
        return $this;
238
    }
239
240
    public function getLocationVector()
241
    {
242
        return $this->locationVector;
243
    }
244
245
    public function setUserLocationVector($locationVector)
246
    {
247
        $this->locationVector = $locationVector;
248
249
        return $this;
250
    }
251
252
    public function getMailCount()
253
    {
254
        return $this->mailCount;
255
    }
256
257
    public function setMailCount($mailCount)
258
    {
259
        $this->mailCount = $mailCount;
260
261
        return $this;
262
    }
263
264
    public function getUserMailId()
265
    {
266
        return $this->userMailId;
267
    }
268
269
    public function setUserMailId($userMailId)
270
    {
271
        $this->userMailId = $userMailId;
272
273
        return $this;
274
    }
275
276
    public function getK()
277
    {
278
        return $this->k;
279
    }
280
281
    public function setK($k)
282
    {
283
        $this->k = $k;
284
285
        return $this;
286
    }
287
288
    public function getKWallet()
289
    {
290
        return $this->kWallet;
291
    }
292
293
    public function setKWallet($kWallet)
294
    {
295
        $this->kWallet = $kWallet;
296
297
        return $this;
298
    }
299
300
    public function getListingAmount()
301
    {
302
        return $this->listingAmount;
303
    }
304
305
    public function setListingAmount($listingAmount)
306
    {
307
        $this->listingAmount = $listingAmount;
308
309
        return $this;
310
    }
311
312
    public function getListingOrder()
313
    {
314
        return $this->listingOrder;
315
    }
316
317
    public function setListingOrder($listingOrder)
318
    {
319
        $this->listingOrder = $listingOrder;
320
321
        return $this;
322
    }
323
324
    public function getHeaderId()
325
    {
326
        return $this->headerId;
327
    }
328
329
    public function setHeaderId($headerId)
330
    {
331
        $this->headerId = $headerId;
332
333
        return $this;
334
    }
335
336
    public function getHash()
337
    {
338
        return $this->hash;
339
    }
340
341
    public function setHash($hash)
342
    {
343
        $this->hash = $hash;
344
345
        return $this;
346
    }
347
348
    public function getAccLockout()
349
    {
350
        return $this->accLockout;
351
    }
352
353
    public function setAccLockout($accLockout)
354
    {
355
        $this->accLockout = $accLockout;
356
357
        return $this;
358
    }
359
360
    public function getPasswordChangePeriod()
361
    {
362
        return $this->passwordChangePeriod;
363
    }
364
365
    public function setPasswordChangePeriod($passwordChangePeriod)
366
    {
367
        $this->passwordChangePeriod = $passwordChangePeriod;
368
369
        return $this;
370
    }
371
372
    public function getLoginRetry()
373
    {
374
        return $this->loginRetry;
375
    }
376
377
    public function setLoginRetry($loginRetry)
378
    {
379
        $this->loginRetry = $loginRetry;
380
381
        return $this;
382
    }
383
384
    public function getDateLastLogin()
385
    {
386
        return $this->dateLastLogin;
387
    }
388
389
    public function setDateLastLogin(DateTime $dateLastLogin)
390
    {
391
        $this->dateLastLogin = $dateLastLogin;
392
393
        return $this;
394
    }
395
396
    public function getDatePasswordChanged()
397
    {
398
        return $this->datePasswordChanged;
399
    }
400
401
    public function setDatePasswordChanged($datePasswordChanged)
402
    {
403
        $this->datePasswordChanged = $datePasswordChanged;
404
405
        return $this;
406
    }
407
408
    public function getDateLoginFailed()
409
    {
410
        return $this->dateLoginFailed;
411
    }
412
413
    public function setDateLoginFailed($dateLoginFailed)
414
    {
415
        $this->dateLoginFailed = $dateLoginFailed;
416
417
        return $this;
418
    }
419
420
    public function getMailNotify()
421
    {
422
        return $this->mailNotify;
423
    }
424
425
    public function setMailNotify($mailNotify)
426
    {
427
        $this->mailNotify = $mailNotify;
428
429
        return $this;
430
    }
431
432
    //endregion
433
434
    //region Association methods
435
436
    public function getNode()
437
    {
438
        return $this->node;
439
    }
440
441
    public function getMails()
442
    {
443
        return $this->mails;
444
    }
445
446
    public function setMails($mails)
447
    {
448
        $this->mails = $mails;
449
450
        return $this;
451
    }
452
453
    //endregion
454
455
    //region UserInterface
456
457
    /**
458
     * Returns the username used to authenticate the user.
459
     *
460
     * @return string The username
461
     */
462
    public function getUsername()
463
    {
464
        return $this->login;
465
    }
466
467
    /**
468
     * Returns the roles granted to the user.
469
     *
470
     * Alternatively, the roles might be stored on a ``roles`` property,
471
     * and populated in any number of different ways when the user object
472
     * is created.
473
     *
474
     * @return (Role|string)[] The user roles
475
     */
476
    public function getRoles()
477
    {
478
        return [
479
            'ROLE_USER',
480
        ];
481
    }
482
483
    /**
484
     * Returns the salt that was originally used to encode the password.
485
     *
486
     * This can return null if the password was not encoded using a salt.
487
     *
488
     * @return string|null The salt
489
     */
490
    public function getSalt()
491
    {
492
        return null;
493
    }
494
495
    /**
496
     * Removes sensitive data from the user.
497
     *
498
     * This is important if, at any given point, sensitive information like
499
     * the plain-text password is stored on this object.
500
     */
501
    public function eraseCredentials()
502
    {
503
        return;
504
    }
505
506
    //endregion
507
508
    //region AdvancedUserInterface
509
510
    /**
511
     * Checks whether the user's account has expired.
512
     *
513
     * Internally, if this method returns false, the authentication system
514
     * will throw an AccountExpiredException and prevent login.
515
     *
516
     * @return bool true if the user's account is non expired, false otherwise
517
     *
518
     * @see AccountExpiredException
519
     */
520
    public function isAccountNonExpired()
521
    {
522
        return true;
523
    }
524
525
    /**
526
     * Checks whether the user is locked.
527
     *
528
     * Internally, if this method returns false, the authentication system
529
     * will throw a LockedException and prevent login.
530
     *
531
     * @return bool true if the user is not locked, false otherwise
532
     *
533
     * @see LockedException
534
     */
535
    public function isAccountNonLocked()
536
    {
537
        $now = new DateTime();
538
539
        return (empty($this->accLockout) || $this->accLockout <= $now);
540
    }
541
542
    /**
543
     * Checks whether the user's credentials (password) has expired.
544
     *
545
     * Internally, if this method returns false, the authentication system
546
     * will throw a CredentialsExpiredException and prevent login.
547
     *
548
     * @return bool true if the user's credentials are non expired, false otherwise
549
     *
550
     * @see CredentialsExpiredException
551
     */
552
    public function isCredentialsNonExpired()
553
    {
554
        return true;
555
    }
556
557
    /**
558
     * Checks whether the user is enabled.
559
     *
560
     * Internally, if this method returns false, the authentication system
561
     * will throw a DisabledException and prevent login.
562
     *
563
     * @return bool true if the user is enabled, false otherwise
564
     *
565
     * @see DisabledException
566
     */
567
    public function isEnabled()
568
    {
569
        //TODO this should return false for users with pending registrations
570
        return true;
571
    }
572
573
    //endregion
574
575
    //region EncoderAwareInterface
576
577
    /**
578
     * Gets the name of the encoder used to encode the password.
579
     *
580
     * If the method returns null, the standard way to retrieve the encoder
581
     * will be used instead.
582
     *
583
     * @return string
584
     */
585
    public function getEncoderName()
586
    {
587
        if (strlen($this->password) === 32) {
588
            return self::ENCODER_MD5;
589
        } elseif (strlen($this->password) === 80 && $this->password[0] !== '$') {
590
            return self::ENCODER_SHA;
591
        } else {
592
            return self::ENCODER_BCRYPT;
593
        }
594
    }
595
596
    //endregion
597
598
    //region Serializable
599
600
    /**
601
     * @see \Serializable::serialize()
602
     */
603
    public function serialize()
604
    {
605
        return serialize([
606
            $this->id,
607
            $this->login,
608
            $this->password,
609
            $this->accLockout,
610
        ]);
611
    }
612
613
    /**
614
     * @param string $serialized
615
     * @see \Serializable::unserialize()
616
     */
617
    public function unserialize($serialized)
618
    {
619
        list(
620
            $this->id,
621
            $this->login,
622
            $this->password,
623
            $this->accLockout
624
        ) = unserialize($serialized);
625
    }
626
627
    //endregion
628
}
629