| Conditions | 21 |
| Paths | 34 |
| Total Lines | 98 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 49 | protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
||
| 50 | { |
||
| 51 | /** @var User $user */ |
||
| 52 | $user = $token->getUser(); |
||
| 53 | |||
| 54 | // Make sure there is a user object (i.e. that the user is logged in) |
||
| 55 | if (!$user instanceof UserInterface) { |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Admins have access to everything. |
||
| 60 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 61 | return true; |
||
| 62 | } |
||
| 63 | |||
| 64 | // Checks if the current course was set up |
||
| 65 | // $session->getCurrentCourse() is set in the class CidReqListener. |
||
| 66 | /** @var Session $session */ |
||
| 67 | $session = $subject; |
||
| 68 | $currentCourse = $session->getCurrentCourse(); |
||
| 69 | |||
| 70 | // Course checks. |
||
| 71 | if ($currentCourse && $currentCourse->isHidden()) { |
||
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | switch ($attribute) { |
||
| 76 | case self::VIEW: |
||
| 77 | // @todo improve performance. |
||
| 78 | $userIsGeneralCoach = $session->hasUserAsGeneralCoach($user); |
||
| 79 | if (null === $currentCourse) { |
||
| 80 | $userIsStudent = $session->getSessionRelCourseByUser($user, Session::STUDENT)->count() > 0; |
||
| 81 | $userIsCourseCoach = $session->hasCoachInCourseList($user); // The current course will be checked in CourseVoter. |
||
| 82 | } else { |
||
| 83 | $userIsCourseCoach = $session->hasCourseCoachInCourse($user, $currentCourse); |
||
| 84 | $userIsStudent = $session->hasUserInCourse($user, $currentCourse, Session::STUDENT); |
||
| 85 | } |
||
| 86 | $duration = (int) $session->getDuration(); |
||
| 87 | if (0 === $duration) { |
||
| 88 | // General coach. |
||
| 89 | if ($userIsGeneralCoach && $session->isActiveForCoach()) { |
||
| 90 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 91 | |||
| 92 | return true; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Course-Coach access. |
||
| 96 | if ($userIsCourseCoach && $session->isActiveForCoach()) { |
||
| 97 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 98 | |||
| 99 | return true; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Student access. |
||
| 103 | if ($userIsStudent && $session->isActiveForStudent()) { |
||
| 104 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_STUDENT); |
||
| 105 | |||
| 106 | return true; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($session->isAvailableByDurationForUser($user)) { |
||
| 111 | if ($userIsGeneralCoach) { |
||
| 112 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($userIsCourseCoach) { |
||
| 118 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 119 | |||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($userIsStudent) { |
||
| 124 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_STUDENT); |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | return false; |
||
| 131 | |||
| 132 | case self::EDIT: |
||
| 133 | case self::DELETE: |
||
| 134 | $canEdit = $this->canEditSession($user, $session, false); |
||
| 135 | |||
| 136 | if ($canEdit) { |
||
| 137 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 138 | |||
| 139 | return true; |
||
| 140 | } |
||
| 141 | |||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | // User don't have access to the session |
||
| 146 | return false; |
||
| 147 | } |
||
| 205 |