GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UserEntity   F
last analyzed

Complexity

Total Complexity 75

Size/Duplication

Total Lines 908
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 5

Importance

Changes 0
Metric Value
wmc 75
lcom 7
cbo 5
dl 0
loc 908
rs 1.8461
c 0
b 0
f 0

66 Methods

Rating   Name   Duplication   Size   Complexity  
A setId() 0 6 1
A getLocale() 0 4 1
A setLocale() 0 6 1
A getUsername() 0 4 1
A setUsername() 0 6 1
A getEmail() 0 4 1
A setEmail() 0 6 1
A getPassword() 0 4 1
A setPassword() 0 8 2
A getPlainPassword() 0 4 1
A setPlainPassword() 0 17 2
A getOldPassword() 0 4 1
A setOldPassword() 0 6 1
A getSalt() 0 4 1
A setSalt() 0 6 1
A getEnabled() 0 4 1
A isEnabled() 0 4 1
A setEnabled() 0 6 1
A setLocked() 0 6 1
A lock() 0 6 1
A isAccountNonLocked() 0 4 1
A setResetPasswordCode() 0 6 1
A setActivationCode() 0 6 1
A getTimeLastActive() 0 4 1
A setTimeLastActive() 0 6 1
A getTimeCreated() 0 4 1
A setTimeCreated() 0 6 1
A getTimeUpdated() 0 4 1
A setTimeUpdated() 0 6 1
A getExpired() 0 4 1
A isExpired() 0 4 1
A isAccountNonExpired() 0 4 1
A getCredentialsExpired() 0 4 1
A isCredentialsExpired() 0 4 1
A serialize() 0 10 1
A preUpdate() 0 4 1
A prePersist() 0 5 1
A __construct() 0 13 1
A getId() 0 4 1
A getNewEmail() 0 4 1
A setNewEmail() 0 6 1
A enable() 0 6 1
A disable() 0 6 1
A getLocked() 0 4 1
A isLocked() 0 4 1
A getResetPasswordCode() 0 4 1
A getActivationCode() 0 4 1
A getNewEmailCode() 0 4 1
A setNewEmailCode() 0 6 1
A getTimeResetPasswordCodeExpires() 0 4 1
A setTimeResetPasswordCodeExpires() 0 6 1
A isCredentialsNonExpired() 0 4 1
A getRoles() 0 10 2
A setRoles() 0 6 1
A hasRole() 0 7 1
A getProfile() 0 4 1
A setProfile() 0 8 1
A getPosts() 0 4 1
A setPosts() 0 6 1
A getUserActions() 0 4 1
A setUserActions() 0 6 1
B isEqualTo() 0 24 6
A eraseCredentials() 0 4 1
A unserialize() 0 9 1
A __toString() 0 4 1
B toArray() 0 24 2

How to fix   Complexity   

Complex Class

Complex classes like UserEntity 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 UserEntity, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Application\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
8
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
9
10
/**
11
 * User Entity.
12
 *
13
 * @ORM\Table(name="users")
14
 * @ORM\Entity(repositoryClass="Application\Repository\UserRepository")
15
 * @ORM\HasLifecycleCallbacks()
16
 *
17
 * @author Borut Balažek <[email protected]>
18
 */
