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.
Completed
Push — develop ( e8545c...16e366 )
by Borut
03:19
created

UserEntity::setNewEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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