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 — user-entity ( ad4926...10e20c )
by Luis Ramón
03:29
created

User::getRoles()   D

Complexity

Conditions 10
Paths 64

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 30
rs 4.8196
cc 10
eloc 15
nc 64
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2016: 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\ArrayCollection;
24
use Doctrine\Common\Collections\Collection;
25
use Doctrine\ORM\Mapping as ORM;
26
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
27
use Symfony\Component\Security\Core\Role\Role;
28
use Symfony\Component\Security\Core\User\EquatableInterface;
29
use Symfony\Component\Security\Core\User\UserInterface;
30
use Symfony\Component\Validator\Constraints as Assert;
31
use Symfony\Component\Validator\Context\ExecutionContextInterface;
32
33
/**
34
 * @ORM\Entity(repositoryClass="UserRepository")
35
 * @UniqueEntity("loginUsername")
36
 */
37
class User extends Person implements UserInterface, \Serializable, EquatableInterface
38
{
39
    /**
40
     * @ORM\Id
41
     * @ORM\Column(type="integer")
42
     * @ORM\GeneratedValue
43
     * @var int
44
     */
45
    protected $id;
46
47
    /**
48
     * @ORM\Column(type="string", nullable=true)
49
     * @Assert\Regex(pattern="/[@ ]{1,}/", match=false, message="login_username.invalid_chars", htmlPattern=false)
50
     * @Assert\Length(min=5)
51
     * @var string
52
     */
53
    protected $loginUsername;
54
55
    /**
56
     * @ORM\Column(type="string", nullable=true)
57
     * @Assert\Length(min=7)
58
     * @var string
59
     */
60
    protected $password;
61
62
    /**
63
     * @ORM\Column(type="string", nullable=true)
64
     * @var string
65
     */
66
    protected $token;
67
68
    /**
69
     * @ORM\Column(type="datetime", nullable=true)
70
     * @var \DateTime
71
     */
72
    protected $tokenValidity;
73
74
    /**
75
     * @ORM\Column(type="boolean")
76
     * @var bool
77
     */
78
    protected $enabled;
79
80
    /**
81
     * @ORM\Column(type="boolean")
82
     * @var bool
83
     */
84
    protected $globalAdministrator;
85
86
    /**
87
     * @ORM\Column(type="boolean")
88
     * @var bool
89
     */
90
    protected $financialManager;
91
92
    /**
93
     * @ORM\Column(type="datetime", nullable=true)
94
     * @var \DateTime
95
     */
96
    protected $lastLogin;
97
98
    /**
99
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="students")
100
     *
101
     * @var Group
102
     */
103
    protected $studentGroup;
104
105
    /**
106
     * @ORM\ManyToMany(targetEntity="Group", mappedBy="tutors")
107
     * @ORM\JoinTable(name="tutorized_groups")
108
     *
109
     * @var Collection
110
     */
111
    protected $tutorizedGroups;
112
113
    /**
114
     * @ORM\OneToMany(targetEntity="Department", mappedBy="head")
115
     *
116
     * @var Collection
117
     */
118
    protected $directs;
119
120
    /**
121
     * @ORM\OneToMany(targetEntity="Agreement", mappedBy="student")
122
     * @ORM\OrderBy({"fromDate": "ASC"})
123
     *
124
     * @var Collection
125
     */
126
    protected $studentAgreements;
127
128
    /**
129
     * @ORM\OneToMany(targetEntity="Agreement", mappedBy="educationalTutor")
130
     * @ORM\OrderBy({"fromDate": "ASC"})
131
     *
132
     * @var Collection
133
     */
134
    protected $educationalTutorAgreements;
135
136
    /**
137
     * @ORM\OneToMany(targetEntity="Agreement", mappedBy="workTutor")
138
     * @ORM\OrderBy({"fromDate": "ASC"})
139
     *
140
     * @var Collection
141
     */
142
    protected $workTutorAgreements;
143
144
    /**
145
     * @ORM\OneToMany(targetEntity="Expense", mappedBy="teacher")
146
     * @ORM\OrderBy({"date": "ASC"})
147
     *
148
     * @var Collection
149
     */
150
    protected $expenses;
151
152
    /**
153
     * @ORM\OneToMany(targetEntity="Visit", mappedBy="tutor")
154
     * @ORM\OrderBy({"date": "ASC"})
155
     * @var Collection
156
     */
157
    protected $visits;
158
159
    /**
160
     * Constructor
161
     */
162
    public function __construct()
163
    {
164
        parent::__construct();
165
166
        $this->tutorizedGroups = new ArrayCollection();
167
        $this->directs = new ArrayCollection();
168
        $this->studentAgreements = new ArrayCollection();
169
        $this->educationalTutorAgreements = new ArrayCollection();
170
        $this->workTutorAgreements = new ArrayCollection();
171
        $this->expenses = new ArrayCollection();
172
        $this->visits = new ArrayCollection();
173
    }
174
175
    /**
176
     * Returns the person's display name
177
     *
178
     * @return string
179
     */
180
    public function getFullDisplayName()
181
    {
182
        $displayName = (string) $this;
183
184
        if (null !== $this->getStudentGroup()) {
185
            $displayName .= ' (' . (string) $this->getStudentGroup() . ')';
186
        }
187
188
        return $displayName;
189
    }
190
191
    /**
192
     * Returns the person's display name
193
     *
194
     * @return string
195
     */
196
    public function getFullPersonDisplayName()
197
    {
198
        $displayName = (string) $this;
199
200
        if (null !== $this->getReference()) {
201
            $displayName .= ' (' . $this->getReference() . ')';
202
        }
203
204
        return $displayName;
205
    }
206
207
    /**
208
     * Get id
209
     *
210
     * @return integer
211
     */
212
    public function getId()
213
    {
214
        return $this->id;
215
    }
216
217
    /**
218
     * Get login username
219
     *
220
     * @return string
221
     */
222
    public function getLoginUsername()
223
    {
224
        return $this->loginUsername;
225
    }
226
227
    /**
228
     * Set login username
229
     *
230
     * @param string $loginUsername
231
     *
232
     * @return User
233
     */
234
    public function setLoginUsername($loginUsername)
235
    {
236
        $this->loginUsername = $loginUsername;
237
238
        return $this;
239
    }
240
241
    /**
242
     * Get password
243
     *
244
     * @return string
245
     */
246
    public function getPassword()
247
    {
248
        return $this->password;
249
    }
250
251
    /**
252
     * Set password
253
     *
254
     * @param string $password
255
     *
256
     * @return User
257
     */
258
    public function setPassword($password)
259
    {
260
        $this->password = $password;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Get token
267
     *
268
     * @return string
269
     */
270
    public function getToken()
271
    {
272
        return $this->token;
273
    }
274
275
    /**
276
     * Set token
277
     *
278
     * @param string $token
279
     *
280
     * @return User
281
     */
282
    public function setToken($token)
283
    {
284
        $this->token = $token;
285
286
        return $this;
287
    }
288
289
    /**
290
     * Get tokenValidity
291
     *
292
     * @return \DateTime
293
     */
294
    public function getTokenValidity()
295
    {
296
        return $this->tokenValidity;
297
    }
298
299
    /**
300
     * Set tokenValidity
301
     *
302
     * @param \DateTime $tokenValidity
303
     *
304
     * @return User
305
     */
306
    public function setTokenValidity($tokenValidity)
307
    {
308
        $this->tokenValidity = $tokenValidity;
309
310
        return $this;
311
    }
312
313
    /**
314
     * Get enabled
315
     *
316
     * @return boolean
317
     */
318
    public function isEnabled()
319
    {
320
        return $this->enabled;
321
    }
322
323
    /**
324
     * Set enabled
325
     *
326
     * @param boolean $enabled
327
     *
328
     * @return User
329
     */
330
    public function setEnabled($enabled)
331
    {
332
        $this->enabled = $enabled;
333
334
        return $this;
335
    }
336
337
    /**
338
     * Get globalAdministrator
339
     *
340
     * @return boolean
341
     */
342
    public function isGlobalAdministrator()
343
    {
344
        return $this->globalAdministrator;
345
    }
346
347
    /**
348
     * Set globalAdministrator
349
     *
350
     * @param boolean $globalAdministrator
351
     *
352
     * @return User
353
     */
354
    public function setGlobalAdministrator($globalAdministrator)
355
    {
356
        $this->globalAdministrator = $globalAdministrator;
357
358
        return $this;
359
    }
360
361
    /**
362
     * String representation of object
363
     * @link http://php.net/manual/en/serializable.serialize.php
364
     * @return string the string representation of the object or null
365
     * @since 5.1.0
366
     */
367
    public function serialize()
368
    {
369
        return serialize(array(
370
            $this->id,
371
            $this->email,
372
            $this->loginUsername,
373
            $this->password
374
        ));
375
    }
376
377
    /**
378
     * Constructs the object
379
     * @link http://php.net/manual/en/serializable.unserialize.php
380
     * @param string $serialized <p>
381
     * The string representation of the object.
382
     * </p>
383
     * @return void
384
     * @since 5.1.0
385
     */
386
    public function unserialize($serialized)
387
    {
388
        list (
389
            $this->id,
390
            $this->email,
391
            $this->loginUsername,
392
            $this->password
393
        ) = unserialize($serialized);
394
    }
395
396
    /**
397
     * The equality comparison should neither be done by referential equality
398
     * nor by comparing identities (i.e. getId() === getId()).
399
     *
400
     * However, you do not need to compare every attribute, but only those that
401
     * are relevant for assessing whether re-authentication is required.
402
     *
403
     * Also implementation should consider that $user instance may implement
404
     * the extended user interface `AdvancedUserInterface`.
405
     *
406
     * @param UserInterface $user
407
     *
408
     * @return bool
409
     */
410
    public function isEqualTo(UserInterface $user)
411
    {
412
        return ($this->getRoles() === $user->getRoles());
413
    }
414
415
    /**
416
     * Returns the salt that was originally used to encode the password.
417
     *
418
     * This can return null if the password was not encoded using a salt.
419
     *
420
     * @return string|null The salt
421
     */
422
    public function getSalt()
423
    {
424
        return null;
425
    }
426
427
    /**
428
     * Removes sensitive data from the user.
429
     *
430
     * This is important if, at any given point, sensitive information like
431
     * the plain-text password is stored on this object.
432
     */
433
    public function eraseCredentials()
434
    {
435
    }
436
437
    /**
438
     * Returns the roles granted to the user.
439
     *
440
     * @return Role[] The user roles
441
     */
442
    public function getRoles()
443
    {
444
        // Always return ROLE_USER
445
        $roles = [new Role('ROLE_USER')];
446
447
        if ($this->isGlobalAdministrator()) {
448
            $roles[] = new Role('ROLE_ADMIN');
449
        }
450
451
        if ($this->tutorizedGroups && $this->tutorizedGroups->count()) {
452
            $roles[] = new Role('ROLE_GROUP_TUTOR');
453
        }
454
455
        if ($this->directs && $this->directs->count()) {
456
            $roles[] = new Role('ROLE_DEPARTMENT_HEAD');
457
        }
458
459
        if ($this->educationalTutorAgreements && $this->educationalTutorAgreements->count()) {
460
            $roles[] = new Role('ROLE_EDUCATIONAL_TUTOR');
461
        }
462
463
        if ($this->studentGroup) {
464
            $roles[] = new Role('ROLE_STUDENT');
465
        }
466
        if ($this->isFinancialManager()) {
467
            $roles[] = new Role('ROLE_FINANCIAL_MANAGER');
468
        }
469
470
        return $roles;
471
    }
472
473
    /**
474
     * Set lastLogin
475
     *
476
     * @param \DateTime $lastLogin
477
     *
478
     * @return User
479
     */
480
    public function setLastLogin($lastLogin)
481
    {
482
        $this->lastLogin = $lastLogin;
483
484
        return $this;
485
    }
486
487
    /**
488
     * Get lastLogin
489
     *
490
     * @return \DateTime
491
     */
492
    public function getLastLogin()
493
    {
494
        return $this->lastLogin;
495
    }
496
497
    /**
498
     * Set group
499
     *
500
     * @param Group $group
501
     *
502
     * @return User
503
     */
504
    public function setStudentGroup(Group $group = null)
505
    {
506
        $this->studentGroup = $group;
507
508
        return $this;
509
    }
510
511
    /**
512
     * Get group
513
     *
514
     * @return Group
515
     */
516
    public function getStudentGroup()
517
    {
518
        return $this->studentGroup;
519
    }
520
521
    /**
522
     * Add tutorizedGroup
523
     *
524
     * @param Group $tutorizedGroup
525
     *
526
     * @return User
527
     */
528
    public function addTutorizedGroup(Group $tutorizedGroup)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
529
    {
530
        if (false === $this->tutorizedGroups->contains($tutorizedGroup)) {
531
            $this->tutorizedGroups[] = $tutorizedGroup;
532
            $tutorizedGroup->addTutor($this);
533
        }
534
535
        return $this;
536
    }
537
538
    /**
539
     * Remove tutorizedGroup
540
     *
541
     * @param Group $tutorizedGroup
542
     */
543
    public function removeTutorizedGroup(Group $tutorizedGroup)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
544
    {
545
        if (true === $this->tutorizedGroups->contains($tutorizedGroup)) {
546
            $this->tutorizedGroups->removeElement($tutorizedGroup);
547
            $tutorizedGroup->removeTutor($this);
548
        }
549
    }
550
551
    /**
552
     * Get tutorizedGroups
553
     *
554
     * @return Collection
555
     */
556
    public function getTutorizedGroups()
557
    {
558
        return $this->tutorizedGroups;
559
    }
560
561
    /**
562
     * Add direct
563
     *
564
     * @param Department $direct
565
     *
566
     * @return User
567
     */
568
    public function addDirect(Department $direct)
569
    {
570
        $this->directs[] = $direct;
571
572
        return $this;
573
    }
574
575
    /**
576
     * Remove direct
577
     *
578
     * @param Department $direct
579
     */
580
    public function removeDirect(Department $direct)
581
    {
582
        $this->directs->removeElement($direct);
583
    }
584
585
    /**
586
     * Get directs
587
     *
588
     * @return Collection
589
     */
590
    public function getDirects()
591
    {
592
        return $this->directs;
593
    }
594
595
    /**
596
     * @Assert\Callback
597
     */
598
    public function validate(ExecutionContextInterface $context)
599
    {
600
        // comprobar si se ha especificado al menos el nombre de usuario o el correo electrónico
601
        if (!$this->getLoginUsername() && !$this->getEmail()) {
602
            $context->buildViolation('user.id.not_found')
603
                ->atPath('loginUsername')
604
                ->addViolation();
605
            $context->buildViolation('user.id.not_found')
606
                ->atPath('email')
607
                ->addViolation();
608
        }
609
    }
610
611
    /**
612
     * Returns the username used to authenticate the user.
613
     *
614
     * @return string The username
615
     */
616
    public function getUsername()
617
    {
618
        return $this->getLoginUsername() ?: $this->getEmail();
619
    }
620
621
    /**
622
     * Add studentAgreement
623
     *
624
     * @param Agreement $studentAgreement
625
     *
626
     * @return User
627
     */
628
    public function addStudentAgreement(Agreement $studentAgreement)
629
    {
630
        $this->studentAgreements[] = $studentAgreement;
631
632
        return $this;
633
    }
634
635
    /**
636
     * Remove studentAgreement
637
     *
638
     * @param Agreement $studentAgreement
639
     */
640
    public function removeStudentAgreement(Agreement $studentAgreement)
641
    {
642
        $this->studentAgreements->removeElement($studentAgreement);
643
    }
644
645
    /**
646
     * Get studentAgreements
647
     *
648
     * @return Collection
649
     */
650
    public function getStudentAgreements()
651
    {
652
        return $this->studentAgreements;
653
    }
654
655
    /**
656
     * Add educationalTutorAgreement
657
     *
658
     * @param Agreement $educationalTutorAgreement
659
     *
660
     * @return User
661
     */
662
    public function addEducationalTutorAgreement(Agreement $educationalTutorAgreement)
663
    {
664
        $this->educationalTutorAgreements[] = $educationalTutorAgreement;
665
666
        return $this;
667
    }
668
669
    /**
670
     * Remove educationalTutorAgreement
671
     *
672
     * @param Agreement $educationalTutorAgreement
673
     */
674
    public function removeEducationalTutorAgreement(Agreement $educationalTutorAgreement)
675
    {
676
        $this->educationalTutorAgreements->removeElement($educationalTutorAgreement);
677
    }
678
679
    /**
680
     * Get educationalTutorAgreements
681
     *
682
     * @return Collection
683
     */
684
    public function getEducationalTutorAgreements()
685
    {
686
        return $this->educationalTutorAgreements;
687
    }
688
689
    /**
690
     * Add workTutorAgreement
691
     *
692
     * @param Agreement $workTutorAgreement
693
     *
694
     * @return User
695
     */
696
    public function addWorkTutorAgreement(Agreement $workTutorAgreement)
697
    {
698
        $this->workTutorAgreements[] = $workTutorAgreement;
699
700
        return $this;
701
    }
702
703
    /**
704
     * Remove workTutorAgreement
705
     *
706
     * @param Agreement $workTutorAgreement
707
     */
708
    public function removeWorkTutorAgreement(Agreement $workTutorAgreement)
709
    {
710
        $this->workTutorAgreements->removeElement($workTutorAgreement);
711
    }
712
713
    /**
714
     * Get workTutorAgreements
715
     *
716
     * @return Collection
717
     */
718
    public function getWorkTutorAgreements()
719
    {
720
        return $this->workTutorAgreements;
721
    }
722
723
    /**
724
     * Add expense
725
     *
726
     * @param \AppBundle\Entity\Expense $expense
727
     *
728
     * @return User
729
     */
730
    public function addExpense(\AppBundle\Entity\Expense $expense)
731
    {
732
        $this->expenses[] = $expense;
733
734
        return $this;
735
    }
736
737
    /**
738
     * Remove expense
739
     *
740
     * @param \AppBundle\Entity\Expense $expense
741
     */
742
    public function removeExpense(\AppBundle\Entity\Expense $expense)
743
    {
744
        $this->expenses->removeElement($expense);
745
    }
746
747
    /**
748
     * Get expenses
749
     *
750
     * @return Collection
751
     */
752
    public function getExpenses()
753
    {
754
        return $this->expenses;
755
    }
756
757
    /**
758
     * Add visit
759
     *
760
     * @param Visit $visit
761
     *
762
     * @return User
763
     */
764
    public function addVisit(Visit $visit)
765
    {
766
        $this->visits[] = $visit;
767
768
        return $this;
769
    }
770
771
    /**
772
     * Remove visit
773
     *
774
     * @param Visit $visit
775
     */
776
    public function removeVisit(Visit $visit)
777
    {
778
        $this->visits->removeElement($visit);
779
    }
780
781
    /**
782
     * Get visits
783
     *
784
     * @return Collection
785
     */
786
    public function getVisits()
787
    {
788
        return $this->visits;
789
    }
790
791
    /**
792
     * Set financialManager
793
     *
794
     * @param boolean $financialManager
795
     *
796
     * @return User
797
     */
798
    public function setFinancialManager($financialManager)
799
    {
800
        $this->financialManager = $financialManager;
801
802
        return $this;
803
    }
804
805
    /**
806
     * Get financialManager
807
     *
808
     * @return boolean
809
     */
810
    public function isFinancialManager()
811
    {
812
        return $this->financialManager;
813
    }
814
}
815