| Conditions | 22 |
| Paths | 47 |
| Total Lines | 136 |
| Code Lines | 75 |
| Lines | 36 |
| Ratio | 26.47 % |
| 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 |
||
| 79 | protected function voteOnAttribute($attribute, $session, TokenInterface $token) |
||
| 80 | { |
||
| 81 | /** @var User $user */ |
||
| 82 | $user = $token->getUser(); |
||
| 83 | |||
| 84 | // Make sure there is a user object (i.e. that the user is logged in) |
||
| 85 | if (!$user instanceof UserInterface) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Checks if the current course was set up |
||
| 90 | // $session->getCurrentCourse() is set in the class CourseListener |
||
| 91 | /** @var Session $session */ |
||
| 92 | $course = $session->getCurrentCourse(); |
||
| 93 | |||
| 94 | if ($course == false) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | $authChecker = $this->container->get('security.authorization_checker'); |
||
| 99 | |||
| 100 | // Admins have access to everything |
||
| 101 | if ($authChecker->isGranted('ROLE_ADMIN')) { |
||
| 102 | return true; |
||
| 103 | } |
||
| 104 | |||
| 105 | $sessionId = $session->getId(); |
||
| 106 | $userId = $user->getId(); |
||
| 107 | |||
| 108 | switch ($attribute) { |
||
| 109 | case self::VIEW: |
||
| 110 | // General coach |
||
| 111 | $generalCoach = $session->getGeneralCoach(); |
||
| 112 | View Code Duplication | if ($generalCoach) { |
|
| 113 | $coachId = $generalCoach->getId(); |
||
| 114 | $userId = $user->getId(); |
||
| 115 | if ($coachId == $userId) { |
||
| 116 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 117 | |||
| 118 | return true; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // Course-Coach access |
||
| 123 | View Code Duplication | if ($session->hasCoachInCourseWithStatus($user, $course)) { |
|
| 124 | if (!$session->isActiveForCoach()) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | |||
| 131 | // Student access |
||
| 132 | if ($session->hasUserInCourse($user, $course)) { |
||
| 133 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_STUDENT'); |
||
| 134 | |||
| 135 | // Session duration per student. |
||
| 136 | if (!empty($session->getDuration())) { |
||
| 137 | $duration = $session->getDuration() * 24 * 60 * 60; |
||
| 138 | |||
| 139 | $courseAccess = \CourseManager::getFirstCourseAccessPerSessionAndUser( |
||
| 140 | $sessionId, |
||
| 141 | $userId |
||
| 142 | ); |
||
| 143 | |||
| 144 | // If there is a session duration but there is no previous |
||
| 145 | // access by the user, then the session is still available |
||
| 146 | if (count($courseAccess) == 0) { |
||
| 147 | return true; |
||
| 148 | } |
||
| 149 | |||
| 150 | $currentTime = time(); |
||
| 151 | $firstAccess = 0; |
||
| 152 | if (isset($courseAccess['login_course_date'])) { |
||
| 153 | $firstAccess = api_strtotime( |
||
| 154 | $courseAccess['login_course_date'], |
||
| 155 | 'UTC' |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | $userDurationData = \SessionManager::getUserSession( |
||
| 159 | $userId, |
||
| 160 | $sessionId |
||
| 161 | ); |
||
| 162 | $userDuration = 0; |
||
| 163 | View Code Duplication | if (isset($userDurationData['duration'])) { |
|
| 164 | $userDuration = intval($userDurationData['duration']) * 24 * 60 * 60; |
||
| 165 | } |
||
| 166 | |||
| 167 | $totalDuration = $firstAccess + $duration + $userDuration; |
||
| 168 | if ($totalDuration > $currentTime) { |
||
| 169 | return true; |
||
| 170 | } else { |
||
| 171 | return false; |
||
| 172 | } |
||
| 173 | } else { |
||
| 174 | if (!$session->isActiveForStudent()) { |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return true; |
||
| 180 | } |
||
| 181 | |||
| 182 | return false; |
||
| 183 | break; |
||
| 184 | case self::EDIT: |
||
| 185 | case self::DELETE: |
||
| 186 | // General coach check |
||
| 187 | $generalCoach = $session->getGeneralCoach(); |
||
| 188 | View Code Duplication | if ($generalCoach) { |
|
| 189 | $coachId = $generalCoach->getId(); |
||
| 190 | $userId = $user->getId(); |
||
| 191 | if ($coachId == $userId) { |
||
| 192 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 193 | |||
| 194 | return true; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | // Course session check |
||
| 199 | View Code Duplication | if ($session->hasCoachInCourseWithStatus($user, $course)) { |
|
| 200 | if (!$session->isActiveForCoach()) { |
||
| 201 | return false; |
||
| 202 | } |
||
| 203 | $user->addRole('ROLE_CURRENT_SESSION_COURSE_TEACHER'); |
||
| 204 | |||
| 205 | return true; |
||
| 206 | } |
||
| 207 | return false; |
||
| 208 | break; |
||
| 209 | } |
||
| 210 | |||
| 211 | // User don't have access to the session |
||
| 212 | return false; |
||
| 213 | |||
| 214 | } |
||
| 215 | } |
||
| 217 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.