| Total Complexity | 54 |
| Total Lines | 410 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SessionRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SessionRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class SessionRepository extends ServiceEntityRepository |
||
| 28 | { |
||
| 29 | public function __construct( |
||
| 30 | ManagerRegistry $registry, |
||
| 31 | private readonly SettingsManager $settingsManager, |
||
| 32 | ) { |
||
| 33 | parent::__construct($registry, Session::class); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function create(): ?Session |
||
| 37 | { |
||
| 38 | return new Session(); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function update(Session $session): void |
||
| 42 | { |
||
| 43 | $this->getEntityManager()->persist($session); |
||
| 44 | $this->getEntityManager()->flush(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return array<SessionRelUser> |
||
| 49 | */ |
||
| 50 | public function getUsersByAccessUrl(Session $session, AccessUrl $url, array $relationTypeList = []): array |
||
| 51 | { |
||
| 52 | if (0 === $session->getUsers()->count()) { |
||
| 53 | return []; |
||
| 54 | } |
||
| 55 | |||
| 56 | $qb = $this->addSessionRelUserFilterByUrl($session, $url); |
||
| 57 | $qb->orderBy('sru.relationType'); |
||
| 58 | |||
| 59 | if ($relationTypeList) { |
||
|
|
|||
| 60 | $qb->andWhere( |
||
| 61 | $qb->expr()->in('sru.relationType', $relationTypeList) |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $qb->getQuery()->getResult(); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getSessionsByUser(User $user, AccessUrl $url): QueryBuilder |
||
| 69 | { |
||
| 70 | $qb = $this->createQueryBuilder('s'); |
||
| 71 | $qb |
||
| 72 | ->innerJoin('s.users', 'sru') |
||
| 73 | ->leftJoin('s.urls', 'urls') |
||
| 74 | ->where($qb->expr()->eq('sru.user', ':user')) |
||
| 75 | ->andWhere($qb->expr()->eq('urls.url', ':url')) |
||
| 76 | ->setParameters([ |
||
| 77 | 'user' => $user, |
||
| 78 | 'url' => $url, |
||
| 79 | ]) |
||
| 80 | ; |
||
| 81 | |||
| 82 | return $qb; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return array<int, Session> |
||
| 87 | * |
||
| 88 | * @throws Exception |
||
| 89 | */ |
||
| 90 | public function getPastSessionsOfUserInUrl(User $user, AccessUrl $url): array |
||
| 91 | { |
||
| 92 | $sessions = $this->getSubscribedSessionOfUserInUrl($user, $url); |
||
| 93 | |||
| 94 | $filterPastSessions = function (Session $session) use ($user) { |
||
| 95 | $now = new DateTime(); |
||
| 96 | |||
| 97 | // Check if the session has a duration |
||
| 98 | if ($session->getDuration() > 0) { |
||
| 99 | $daysLeft = $session->getDaysLeftByUser($user); |
||
| 100 | |||
| 101 | return $daysLeft < 0; |
||
| 102 | } |
||
| 103 | |||
| 104 | // Get the appropriate end date based on whether the user is a coach |
||
| 105 | $sessionEndDate = $session->hasCoach($user) && $session->getCoachAccessEndDate() |
||
| 106 | ? $session->getCoachAccessEndDate() |
||
| 107 | : $session->getAccessEndDate(); |
||
| 108 | |||
| 109 | // If there's no end date, the session is not considered past |
||
| 110 | if (!$sessionEndDate) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | // Check if the current date is after the end date |
||
| 115 | return $now > $sessionEndDate; |
||
| 116 | }; |
||
| 117 | |||
| 118 | return array_filter($sessions, $filterPastSessions); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return array<int, Session> |
||
| 123 | * |
||
| 124 | * @throws Exception |
||
| 125 | */ |
||
| 126 | public function getCurrentSessionsOfUserInUrl(User $user, AccessUrl $url): array |
||
| 127 | { |
||
| 128 | $sessions = $this->getSubscribedSessionOfUserInUrl($user, $url); |
||
| 129 | |||
| 130 | $filterCurrentSessions = function (Session $session) use ($user) { |
||
| 131 | // Check if session has a duration |
||
| 132 | if ($session->getDuration() > 0) { |
||
| 133 | $daysLeft = $session->getDaysLeftByUser($user); |
||
| 134 | |||
| 135 | return $daysLeft >= 0 && $daysLeft < $session->getDuration(); |
||
| 136 | } |
||
| 137 | |||
| 138 | // Determine if the user is a coach |
||
| 139 | $userIsCoach = $session->hasCoach($user); |
||
| 140 | |||
| 141 | // Determine the start date based on whether the user is a coach |
||
| 142 | $sessionStartDate = $userIsCoach && $session->getCoachAccessStartDate() |
||
| 143 | ? $session->getCoachAccessStartDate() |
||
| 144 | : $session->getAccessStartDate(); |
||
| 145 | |||
| 146 | // If there is no start date, consider the session current |
||
| 147 | if (!$sessionStartDate) { |
||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | // Get the current date and time |
||
| 152 | $now = new DateTime(); |
||
| 153 | |||
| 154 | // Determine the end date based on whether the user is a coach |
||
| 155 | $sessionEndDate = $userIsCoach && $session->getCoachAccessEndDate() |
||
| 156 | ? $session->getCoachAccessEndDate() |
||
| 157 | : $session->getAccessEndDate(); |
||
| 158 | |||
| 159 | // Check if the current date is within the start and end dates |
||
| 160 | return $now >= $sessionStartDate && (!$sessionEndDate || $now <= $sessionEndDate); |
||
| 161 | }; |
||
| 162 | |||
| 163 | return array_filter($sessions, $filterCurrentSessions); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return array<int, Session> |
||
| 168 | * |
||
| 169 | * @throws Exception |
||
| 170 | */ |
||
| 171 | public function getUpcomingSessionsOfUserInUrl(User $user, AccessUrl $url): array |
||
| 172 | { |
||
| 173 | $sessions = $this->getSubscribedSessionOfUserInUrl($user, $url); |
||
| 174 | |||
| 175 | $filterUpcomingSessions = function (Session $session) use ($user) { |
||
| 176 | $now = new DateTime(); |
||
| 177 | |||
| 178 | // Check if the session has a duration |
||
| 179 | if ($session->getDuration() > 0) { |
||
| 180 | $daysLeft = $session->getDaysLeftByUser($user); |
||
| 181 | |||
| 182 | return $daysLeft >= $session->getDuration(); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Determine if the user is a coach |
||
| 186 | $userIsCoach = $session->hasCoach($user); |
||
| 187 | |||
| 188 | // Get the appropriate start date based on whether the user is a coach |
||
| 189 | $sessionStartDate = $userIsCoach && $session->getCoachAccessStartDate() |
||
| 190 | ? $session->getCoachAccessStartDate() |
||
| 191 | : $session->getAccessStartDate(); |
||
| 192 | |||
| 193 | // If there's no start date, the session is not considered future |
||
| 194 | if (!$sessionStartDate) { |
||
| 195 | return false; |
||
| 196 | } |
||
| 197 | |||
| 198 | // Check if the current date is before the start date |
||
| 199 | return $now < $sessionStartDate; |
||
| 200 | }; |
||
| 201 | |||
| 202 | return array_filter($sessions, $filterUpcomingSessions); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function addUserInCourse(int $relationType, User $user, Course $course, Session $session): void |
||
| 206 | { |
||
| 207 | if (!$user->isActive()) { |
||
| 208 | throw new Exception('User not active'); |
||
| 209 | } |
||
| 210 | |||
| 211 | if (!$session->hasCourse($course)) { |
||
| 212 | $msg = sprintf('Course %s is not subscribed to the session %s', $course->getTitle(), $session->getTitle()); |
||
| 213 | |||
| 214 | throw new Exception($msg); |
||
| 215 | } |
||
| 216 | |||
| 217 | if (!\in_array($relationType, Session::getRelationTypeList(), true)) { |
||
| 218 | throw new Exception(sprintf('Cannot handle relationType %s', $relationType)); |
||
| 219 | } |
||
| 220 | |||
| 221 | switch ($relationType) { |
||
| 222 | case Session::DRH: |
||
| 223 | if ($user->hasRole('ROLE_RRHH')) { |
||
| 224 | $session->addUserInSession(Session::DRH, $user); |
||
| 225 | } |
||
| 226 | |||
| 227 | break; |
||
| 228 | |||
| 229 | case Session::STUDENT: |
||
| 230 | $session |
||
| 231 | ->addUserInSession(Session::STUDENT, $user) |
||
| 232 | ->addUserInCourse(Session::STUDENT, $user, $course) |
||
| 233 | ; |
||
| 234 | |||
| 235 | break; |
||
| 236 | |||
| 237 | case Session::COURSE_COACH: |
||
| 238 | if ($user->hasRole('ROLE_TEACHER')) { |
||
| 239 | $session |
||
| 240 | ->addUserInSession(Session::COURSE_COACH, $user) |
||
| 241 | ->addUserInCourse( |
||
| 242 | Session::COURSE_COACH, |
||
| 243 | $user, |
||
| 244 | $course |
||
| 245 | ) |
||
| 246 | ; |
||
| 247 | } |
||
| 248 | |||
| 249 | break; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return array<SessionRelCourse> |
||
| 255 | */ |
||
| 256 | public function getSessionCoursesByStatusInUserSubscription(User $user, Session $session, int $relationType, ?AccessUrl $url = null): array |
||
| 257 | { |
||
| 258 | $qb = $this->getEntityManager()->createQueryBuilder(); |
||
| 259 | |||
| 260 | $qb->select('src') |
||
| 261 | ->from(SessionRelCourse::class, 'src') |
||
| 262 | ->innerJoin( |
||
| 263 | SessionRelUser::class, |
||
| 264 | 'sru', |
||
| 265 | Join::WITH, |
||
| 266 | 'src.session = sru.session' |
||
| 267 | ) |
||
| 268 | ->innerJoin('src.session', 'session') |
||
| 269 | ->where( |
||
| 270 | $qb->expr()->eq('session', ':session') |
||
| 271 | ) |
||
| 272 | ->andWhere( |
||
| 273 | $qb->expr()->eq('sru.user', ':user') |
||
| 274 | ) |
||
| 275 | ->andWhere( |
||
| 276 | $qb->expr()->eq('sru.relationType', ':relation_type') |
||
| 277 | ) |
||
| 278 | ; |
||
| 279 | |||
| 280 | $parameters = [ |
||
| 281 | 'session' => $session, |
||
| 282 | 'user' => $user, |
||
| 283 | 'relation_type' => $relationType, |
||
| 284 | ]; |
||
| 285 | |||
| 286 | if ($url) { |
||
| 287 | $qb->innerJoin('session.urls', 'urls') |
||
| 288 | ->andWhere( |
||
| 289 | $qb->expr()->eq('urls.url', ':url') |
||
| 290 | ) |
||
| 291 | ; |
||
| 292 | |||
| 293 | $parameters['url'] = $url; |
||
| 294 | } |
||
| 295 | |||
| 296 | $qb->setParameters($parameters); |
||
| 297 | |||
| 298 | return $qb->getQuery()->getResult(); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return array<SessionRelCourse> |
||
| 303 | */ |
||
| 304 | public function getSessionCoursesByStatusInCourseSubscription(User $user, Session $session, int $status, ?AccessUrl $url = null): array |
||
| 305 | { |
||
| 306 | $qb = $this->getEntityManager()->createQueryBuilder(); |
||
| 307 | |||
| 308 | $qb->select('src') |
||
| 309 | ->from(SessionRelCourse::class, 'src') |
||
| 310 | ->innerJoin( |
||
| 311 | SessionRelCourseRelUser::class, |
||
| 312 | 'srcru', |
||
| 313 | Join::WITH, |
||
| 314 | 'src.session = srcru.session AND src.course = srcru.course' |
||
| 315 | ) |
||
| 316 | ->innerJoin('srcru.session', 'session') |
||
| 317 | ->where( |
||
| 318 | $qb->expr()->eq('session', ':session') |
||
| 319 | ) |
||
| 320 | ->andWhere( |
||
| 321 | $qb->expr()->eq('srcru.user', ':user') |
||
| 322 | ) |
||
| 323 | ->andWhere( |
||
| 324 | $qb->expr()->eq('srcru.status', ':status') |
||
| 325 | ) |
||
| 326 | ; |
||
| 327 | |||
| 328 | $parameters = [ |
||
| 329 | 'session' => $session, |
||
| 330 | 'user' => $user, |
||
| 331 | 'status' => $status, |
||
| 332 | ]; |
||
| 333 | |||
| 334 | if ($url) { |
||
| 335 | $qb->innerJoin('session.urls', 'urls') |
||
| 336 | ->andWhere( |
||
| 337 | $qb->expr()->eq('urls.url', ':url') |
||
| 338 | ) |
||
| 339 | ; |
||
| 340 | |||
| 341 | $parameters['url'] = $url; |
||
| 342 | } |
||
| 343 | |||
| 344 | $qb->setParameters($parameters); |
||
| 345 | |||
| 346 | return $qb->getQuery()->getResult(); |
||
| 347 | } |
||
| 348 | |||
| 349 | private function addSessionRelUserFilterByUrl(Session $session, AccessUrl $url): QueryBuilder |
||
| 365 | } |
||
| 366 | |||
| 367 | public function getUserFollowedSessionsInAccessUrl(User $user, AccessUrl $url): QueryBuilder |
||
| 368 | { |
||
| 369 | $callback = fn (Session $session) => $session->getId(); |
||
| 370 | |||
| 371 | if ($user->isHRM()) { |
||
| 372 | $idList = array_map($callback, $user->getDRHSessions()); |
||
| 373 | } elseif ($user->isTeacher() || COURSEMANAGER === $user->getStatus()) { |
||
| 374 | $idListAsCoach = $user |
||
| 375 | ->getSessionsByStatusInCourseSubscription(Session::COURSE_COACH) |
||
| 376 | ->map($callback) |
||
| 377 | ->getValues() |
||
| 378 | ; |
||
| 379 | $idListAsGeneralCoach = array_map($callback, $user->getSessionsAsGeneralCoach()); |
||
| 380 | $idList = array_merge($idListAsCoach, $idListAsGeneralCoach); |
||
| 381 | } elseif ($user->isSessionAdmin()) { |
||
| 382 | $idList = array_map($callback, $user->getSessionsAsAdmin()); |
||
| 383 | } else { |
||
| 384 | $idList = array_map($callback, $user->getSessionsAsStudent()); |
||
| 385 | } |
||
| 386 | |||
| 387 | $qb = $this->createQueryBuilder('s'); |
||
| 388 | $qb |
||
| 389 | ->innerJoin('s.urls', 'u') |
||
| 390 | ->where($qb->expr()->eq('u.url', $url->getId())) |
||
| 391 | ->andWhere($qb->expr()->in('s.id', ':id_list')) |
||
| 392 | ->setParameter('id_list', $idList) |
||
| 393 | ; |
||
| 394 | |||
| 395 | return $qb; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return array<int, Session> |
||
| 400 | * |
||
| 401 | * @throws Exception |
||
| 402 | */ |
||
| 403 | public function getSubscribedSessionOfUserInUrl( |
||
| 437 | } |
||
| 438 | } |
||
| 439 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.