19
class UserEntity implements AdvancedUserInterface, \Serializable
20
{
21
    /**
22
     * @var int
23
     *
24
     * @ORM\Column(name="id", type="integer")
25
     * @ORM\Id
26
     * @ORM\GeneratedValue(strategy="IDENTITY")
27
     */
28
    protected $id;
29
30
    /**
31
     * What is the locale for this user?
32
     *
33
     * @var string
34
     *
35
     * @ORM\Column(name="locale", type="string", length=8, nullable=true)
36
     */
37
    protected $locale = 'en_US';
38
39
    /**
40
     * @var string
41
     *
42
     * @ORM\Column(name="username", type="string", length=64, unique=true)
43
     */
44
    protected $username;
45
46
    /**
47
     * @var string
48
     *
49
     * @ORM\Column(name="email", type="string", length=64, unique=true)
50
     */
51
    protected $email;
52
53
    /**
54
     * We must confirm the new password, so temporary save it inside this field.
55
     *
56
     * @var string
57
     *
58
     * @ORM\Column(name="new_email", type="string", length=64, nullable=true)
59
     */
60
    protected $newEmail;
61
62
    /**
63
     * @var array
64
     *
65
     * @ORM\Column(name="roles", type="json_array", nullable=true)
66
     */
67
    protected $roles;
68
69
    /**
70
     * @var string
71
     *
72
     * @ORM\Column(name="password", type="string", length=255)
73
     */
74
    protected $password;
75
76
    /**
77
     * Used only when saving the user.
78
     *
79
     * @var string
80
     */
81
    protected $plainPassword;
82
83
    /**
84
     * Used only when saving a new password.
85
     *
86
     * @var string
87
     */
88
    protected $oldPassword;
89
90
    /**
91
     * @var string
92
     *
93
     * @ORM\Column(name="salt", type="string", length=255, nullable=true)
94
     */
95
    protected $salt;
96
97
    /**
98
     * @var bool
99
     *
100
     * @ORM\Column(name="enabled", type="boolean")
101
     */
102
    protected $enabled = true;
103
104
    /**
105
     * @var bool
106
     *
107
     * @ORM\Column(name="locked", type="boolean")
108
     */
109
    protected $locked = false;
110
111
    /**
112
     * @var string
113
     *
114
     * @ORM\Column(name="reset_password_code", type="string", length=255, nullable=true, unique=true)
115
     */
116
    protected $resetPasswordCode;
117
118
    /**
119
     * @var string
120
     *
121
     * @ORM\Column(name="activation_code", type="string", length=255, nullable=true, unique=true)
122
     */
123
    protected $activationCode;
124
125
    /**
126
     * @var string
127
     *
128
     * @ORM\Column(name="new_email_code", type="string", length=255, nullable=true, unique=true)
129
     */
130
    protected $newEmailCode;
131
132
    /**
133
     * @var \DateTime
134
     *
135
     * @ORM\Column(name="time_last_active", type="datetime", nullable=true)
136
     */
137
    protected $timeLastActive;
138
139
    /**
140
     * @var \DateTime
141
     *
142
     * @ORM\Column(name="time_reset_password_code_expires", type="datetime", nullable=true)
143
     */
144
    protected $timeResetPasswordCodeExpires;
145
146
    /**
147
     * @var \DateTime
148
     *
149
     * @ORM\Column(name="time_created", type="datetime")
150
     */
151
    protected $timeCreated;
152
153
    /**
154
     * @var \DateTime
155
     *
156
     * @ORM\Column(name="time_updated", type="datetime")
157
     */
158
    protected $timeUpdated;
159
160
    /**
161
     * @ORM\OneToOne(targetEntity="Application\Entity\ProfileEntity", mappedBy="user", cascade={"all"})
162
     **/
163
    protected $profile;
164
165
    /**
166
     * @var ArrayCollection
167
     *
168
     * @ORM\OneToMany(targetEntity="Application\Entity\PostEntity", mappedBy="user", cascade={"all"})
169
     */
170
    protected $posts;
171
172
    /**
173
     * @var ArrayCollection
174
     *
175
     * @ORM\OneToMany(targetEntity="Application\Entity\UserActionEntity", mappedBy="user", cascade={"all"})
176
     */
177
    protected $userActions;
178
179
    /**
180
     * Otherwise known as: userExpired / accountExpired.
181
     *
182
     * @var bool
183
     */
184
    protected $expired = false;
185
186
    /**
187
     * @var bool
188
     */
189
    protected $credentialsExpired = false;
190
191
    /**
192
     * The constructor.
193
     */
194
    public function __construct()
195
    {
196
        $this->setSalt(
197
            md5(uniqid(null, true))
198
        );
199
200
        $this->setActivationCode(
201
            md5(uniqid(null, true))
202
        );
203
204
        $this->posts = new ArrayCollection();
205
        $this->userActions = new ArrayCollection();
206
    }
207
208
    /*** Id ***/
209
    /**
210
     * @return int
211
     */
212
    public function getId()
213
    {
214
        return $this->id;
215
    }
216
217
    /**
218
     * @param int $id
219
     *
220
     * @return UserEntity
221
     */
222
    public function setId($id)
223
    {
224
        $this->id = $id;
225
226
        return $this;
227
    }
228
229
    /*** Locale ***/
230
    /**
231
     * @return string
232
     */
233
    public function getLocale()
234
    {
235
        return $this->locale;
236
    }
237
238
    /**
239
     * @param string $locale
240
     *
241
     * @return UserEntity
242
     */
243
    public function setLocale($locale)
244
    {
245
        $this->locale = $locale;
246
247
        return $this;
248
    }
249
250
    /*** Username ***/
251
    /**
252
     * @return string
253
     */
254
    public function getUsername()
255
    {
256
        return $this->username;
257
    }
258
259
    /**
260
     * @param string $username
261
     *
262
     * @return UserEntity
263
     */
264
    public function setUsername($username)
265
    {
266
        $this->username = $username;
267
268
        return $this;
269
    }
270
271
    /*** Email ***/
272
    /**
273
     * @return string
274
     */
275
    public function getEmail()
276
    {
277
        return $this->email;
278
    }
279
280
    /**
281
     * @param string $email
282
     *
283
     * @return UserEntity
284
     */
285
    public function setEmail($email)
286
    {
287
        $this->email = $email;
288
289
        return $this;
290
    }
291
292
    /*** New email ***/
293
    /**
294
     * @return string
295
     */
296
    public function getNewEmail()
297
    {
298
        return $this->newEmail;
299
    }
300
301
    /**
302
     * @param string $newEmail
303
     *
304
     * @return UserEntity
305
     */
306
    public function setNewEmail($newEmail)
307
    {
308
        $this->newEmail = $newEmail;
309
310
        return $this;
311
    }
312
313
    /*** Password ***/
314
    /**
315
     * @return string
316
     */
317
    public function getPassword()
318
    {
319
        return $this->password;
320
    }
321
322
    /**
323
     * @param string $password
324
     *
325
     * @return UserEntity
326
     */
327
    public function setPassword($password)
328
    {
329
        if ($password) {
330
            $this->password = $password;
331
        }
332
333
        return $this;
334
    }
335
336
    /*** Plain password ***/
337
    /**
338
     * @return string
339
     */
340
    public function getPlainPassword()
341
    {
342
        return $this->plainPassword;
343
    }
344
345
    /**
346
     * @param string         $plainPassword
347
     * @param EncoderFactory $encoderFactory
348
     *
349
     * @return UserEntity
350
     */
351
    public function setPlainPassword($plainPassword, EncoderFactory $encoderFactory = null)
352
    {
353
        $this->plainPassword = $plainPassword;
354
355
        if ($encoderFactory) {
356
            $encoder = $encoderFactory->getEncoder($this);
357
358
            $password = $encoder->encodePassword(
359
                $this->getPlainPassword(),
360
                $this->getSalt()
361
            );
362
363
            $this->setPassword($password);
364
        }
365
366
        return $this;
367
    }
368
369
    /*** Old password ***/
370
    /**
371
     * @return string
372
     */
373
    public function getOldPassword()
374
    {
375
        return $this->oldPassword;
376
    }
377
378
    /**
379
     * @param string $oldPassword
380
     *
381
     * @return UserEntity
382
     */
383
    public function setOldPassword($oldPassword)
384
    {
385
        $this->oldPassword = $oldPassword;
386
387
        return $this;
388
    }
389
390
    /*** Salt ***/
391
    /**
392
     * @return string
393
     */
394
    public function getSalt()
395
    {
396
        return $this->salt;
397
    }
398
399
    /**
400
     * @param string $salt
401
     *
402
     * @return UserEntity
403
     */
404
    public function setSalt($salt)
405
    {
406
        $this->salt = $salt;
407
408
        return $this;
409
    }
410
411
    /*** Enabled ***/
412
    /**
413
     * @return bool
414
     */
415
    public function getEnabled()
416
    {
417
        return $this->enabled;
418
    }
419
420
    /**
421
     * @return bool
422
     */
423
    public function isEnabled()
424
    {
425
        return $this->getEnabled();
426
    }
427
428
    /**
429
     * @param bool $enabled
430
     */
431
    public function setEnabled($enabled)
432
    {
433
        $this->enabled = $enabled;
434
435
        return $this;
436
    }
437
438
    /**
439
     * @return UserEntity
440
     */
441
    public function enable()
442
    {
443
        $this->setEnabled(true);
444
445
        return $this;
446
    }
447
448
    /**
449
     * @return UserEntity
450
     */
451
    public function disable()
452
    {
453
        $this->setEnabled(false);
454
455
        return $this;
456
    }
457
458
    /*** Locked ***/
459
    /**
460
     * @return bool
461
     */
462
    public function getLocked()
463
    {
464
        return $this->locked;
465
    }
466
467
    /**
468
     * @return bool
469
     */
470
    public function isLocked()
471
    {
472
        return $this->getLocked();
473
    }
474
475
    /**
476
     * @param bool $locked
477
     *
478
     * @return UserEntity
479
     */
480
    public function setLocked($locked)
481
    {
482
        $this->locked = $locked;
483
484
        return $this;
485
    }
486
487
    /**
488
     * @return UserEntity
489
     */
490
    public function lock()
491
    {
492
        $this->setLocked(true);
493
494
        return $this;
495
    }
496
497
    /**
498
     * @return bool
499
     */
500
    public function isAccountNonLocked()
501
    {
502
        return !$this->isLocked();
503
    }
504
505
    /*** Reset password code ***/
506
    /**
507
     * @return string
508
     */
509
    public function getResetPasswordCode()
510
    {
511
        return $this->resetPasswordCode;
512
    }
513
514
    /**
515
     * @param string $resetPasswordCode
516
     *
517
     * @return UserEntity
518
     */
519
    public function setResetPasswordCode($resetPasswordCode)
520
    {
521
        $this->resetPasswordCode = $resetPasswordCode;
522
523
        return $this;
524
    }
525
526
    /*** Activation code ***/
527
    /**
528
     * @return string
529
     */
530
    public function getActivationCode()
531
    {
532
        return $this->activationCode;
533
    }
534
535
    /**
536
     * @param string $activationCode
537
     *
538
     * @return UserEntity
539
     */
540
    public function setActivationCode($activationCode)
541
    {
542
        $this->activationCode = $activationCode;
543
544
        return $this;
545
    }
546
547
    /*** New email code ***/
548
    /**
549
     * @return string
550
     */
551
    public function getNewEmailCode()
552
    {
553
        return $this->newEmailCode;
554
    }
555
556
    /**
557
     * @param string $newEmailCode
558
     *
559
     * @return UserEntity
560
     */
561
    public function setNewEmailCode($newEmailCode)
562
    {
563
        $this->newEmailCode = $newEmailCode;
564
565
        return $this;
566
    }
567
568
    /*** Time last active ***/
569
    /**
570
     * @return \DateTime
571
     */
572
    public function getTimeLastActive()
573
    {
574
        return $this->timeLastActive;
575
    }
576
577
    /**
578
     * @param $timeLastActive
579
     *
580
     * @return UserEntity
581
     */
582
    public function setTimeLastActive(\Datetime $timeLastActive = null)
583
    {
584
        $this->timeLastActive = $timeLastActive;
585
586
        return $this;
587
    }
588
589
    /*** Time reset password code expires ***/
590
    /**
591
     * @return \DateTime
592
     */
593
    public function getTimeResetPasswordCodeExpires()
594
    {
595
        return $this->timeResetPasswordCodeExpires;
596
    }
597
598
    /**
599
     * @param $timeResetPasswordCodeExpires
600
     *
601
     * @return UserEntity
602
     */
603
    public function setTimeResetPasswordCodeExpires(\Datetime $timeResetPasswordCodeExpires = null)
604
    {
605
        $this->timeResetPasswordCodeExpires = $timeResetPasswordCodeExpires;
606
607
        return $this;
608
    }
609
610
    /*** Time created ***/
611
    /**
612
     * @return \DateTime
613
     */
614
    public function getTimeCreated()
615
    {
616
        return $this->timeCreated;
617
    }
618
619
    /**
620
     * @param \DateTime $timeCreated
621
     *
622
     * @return UserEntity
623
     */
624
    public function setTimeCreated(\Datetime $timeCreated)
625
    {
626
        $this->timeCreated = $timeCreated;
627
628
        return $this;
629
    }
630
631
    /*** Time updated ***/
632
    /**
633
     * @return \DateTime
634
     */
635
    public function getTimeUpdated()
636
    {
637
        return $this->timeUpdated;
638
    }
639
640
    /**
641
     * @param \DateTime $timeUpdated
642
     *
643
     * @return UserEntity
644
     */
645
    public function setTimeUpdated(\DateTime $timeUpdated)
646
    {
647
        $this->timeUpdated = $timeUpdated;
648
649
        return $this;
650
    }
651
652
    /*** Expired ***/
653
    /**
654
     * @return bool
655
     */
656
    public function getExpired()
657
    {
658
        return $this->expired;
659
    }
660
661
    /**
662
     * @return bool
663
     */
664
    public function isExpired()
665
    {
666
        return $this->getExpired();
667
    }
668
669
    /**
670
     * @return bool
671
     */
672
    public function isAccountNonExpired()
673
    {
674
        return !$this->getExpired();
675
    }
676
677
    /*** Credentials expired ***/
678
    /**
679
     * @return bool
680
     */
681
    public function getCredentialsExpired()
682
    {
683
        return $this->credentialsExpired;
684
    }
685
686
    /**
687
     * @return bool
688
     */
689
    public function isCredentialsExpired()
690
    {
691
        return $this->getCredentialsExpired();
692
    }
693
694
    /**
695
     * @return bool
696
     */
697
    public function isCredentialsNonExpired()
698
    {
699
        return !$this->getExpired();
700
    }
701
702
    /*** Roles ***/
703
    /**
704
     * @return array
705
     */
706
    public function getRoles()
707
    {
708
        $roles = is_array($this->roles)
709
            ? $this->roles
710
            : array()
711
        ;
712
        $roles[] = 'ROLE_USER';
713
714
        return (array) array_unique($roles, SORT_REGULAR);
715
    }
716
717
    /**
718
     * @param array $roles
719
     *
720
     * @return UserEntity
721
     */
722
    public function setRoles(array $roles)
723
    {
724
        $this->roles = $roles;
725
726
        return $this;
727
    }
728
729
    /**
730
     * @param string $role
731
     *
732
     * @return bool
733
     */
734
    public function hasRole($role)
735
    {
736
        return in_array(
737
            $role,
738
            $this->getRoles()
739
        );
740
    }
741
742
    /*** Profile ***/
743
    /**
744
     * @return ProfileEntity
745
     */
746
    public function getProfile()
747
    {
748
        return $this->profile;
749
    }
750
751
    /**
752
     * @param ProfileEntity $profile
753
     *
754
     * @return UserEntity
755
     */
756
    public function setProfile(ProfileEntity $profile)
757
    {
758
        $this->profile = $profile;
759
760
        $this->getProfile()->setUser($this);
761
762
        return $this;
763
    }
764
765
    /*** Posts ***/
766
    /**
767
     * @return array
768
     */
769
    public function getPosts()
770
    {
771
        return $this->posts->toArray();
772
    }
773
774
    /**
775
     * @param ArrayCollection $posts
776
     *
777
     * @return UserEntity
778
     */
779
    public function setPosts($posts)
780
    {
781
        $this->posts = $posts;
782
783
        return $this;
784
    }
785
786
    /*** User actions ***/
787
    /**
788
     * @return array
789
     */
790
    public function getUserActions()
791
    {
792
        return $this->userActions->toArray();
793
    }
794
795
    /**
796
     * @param $userActions
797
     *
798
     * @return UserEntity
799
     */
800
    public function setUserActions($userActions)
801
    {
802
        $this->userActions = $userActions;
803
804
        return $this;
805
    }
806
807
    /**
808
     * @param AdvancedUserInterface $user
809
     *
810
     * @return bool
811
     */
812
    public function isEqualTo(AdvancedUserInterface $user)
813
    {
814
        if (!($user instanceof AdvancedUserInterface)) {
815
            return false;
816
        }
817
818
        if ($this->getPassword() !== $user->getPassword()) {
819
            return false;
820
        }
821
822
        if ($this->getSalt() !== $user->getSalt()) {
823
            return false;
824
        }
825
826
        if ($this->getUsername() !== $user->getUsername()) {
827
            return false;
828
        }
829
830
        if (serialize($this->getRoles()) !== serialize($user->getRoles())) {
831
            return false;
832
        }
833
834
        return true;
835
    }
836
837
    /**
838
     * {@inheritdoc}
839
     */
840
    public function eraseCredentials()
841
    {
842
        $this->setPlainPassword(null);
843
    }
844
845
    /**
846
     * @return string
847
     */
848
    public function serialize()
849
    {
850
        return serialize(array(
851
            $this->id,
852
            $this->username,
853
            $this->email,
854
            $this->password,
855
            $this->salt,
856
        ));
857
    }
858
859
    /**
860
     * {@inheritdoc}
861
     */
862
    public function unserialize($serialized)
863
    {
864
        list(
865
            $this->id,
866
            $this->username,
867
            $this->email,
868
            $this->password,
869
            $this->salt) = unserialize($serialized);
870
    }
871
872
    /**
873
     * @return string
874
     */
875
    public function __toString()
876
    {
877
        return $this->getUsername();
878
    }
879
880
    /**
881
     * @param $allData Show all the data for this user.
882
     *
883
     * @return array
884
     */
885
    public function toArray($allData = true)
886
    {
887
        $data = array(
888
            'username' => $this->getUsername(),
889
            'email' => $this->getEmail(),
890
            'title' => $this->getProfile()->getTitle(),
891
            'first_name' => $this->getProfile()->getFirstName(),
892
            'middle_name' => $this->getProfile()->getMiddleName(),
893
            'last_name' => $this->getProfile()->getLastName(),
894
            'full_name' => $this->getProfile()->getFullName(),
895
            'gender' => $this->getProfile()->getGender(),
896
            'birthdate' => $this->getProfile()->getBirthdate()->format('Y-m-d'),
897
            'image_url' => $this->getProfile()->getImageUrl(),
898
        );
899
900
        if ($allData) {
901
            $data = array_merge($data, array(
902
                'id' => $this->getId(),
903
                'time_created' => $this->getTimeCreated()->format(DATE_ATOM),
904
            ));
905
        }
906
907
        return $data;
908
    }
909
910
    /**
911
     * @ORM\PreUpdate
912
     */
913
    public function preUpdate()
914
    {
915
        $this->setTimeUpdated(new \DateTime('now'));
916
    }
917
918
    /**
919
     * @ORM\PrePersist
920
     */
921
    public function prePersist()
922
    {
923
        $this->setTimeUpdated(new \DateTime('now'));
924
        $this->setTimeCreated(new \DateTime('now'));
925
    }
926
}
927