| Conditions | 18 |
| Paths | 20 |
| Total Lines | 87 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 59 | protected function voteOnAttribute(string $attribute, $session, TokenInterface $token): bool |
||
| 60 | { |
||
| 61 | /** @var User $user */ |
||
| 62 | $user = $token->getUser(); |
||
| 63 | |||
| 64 | // Make sure there is a user object (i.e. that the user is logged in) |
||
| 65 | if (!$user instanceof UserInterface) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | // Admins have access to everything |
||
| 70 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 71 | return true; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Checks if the current course was set up |
||
| 75 | // $session->getCurrentCourse() is set in the class CourseListener |
||
| 76 | /** @var Session $session */ |
||
| 77 | $currentCourse = $session->getCurrentCourse(); |
||
| 78 | |||
| 79 | switch ($attribute) { |
||
| 80 | case self::VIEW: |
||
| 81 | $userIsGeneralCoach = $session->isUserGeneralCoach($user); |
||
| 82 | $userIsCourseCoach = $session->hasCoachInCourseWithStatus($user, $currentCourse); |
||
| 83 | $userIsStudent = $session->hasUserInCourse($user, $currentCourse, Session::STUDENT); |
||
| 84 | |||
| 85 | if (empty($session->getDuration())) { |
||
| 86 | // General coach. |
||
| 87 | if ($userIsGeneralCoach && $session->isActiveForCoach()) { |
||
| 88 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER); |
||
| 89 | |||
| 90 | return true; |
||
| 91 | } |
||
| 92 | |||
| 93 | // Course-Coach access. |
||
| 94 | if ($userIsCourseCoach && $session->isActiveForCoach()) { |
||
| 95 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER); |
||
| 96 | |||
| 97 | return true; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Student access |
||
| 101 | if ($userIsStudent && $session->isActiveForStudent()) { |
||
| 102 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_STUDENT); |
||
| 103 | |||
| 104 | //$token->setUser($user); |
||
| 105 | |||
| 106 | return true; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($this->sessionIsAvailableByDuration($session, $user)) { |
||
| 111 | if ($userIsGeneralCoach) { |
||
| 112 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER); |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($userIsCourseCoach) { |
||
| 118 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER); |
||
| 119 | |||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($userIsStudent) { |
||
| 124 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_STUDENT); |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | return false; |
||
| 131 | case self::EDIT: |
||
| 132 | case self::DELETE: |
||
| 133 | $canEdit = $this->canEditSession($user, $session, false); |
||
| 134 | |||
| 135 | if ($canEdit) { |
||
| 136 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_SESSION_COURSE_TEACHER); |
||
| 137 | |||
| 138 | return true; |
||
| 139 | } |
||
| 140 | |||
| 141 | return false; |
||
| 142 | } |
||
| 143 | |||
| 144 | // User don't have access to the session |
||
| 145 | return false; |
||
| 146 | } |
||
| 266 |