| Conditions | 13 |
| Paths | 14 |
| Total Lines | 76 |
| Code Lines | 33 |
| 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 |
||
| 102 | protected function voteOnAttribute($attribute, $course, TokenInterface $token): bool |
||
| 103 | { |
||
| 104 | /** @var User $user */ |
||
| 105 | $user = $token->getUser(); |
||
| 106 | // Anons can enter a course depending of the course visibility |
||
| 107 | /*if (!$user instanceof UserInterface) { |
||
| 108 | return false; |
||
| 109 | }*/ |
||
| 110 | |||
| 111 | $authChecker = $this->getAuthorizationChecker(); |
||
| 112 | |||
| 113 | // Admins have access to everything |
||
| 114 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
| 115 | return true; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Course is active? |
||
| 119 | /** @var Course $course */ |
||
| 120 | switch ($attribute) { |
||
| 121 | case self::VIEW: |
||
| 122 | // Course is hidden then is not visible for nobody expect admins. |
||
| 123 | if ($course->getVisibility() == Course::HIDDEN) { |
||
| 124 | return false; |
||
| 125 | } |
||
| 126 | |||
| 127 | // "Open to the world" no need to check if user is registered or if user exists. |
||
| 128 | // Course::OPEN_WORLD |
||
| 129 | if ($course->isPublic()) { |
||
| 130 | return true; |
||
| 131 | } |
||
| 132 | |||
| 133 | // User should be instance of UserInterface. |
||
| 134 | if (!$user instanceof UserInterface) { |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | // If user is logged in and is open platform, allow access. |
||
| 139 | if ($course->getVisibility() == Course::OPEN_PLATFORM) { |
||
| 140 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT); |
||
| 141 | |||
| 142 | if ($course->hasTeacher($user)) { |
||
| 143 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 144 | } |
||
| 145 | |||
| 146 | $token->setUser($user); |
||
| 147 | |||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Course::REGISTERED |
||
| 152 | // User must be subscribed in the course no matter if is teacher/student |
||
| 153 | if ($course->hasUser($user)) { |
||
| 154 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_STUDENT); |
||
| 155 | |||
| 156 | if ($course->hasTeacher($user)) { |
||
| 157 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 158 | } |
||
| 159 | |||
| 160 | $token->setUser($user); |
||
| 161 | |||
| 162 | return true; |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | case self::EDIT: |
||
| 166 | case self::DELETE: |
||
| 167 | // Only teacher can edit/delete stuff |
||
| 168 | if ($course->hasTeacher($user)) { |
||
| 169 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER); |
||
| 170 | $token->setUser($user); |
||
| 171 | |||
| 172 | return true; |
||
| 173 | } |
||
| 174 | break; |
||
| 175 | } |
||
| 176 | |||
| 177 | return false; |
||
| 178 | } |
||
| 180 |