| Conditions | 20 |
| Paths | 68 |
| Total Lines | 101 |
| Code Lines | 54 |
| 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 |
||
| 55 | protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
||
| 56 | { |
||
| 57 | /** @var User $user */ |
||
| 58 | $user = $token->getUser(); |
||
| 59 | |||
| 60 | // make sure there is a user object (i.e. that the user is logged in) |
||
| 61 | if (!$user instanceof UserInterface) { |
||
| 62 | return false; |
||
| 63 | } |
||
| 64 | |||
| 65 | if (false === $subject) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | // Admins have access to everything |
||
| 70 | if ($this->security->isGranted('ROLE_ADMIN')) { |
||
| 71 | return true; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** @var CGroup $group */ |
||
| 75 | $group = $subject; |
||
| 76 | |||
| 77 | // Legacy |
||
| 78 | //\GroupManager::userHasAccessToBrowse($user->getId(), $group); |
||
| 79 | $isTutor = $group->hasTutor($user); |
||
| 80 | |||
| 81 | switch ($attribute) { |
||
| 82 | case self::VIEW: |
||
| 83 | if ($isTutor) { |
||
| 84 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_TEACHER); |
||
| 85 | |||
| 86 | return true; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!$group->getStatus()) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | $userIsInGroup = $group->hasMember($user); |
||
| 94 | |||
| 95 | if ($userIsInGroup) { |
||
| 96 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_STUDENT); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Check if user has access in legacy tool. |
||
| 100 | $request = $this->requestStack->getCurrentRequest(); |
||
| 101 | $requestUri = $request->getRequestUri(); |
||
| 102 | |||
| 103 | $tools = [ |
||
| 104 | '/main/forum/' => $group->getForumState(), |
||
| 105 | '/documents/' => $group->getDocState(), |
||
| 106 | '/main/calendar/' => $group->getCalendarState(), |
||
| 107 | '/main/announcements/' => $group->getAnnouncementsState(), |
||
| 108 | '/main/work/' => $group->getWorkState(), |
||
| 109 | '/main/wiki/' => $group->getWikiState(), |
||
| 110 | //'/main/chat/' => $group->getAnnouncementsState(), ?? |
||
| 111 | ]; |
||
| 112 | |||
| 113 | $toolStatus = null; |
||
| 114 | foreach ($tools as $path => $status) { |
||
| 115 | if (false !== strpos($requestUri, $path)) { |
||
| 116 | $toolStatus = $status; |
||
| 117 | |||
| 118 | break; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | switch ($toolStatus) { |
||
| 123 | case GroupManager::TOOL_NOT_AVAILABLE: |
||
| 124 | return false; |
||
| 125 | case GroupManager::TOOL_PUBLIC: |
||
| 126 | return true; |
||
| 127 | case GroupManager::TOOL_PRIVATE: |
||
| 128 | if ($userIsInGroup) { |
||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | break; |
||
| 133 | case GroupManager::TOOL_PRIVATE_BETWEEN_USERS: |
||
| 134 | // Only works for announcements for now |
||
| 135 | if ($userIsInGroup && '/main/announcements/' === $path) { |
||
|
|
|||
| 136 | return true; |
||
| 137 | } |
||
| 138 | |||
| 139 | break; |
||
| 140 | } |
||
| 141 | |||
| 142 | break; |
||
| 143 | case self::EDIT: |
||
| 144 | case self::DELETE: |
||
| 145 | if ($isTutor) { |
||
| 146 | $user->addRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_GROUP_TEACHER); |
||
| 147 | |||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | break; |
||
| 152 | } |
||
| 153 | //dump("You don't have access to this group!!"); |
||
| 154 | |||
| 155 | return false; |
||
| 156 | } |
||
| 158 |