| Conditions | 24 |
| Paths | 57 |
| Total Lines | 138 |
| Code Lines | 71 |
| 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 |
||
| 61 | protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
||
| 62 | { |
||
| 63 | /** @var User $user */ |
||
| 64 | $user = $token->getUser(); |
||
| 65 | |||
| 66 | // Admins have access to everything. |
||
| 67 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 68 | return true; |
||
| 69 | } |
||
| 70 | |||
| 71 | $request = $this->requestStack->getCurrentRequest(); |
||
| 72 | $sessionId = $request->query->get('sid'); |
||
| 73 | $sessionRepository = $this->entityManager->getRepository(Session::class); |
||
| 74 | |||
| 75 | // Course is active? |
||
| 76 | /** @var Course $course */ |
||
| 77 | $course = $subject; |
||
| 78 | |||
| 79 | $session = null; |
||
| 80 | if ($sessionId) { |
||
| 81 | // Session is active? |
||
| 82 | /** @var Session $session */ |
||
| 83 | $session = $sessionRepository->find($sessionId); |
||
| 84 | } |
||
| 85 | |||
| 86 | switch ($attribute) { |
||
| 87 | case self::VIEW: |
||
| 88 | // Course is hidden then is not visible for nobody expect admins. |
||
| 89 | if ($course->isHidden()) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | // "Open to the world" no need to check if user is registered or if user exists. |
||
| 94 | // Course::OPEN_WORLD |
||
| 95 | if ($course->isPublic()) { |
||
| 96 | if ($this->isStudent($user, $course, $session)) { |
||
| 97 | if ($this->isCourseLockedForUser($user, $course, $session?->getId() ?? 0)) { |
||
| 98 | throw new NotAllowedException( |
||
| 99 | $this->translator->trans('This course is locked. You must complete the prerequisite(s) first.'), |
||
| 100 | 'warning', |
||
| 101 | 403 |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | return true; |
||
| 106 | } |
||
| 107 | |||
| 108 | // User should be instance of UserInterface. |
||
| 109 | if (!$user instanceof UserInterface) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | |||
| 113 | // If user is logged in and is open platform, allow access. |
||
| 114 | if (Course::OPEN_PLATFORM === $course->getVisibility()) { |
||
| 115 | if ($this->isStudent($user, $course, $session)) { |
||
| 116 | if ($this->isCourseLockedForUser($user, $course, $session?->getId() ?? 0)) { |
||
| 117 | throw new NotAllowedException( |
||
| 118 | $this->translator->trans('This course is locked. You must complete the prerequisite(s) first.'), |
||
| 119 | 'warning', |
||
| 120 | 403 |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT); |
||
| 126 | |||
| 127 | if ($course->hasUserAsTeacher($user)) { |
||
| 128 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 129 | } |
||
| 130 | |||
| 131 | $token->setUser($user); |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Validation in session |
||
| 137 | if ($session) { |
||
| 138 | $userIsGeneralCoach = $session->hasUserAsGeneralCoach($user); |
||
| 139 | $userIsCourseCoach = $session->hasCourseCoachInCourse($user, $course); |
||
| 140 | $userIsStudent = $session->hasUserInCourse($user, $course, Session::STUDENT); |
||
| 141 | |||
| 142 | if ($userIsGeneralCoach || $userIsCourseCoach) { |
||
| 143 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_TEACHER); |
||
| 144 | return true; |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($userIsStudent) { |
||
| 148 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_SESSION_STUDENT); |
||
| 149 | if ($this->isCourseLockedForUser($user, $course, $session->getId())) { |
||
| 150 | throw new NotAllowedException( |
||
| 151 | $this->translator->trans('This course is locked. You must complete the prerequisite(s) first.'), |
||
| 152 | 'warning', |
||
| 153 | 403 |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | return true; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | // Course::REGISTERED |
||
| 162 | // User must be subscribed in the course no matter if is teacher/student |
||
| 163 | if ($course->hasSubscriptionByUser($user)) { |
||
| 164 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT); |
||
| 165 | |||
| 166 | if ($course->hasUserAsTeacher($user)) { |
||
| 167 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 168 | } |
||
| 169 | |||
| 170 | if ($this->isCourseLockedForUser($user, $course)) { |
||
| 171 | throw new NotAllowedException( |
||
| 172 | $this->translator->trans('This course is locked. You must complete the prerequisite(s) first.'), |
||
| 173 | 'warning', |
||
| 174 | 403 |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | $token->setUser($user); |
||
| 179 | |||
| 180 | return true; |
||
| 181 | } |
||
| 182 | |||
| 183 | break; |
||
| 184 | |||
| 185 | case self::EDIT: |
||
| 186 | case self::DELETE: |
||
| 187 | // Only teacher can edit/delete stuff. |
||
| 188 | if ($course->hasUserAsTeacher($user)) { |
||
| 189 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 190 | $token->setUser($user); |
||
| 191 | |||
| 192 | return true; |
||
| 193 | } |
||
| 194 | |||
| 195 | break; |
||
| 196 | } |
||
| 197 | |||
| 198 | return false; |
||
| 199 | } |
||
| 236 |