| Total Complexity | 75 |
| Total Lines | 560 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 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 |
||
| 28 | class SessionRepository extends ServiceEntityRepository |
||
| 29 | { |
||
| 30 | public function __construct( |
||
| 35 | } |
||
| 36 | |||
| 37 | public function create(): ?Session |
||
| 38 | { |
||
| 39 | return new Session(); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function update(Session $session): void |
||
| 43 | { |
||
| 44 | $this->getEntityManager()->persist($session); |
||
| 45 | $this->getEntityManager()->flush(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return array<SessionRelUser> |
||
| 50 | */ |
||
| 51 | public function getUsersByAccessUrl(Session $session, AccessUrl $url, array $relationTypeList = []): array |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getSessionsByUser(User $user, AccessUrl $url): QueryBuilder |
||
| 70 | { |
||
| 71 | $qb = $this->createQueryBuilder('s'); |
||
| 72 | $qb |
||
| 73 | ->innerJoin('s.users', 'sru') |
||
| 74 | ->leftJoin('s.urls', 'urls') |
||
| 75 | ->where($qb->expr()->eq('sru.user', ':user')) |
||
| 76 | ->andWhere($qb->expr()->eq('urls.url', ':url')) |
||
| 77 | ->setParameters([ |
||
| 78 | 'user' => $user, |
||
| 79 | 'url' => $url, |
||
| 80 | ]) |
||
| 81 | ->orderBy('s.displayStartDate', 'ASC') |
||
| 82 | ->addOrderBy('s.title', 'ASC') |
||
| 83 | ->addOrderBy('s.position', 'ASC') |
||
| 84 | ; |
||
| 85 | |||
| 86 | return $qb; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array<int, Session> |
||
| 91 | * |
||
| 92 | * @throws Exception |
||
| 93 | */ |
||
| 94 | public function getPastSessionsOfUserInUrl(User $user, AccessUrl $url): array |
||
| 95 | { |
||
| 96 | $sessions = $this->getSubscribedSessionsOfUserInUrl($user, $url); |
||
| 97 | |||
| 98 | $filterPastSessions = function (Session $session) use ($user) { |
||
| 99 | $now = new DateTime(); |
||
| 100 | // Determine if the user is a coach |
||
| 101 | $userIsCoach = $session->hasCoach($user); |
||
| 102 | |||
| 103 | // Check if the session has a duration |
||
| 104 | if ($session->getDuration() > 0) { |
||
| 105 | $daysLeft = $session->getDaysLeftByUser($user); |
||
| 106 | $session->setTitle($session->getTitle().'<-'.$daysLeft); |
||
| 107 | |||
| 108 | return $daysLeft < 0 && !$userIsCoach; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Get the appropriate end date based on whether the user is a coach |
||
| 112 | $sessionEndDate = $userIsCoach && $session->getCoachAccessEndDate() |
||
| 113 | ? $session->getCoachAccessEndDate() |
||
| 114 | : $session->getAccessEndDate(); |
||
| 115 | |||
| 116 | // If there's no end date, the session is not considered past |
||
| 117 | if (!$sessionEndDate) { |
||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 121 | // Check if the current date is after the end date |
||
| 122 | return $now > $sessionEndDate; |
||
| 123 | }; |
||
| 124 | |||
| 125 | return array_filter($sessions, $filterPastSessions); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return array<int, Session> |
||
| 130 | * |
||
| 131 | * @throws Exception |
||
| 132 | */ |
||
| 133 | public function getCurrentSessionsOfUserInUrl(User $user, AccessUrl $url): array |
||
| 134 | { |
||
| 135 | $sessions = $this->getSubscribedSessionsOfUserInUrl($user, $url); |
||
| 136 | |||
| 137 | $filterCurrentSessions = function (Session $session) use ($user, $url) { |
||
| 138 | $userIsGeneralCoach = $session->hasUserAsGeneralCoach($user); |
||
| 139 | if (!$userIsGeneralCoach) { |
||
| 140 | $coursesAsCoach = $this->getSessionCoursesByStatusInCourseSubscription($user, $session, Session::COURSE_COACH, $url); |
||
| 141 | $coursesAsStudent = $this->getSessionCoursesByStatusInCourseSubscription($user, $session, Session::STUDENT, $url); |
||
| 142 | $validCourses = array_merge($coursesAsCoach, $coursesAsStudent); |
||
| 143 | |||
| 144 | if (empty($validCourses)) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | $session->setCourses(new ArrayCollection($validCourses)); |
||
| 148 | } |
||
| 149 | |||
| 150 | $userIsCoach = $session->hasCoach($user); |
||
| 151 | |||
| 152 | // Check if session has a duration |
||
| 153 | if ($session->getDuration() > 0) { |
||
| 154 | $daysLeft = $session->getDaysLeftByUser($user); |
||
| 155 | |||
| 156 | return $daysLeft >= 0 || $userIsCoach; |
||
| 157 | } |
||
| 158 | |||
| 159 | // Determine the start date based on whether the user is a coach |
||
| 160 | $sessionStartDate = $userIsCoach && $session->getCoachAccessStartDate() |
||
| 161 | ? $session->getCoachAccessStartDate() |
||
| 162 | : $session->getAccessStartDate(); |
||
| 163 | |||
| 164 | // If there is no start date, consider the session current |
||
| 165 | if (!$sessionStartDate) { |
||
| 166 | return true; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Get the current date and time |
||
| 170 | $now = new DateTime(); |
||
| 171 | |||
| 172 | // Determine the end date based on whether the user is a coach |
||
| 173 | $sessionEndDate = $userIsCoach && $session->getCoachAccessEndDate() |
||
| 174 | ? $session->getCoachAccessEndDate() |
||
| 175 | : $session->getAccessEndDate(); |
||
| 176 | |||
| 177 | // Check if the current date is within the start and end dates |
||
| 178 | return $now >= $sessionStartDate && (!$sessionEndDate || $now <= $sessionEndDate); |
||
| 179 | }; |
||
| 180 | |||
| 181 | return array_filter($sessions, $filterCurrentSessions); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return array<int, Session> |
||
| 186 | * |
||
| 187 | * @throws Exception |
||
| 188 | */ |
||
| 189 | public function getUpcomingSessionsOfUserInUrl(User $user, AccessUrl $url): array |
||
| 190 | { |
||
| 191 | $sessions = $this->getSubscribedSessionsOfUserInUrl($user, $url); |
||
| 192 | |||
| 193 | $filterUpcomingSessions = function (Session $session) use ($user) { |
||
| 194 | $now = new DateTime(); |
||
| 195 | |||
| 196 | // All session with access by duration call be either current or past |
||
| 197 | if ($session->getDuration() > 0) { |
||
| 198 | return false; |
||
| 199 | } |
||
| 200 | |||
| 201 | // Determine if the user is a coach |
||
| 202 | $userIsCoach = $session->hasCoach($user); |
||
| 203 | |||
| 204 | // Get the appropriate start date based on whether the user is a coach |
||
| 205 | $sessionStartDate = $userIsCoach && $session->getCoachAccessStartDate() |
||
| 206 | ? $session->getCoachAccessStartDate() |
||
| 207 | : $session->getAccessStartDate(); |
||
| 208 | |||
| 209 | // If there's no start date, the session is not considered future |
||
| 210 | if (!$sessionStartDate) { |
||
| 211 | return false; |
||
| 212 | } |
||
| 213 | |||
| 214 | // Check if the current date is before the start date |
||
| 215 | return $now < $sessionStartDate; |
||
| 216 | }; |
||
| 217 | |||
| 218 | return array_filter($sessions, $filterUpcomingSessions); |
||
| 219 | } |
||
| 220 | |||
| 221 | public function addUserInCourse(int $relationType, User $user, Course $course, Session $session): void |
||
| 222 | { |
||
| 223 | if (!$user->isActive()) { |
||
| 224 | throw new Exception('User not active'); |
||
| 225 | } |
||
| 226 | |||
| 227 | if (!$session->hasCourse($course)) { |
||
| 228 | $msg = \sprintf('Course %s is not subscribed to the session %s', $course->getTitle(), $session->getTitle()); |
||
| 229 | |||
| 230 | throw new Exception($msg); |
||
| 231 | } |
||
| 232 | |||
| 233 | if (!\in_array($relationType, Session::getRelationTypeList(), true)) { |
||
| 234 | throw new Exception(\sprintf('Cannot handle relationType %s', $relationType)); |
||
| 235 | } |
||
| 236 | |||
| 237 | $entityManager = $this->getEntityManager(); |
||
| 238 | $existingRecord = $entityManager->getRepository(SessionRelUser::class)->findOneBy([ |
||
| 239 | 'session' => $session, |
||
| 240 | 'user' => $user, |
||
| 241 | 'relationType' => $relationType, |
||
| 242 | ]); |
||
| 243 | |||
| 244 | if ($existingRecord) { |
||
| 245 | $entityManager->remove($existingRecord); |
||
| 246 | $entityManager->flush(); |
||
| 247 | } |
||
| 248 | |||
| 249 | switch ($relationType) { |
||
| 250 | case Session::DRH: |
||
| 251 | if ($user->hasRole('ROLE_HR')) { |
||
| 252 | $session->addUserInSession(Session::DRH, $user); |
||
| 253 | } |
||
| 254 | |||
| 255 | break; |
||
| 256 | |||
| 257 | case Session::STUDENT: |
||
| 258 | $session |
||
| 259 | ->addUserInSession(Session::STUDENT, $user) |
||
| 260 | ->addUserInCourse(Session::STUDENT, $user, $course) |
||
| 261 | ; |
||
| 262 | |||
| 263 | break; |
||
| 264 | |||
| 265 | case Session::COURSE_COACH: |
||
| 266 | if ($user->hasRole('ROLE_TEACHER')) { |
||
| 267 | $session |
||
| 268 | ->addUserInSession(Session::COURSE_COACH, $user) |
||
| 269 | ->addUserInCourse( |
||
| 270 | Session::COURSE_COACH, |
||
| 271 | $user, |
||
| 272 | $course |
||
| 273 | ) |
||
| 274 | ; |
||
| 275 | } |
||
| 276 | |||
| 277 | break; |
||
| 278 | } |
||
| 279 | |||
| 280 | $entityManager->persist($session); |
||
| 281 | $entityManager->flush(); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return array<SessionRelCourse> |
||
| 286 | */ |
||
| 287 | public function getSessionCoursesByStatusInUserSubscription(User $user, Session $session, int $relationType, ?AccessUrl $url = null): array |
||
| 288 | { |
||
| 289 | $qb = $this->getEntityManager()->createQueryBuilder(); |
||
| 290 | |||
| 291 | $qb->select('src') |
||
| 292 | ->from(SessionRelCourse::class, 'src') |
||
| 293 | ->innerJoin( |
||
| 294 | SessionRelUser::class, |
||
| 295 | 'sru', |
||
| 296 | Join::WITH, |
||
| 297 | 'src.session = sru.session' |
||
| 298 | ) |
||
| 299 | ->innerJoin('src.session', 'session') |
||
| 300 | ->where( |
||
| 301 | $qb->expr()->eq('session', ':session') |
||
| 302 | ) |
||
| 303 | ->andWhere( |
||
| 304 | $qb->expr()->eq('sru.user', ':user') |
||
| 305 | ) |
||
| 306 | ->andWhere( |
||
| 307 | $qb->expr()->eq('sru.relationType', ':relation_type') |
||
| 308 | ) |
||
| 309 | ; |
||
| 310 | |||
| 311 | $parameters = [ |
||
| 312 | 'session' => $session, |
||
| 313 | 'user' => $user, |
||
| 314 | 'relation_type' => $relationType, |
||
| 315 | ]; |
||
| 316 | |||
| 317 | if ($url) { |
||
| 318 | $qb->innerJoin('session.urls', 'urls') |
||
| 319 | ->andWhere( |
||
| 320 | $qb->expr()->eq('urls.url', ':url') |
||
| 321 | ) |
||
| 322 | ; |
||
| 323 | |||
| 324 | $parameters['url'] = $url; |
||
| 325 | } |
||
| 326 | |||
| 327 | $qb->setParameters($parameters); |
||
| 328 | |||
| 329 | return $qb->getQuery()->getResult(); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return array<SessionRelCourse> |
||
| 334 | */ |
||
| 335 | public function getSessionCoursesByStatusInCourseSubscription(User $user, Session $session, int $status, ?AccessUrl $url = null): array |
||
| 336 | { |
||
| 337 | $qb = $this->getEntityManager()->createQueryBuilder(); |
||
| 338 | |||
| 339 | $qb->select('src') |
||
| 340 | ->from(SessionRelCourse::class, 'src') |
||
| 341 | ->innerJoin( |
||
| 342 | SessionRelCourseRelUser::class, |
||
| 343 | 'srcru', |
||
| 344 | Join::WITH, |
||
| 345 | 'src.session = srcru.session AND src.course = srcru.course' |
||
| 346 | ) |
||
| 347 | ->innerJoin('srcru.session', 'session') |
||
| 348 | ->where( |
||
| 349 | $qb->expr()->eq('session', ':session') |
||
| 350 | ) |
||
| 351 | ->andWhere( |
||
| 352 | $qb->expr()->eq('srcru.user', ':user') |
||
| 353 | ) |
||
| 354 | ->andWhere( |
||
| 355 | $qb->expr()->eq('srcru.status', ':status') |
||
| 356 | ) |
||
| 357 | ; |
||
| 358 | |||
| 359 | $parameters = [ |
||
| 360 | 'session' => $session, |
||
| 361 | 'user' => $user, |
||
| 362 | 'status' => $status, |
||
| 363 | ]; |
||
| 364 | |||
| 365 | if ($url) { |
||
| 366 | $qb->innerJoin('session.urls', 'urls') |
||
| 367 | ->andWhere( |
||
| 368 | $qb->expr()->eq('urls.url', ':url') |
||
| 369 | ) |
||
| 370 | ; |
||
| 371 | |||
| 372 | $parameters['url'] = $url; |
||
| 373 | } |
||
| 374 | |||
| 375 | $qb->setParameters($parameters); |
||
| 376 | |||
| 377 | return $qb->getQuery()->getResult(); |
||
| 378 | } |
||
| 379 | |||
| 380 | private function addSessionRelUserFilterByUrl(Session $session, AccessUrl $url): QueryBuilder |
||
| 396 | } |
||
| 397 | |||
| 398 | public function getUserFollowedSessionsInAccessUrl(User $user, AccessUrl $url): QueryBuilder |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return array<int, Session> |
||
| 431 | * |
||
| 432 | * @throws Exception |
||
| 433 | */ |
||
| 434 | public function getSubscribedSessionsOfUserInUrl( |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Finds a valid child session based on access dates and reinscription days. |
||
| 472 | * |
||
| 473 | * @param Session $session |
||
| 474 | * @return Session|null |
||
| 475 | */ |
||
| 476 | public function findValidChildSession(Session $session): ?Session |
||
| 477 | { |
||
| 478 | $childSessions = $this->findChildSessions($session); |
||
| 479 | $now = new \DateTime(); |
||
| 480 | |||
| 481 | foreach ($childSessions as $childSession) { |
||
| 482 | $startDate = $childSession->getAccessStartDate(); |
||
| 483 | $endDate = $childSession->getAccessEndDate(); |
||
| 484 | $daysToReinscription = $childSession->getDaysToReinscription(); |
||
| 485 | |||
| 486 | if (empty($daysToReinscription) || $daysToReinscription <= 0) { |
||
| 487 | continue; |
||
| 488 | } |
||
| 489 | |||
| 490 | $adjustedEndDate = (clone $endDate)->modify('-' . $daysToReinscription . ' days'); |
||
| 491 | |||
| 492 | if ($startDate <= $now && $adjustedEndDate >= $now) { |
||
| 493 | return $childSession; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | return null; |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Finds a valid parent session based on access dates and reinscription days. |
||
| 501 | */ |
||
| 502 | public function findValidParentSession(Session $session): ?Session |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Finds child sessions based on the parent session. |
||
| 529 | */ |
||
| 530 | public function findChildSessions(Session $parentSession): array |
||
| 531 | { |
||
| 532 | return $this->createQueryBuilder('s') |
||
| 533 | ->where('s.parentId = :parentId') |
||
| 534 | ->setParameter('parentId', $parentSession->getId()) |
||
| 535 | ->getQuery() |
||
| 536 | ->getResult(); |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Finds the parent session for a given session. |
||
| 541 | */ |
||
| 542 | public function findParentSession(Session $session): ?Session |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Find sessions without child and ready for repetition. |
||
| 553 | * |
||
| 554 | * @return Session[] |
||
| 555 | */ |
||
| 556 | public function findSessionsWithoutChildAndReadyForRepetition() |
||
| 557 | { |
||
| 558 | $currentDate = new \DateTime(); |
||
| 559 | |||
| 560 | $qb = $this->createQueryBuilder('s') |
||
| 561 | ->where('s.daysToNewRepetition IS NOT NULL') |
||
| 562 | ->andWhere('s.lastRepetition = :false') |
||
| 563 | ->andWhere(':currentDate BETWEEN DATE_SUB(s.accessEndDate, s.daysToNewRepetition, \'DAY\') AND s.accessEndDate') |
||
| 564 | ->andWhere('NOT EXISTS ( |
||
| 565 | SELECT 1 |
||
| 566 | FROM Chamilo\CoreBundle\Entity\Session child |
||
| 567 | WHERE child.parentId = s.id |
||
| 568 | AND child.accessEndDate >= :currentDate |
||
| 569 | )') |
||
| 570 | ->setParameter('false', false) |
||
| 571 | ->setParameter('currentDate', $currentDate); |
||
| 572 | |||
| 573 | return $qb->getQuery()->getResult(); |
||
| 574 | } |
||
| 575 | |||
| 576 | public function countUsersBySession(int $sessionId, int $relationType = Session::STUDENT): int |
||
| 588 | } |
||
| 589 | } |
||
| 590 |
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.