| Conditions | 18 |
| Paths | 20 |
| Total Lines | 84 |
| Code Lines | 40 |
| 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 |
||
| 94 | protected function voteOnAttribute($attribute, $session, TokenInterface $token): bool |
||
| 95 | { |
||
| 96 | /** @var User $user */ |
||
| 97 | $user = $token->getUser(); |
||
| 98 | |||
| 99 | // Make sure there is a user object (i.e. that the user is logged in) |
||
| 100 | if (!$user instanceof UserInterface) { |
||
| 101 | return false; |
||
| 102 | } |
||
| 103 | |||
| 104 | $authChecker = $this->getAuthorizationChecker(); |
||
| 105 | |||
| 106 | // Admins have access to everything |
||
| 107 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
| 108 | return true; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Checks if the current course was set up |
||
| 112 | // $session->getCurrentCourse() is set in the class CourseListener |
||
| 113 | /** @var Session $session */ |
||
| 114 | $currentCourse = $session->getCurrentCourse(); |
||
| 115 | |||
| 116 | switch ($attribute) { |
||
| 117 | case self::VIEW: |
||
| 118 | $userIsGeneralCoach = $session->isUserGeneralCoach($user); |
||
| 119 | $userIsCourseCoach = $session->hasCoachInCourseWithStatus($user, $currentCourse); |
||
| 120 | $userIsStudent = $session->hasUserInCourse($user, $currentCourse, Session::STUDENT); |
||
| 121 | |||
| 122 | if (empty($session->getDuration())) { |
||
| 123 | // General coach |
||
| 124 | if ($userIsGeneralCoach && $session->isActiveForCoach()) { |
||
| 125 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 126 | |||
| 127 | return true; |
||
| 128 | } |
||
| 129 | // Course-Coach access |
||
| 130 | if ($userIsCourseCoach && $session->isActiveForCoach()) { |
||
| 131 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Student access |
||
| 137 | if ($userIsStudent && $session->isActiveForStudent()) { |
||
| 138 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_STUDENT'); |
||
| 139 | |||
| 140 | return true; |
||
| 141 | } |
||
| 142 | |||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($this->sessionIsAvailableByDuration($session, $user)) { |
||
| 147 | if ($userIsGeneralCoach) { |
||
| 148 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($userIsCourseCoach) { |
||
| 152 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($userIsStudent) { |
||
| 156 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_STUDENT'); |
||
| 157 | } |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | return false; |
||
| 163 | case self::EDIT: |
||
| 164 | case self::DELETE: |
||
| 165 | $canEdit = $this->canEditSession($user, $session, false); |
||
| 166 | |||
| 167 | if ($canEdit) { |
||
| 168 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 169 | |||
| 170 | return true; |
||
| 171 | } |
||
| 172 | |||
| 173 | return false; |
||
| 174 | } |
||
| 175 | |||
| 176 | // User don't have access to the session |
||
| 177 | return false; |
||
| 178 | } |
||
| 324 |