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 ( 8ebd89...b16691 )
by Borut
14:41
created

UserEntity::setToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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