| Total Complexity | 40 |
| Total Lines | 261 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ReinscriptionCheckCommand 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 ReinscriptionCheckCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ReinscriptionCheckCommand extends Command |
||
| 22 | { |
||
| 23 | protected static $defaultName = 'app:reinscription-check'; |
||
| 24 | |||
| 25 | private SessionRepository $sessionRepository; |
||
| 26 | private GradebookCertificateRepository $certificateRepository; |
||
| 27 | private EntityManagerInterface $entityManager; |
||
| 28 | |||
| 29 | public function __construct( |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function configure(): void |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Retrieves all users associated with the session. |
||
| 130 | */ |
||
| 131 | private function getUsersForSession(Session $session): array |
||
| 132 | { |
||
| 133 | $usersToNotify = []; |
||
| 134 | $sessionCourses = $this->entityManager->getRepository(SessionRelCourse::class)->findBy(['session' => $session]); |
||
| 135 | |||
| 136 | foreach ($sessionCourses as $courseRel) { |
||
| 137 | $course = $courseRel->getCourse(); |
||
| 138 | |||
| 139 | $studentSubscriptions = $session->getSessionRelCourseRelUsersByStatus($course, Session::STUDENT); |
||
| 140 | foreach ($studentSubscriptions as $studentSubscription) { |
||
| 141 | $usersToNotify[$studentSubscription->getUser()->getId()] = $studentSubscription->getUser(); |
||
| 142 | } |
||
| 143 | |||
| 144 | $coachSubscriptions = $session->getSessionRelCourseRelUsersByStatus($course, Session::COURSE_COACH); |
||
| 145 | foreach ($coachSubscriptions as $coachSubscription) { |
||
| 146 | $usersToNotify[$coachSubscription->getUser()->getId()] = $coachSubscription->getUser(); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $generalCoaches = $session->getGeneralCoaches(); |
||
| 151 | foreach ($generalCoaches as $generalCoach) { |
||
| 152 | $usersToNotify[$generalCoach->getId()] = $generalCoach; |
||
| 153 | } |
||
| 154 | |||
| 155 | return array_values($usersToNotify); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Checks if the user is already enrolled in a valid child session. |
||
| 160 | */ |
||
| 161 | private function isUserAlreadyEnrolledInChildSession($user, $parentSession): bool |
||
| 162 | { |
||
| 163 | $childSessions = $this->sessionRepository->findChildSessions($parentSession); |
||
| 164 | |||
| 165 | foreach ($childSessions as $childSession) { |
||
| 166 | if ($this->findUserSubscriptionInSession($user, $childSession)) { |
||
| 167 | return true; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | return false; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the user's certificates for the courses in the session. |
||
| 176 | */ |
||
| 177 | private function getUserCertificatesForSession($user, Session $session): array |
||
| 178 | { |
||
| 179 | $courses = $this->entityManager->getRepository(SessionRelCourse::class) |
||
| 180 | ->findBy(['session' => $session]); |
||
| 181 | |||
| 182 | $courseIds = array_map(fn($rel) => $rel->getCourse()->getId(), $courses); |
||
| 183 | |||
| 184 | return $this->certificateRepository->createQueryBuilder('gc') |
||
| 185 | ->join('gc.category', 'cat') |
||
| 186 | ->where('gc.user = :user') |
||
| 187 | ->andWhere('cat.course IN (:courses)') |
||
| 188 | ->setParameter('user', $user) |
||
| 189 | ->setParameter('courses', $courseIds) |
||
| 190 | ->getQuery() |
||
| 191 | ->getResult(); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Checks if the user has validated all gradebooks in the session. |
||
| 196 | */ |
||
| 197 | private function hasUserValidatedAllGradebooks(Session $session, array $certificates): bool |
||
| 198 | { |
||
| 199 | $courses = $this->entityManager->getRepository(SessionRelCourse::class) |
||
| 200 | ->findBy(['session' => $session]); |
||
| 201 | |||
| 202 | return count($certificates) === count($courses); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns the latest certificate creation date. |
||
| 207 | */ |
||
| 208 | private function getLatestCertificateDate(array $certificates): ?\DateTime |
||
| 209 | { |
||
| 210 | $dates = array_map(fn($cert) => $cert->getCreatedAt(), $certificates); |
||
| 211 | |||
| 212 | if (empty($dates)) { |
||
| 213 | return null; |
||
| 214 | } |
||
| 215 | |||
| 216 | return max($dates); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Enrolls the user in a new session and updates the previous session subscription. |
||
| 221 | */ |
||
| 222 | private function enrollUserInSession($user, $newSession, $oldSession): void |
||
| 223 | { |
||
| 224 | $existingSubscription = $this->findUserSubscriptionInSession($user, $newSession); |
||
| 225 | |||
| 226 | if (!$existingSubscription) { |
||
| 227 | $newSession->addUserInSession(Session::STUDENT, $user); |
||
| 228 | |||
| 229 | $subscription = $this->findUserSubscriptionInSession($user, $oldSession); |
||
| 230 | if ($subscription) { |
||
| 231 | $subscription->setNewSubscriptionSessionId($newSession->getId()); |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->entityManager->persist($newSession); |
||
| 235 | $this->entityManager->flush(); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Determines if the user has already been reinscribed. |
||
| 241 | */ |
||
| 242 | private function isUserReinscribed($user, Session $session): bool |
||
| 243 | { |
||
| 244 | $subscription = $this->findUserSubscriptionInSession($user, $session); |
||
| 245 | return $subscription && $subscription->getNewSubscriptionSessionId() !== null; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Finds the user's subscription in the specified session. |
||
| 250 | */ |
||
| 251 | private function findUserSubscriptionInSession($user, $session) |
||
| 257 | ]); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Finds a valid session within the session hierarchy. |
||
| 262 | */ |
||
| 263 | private function findValidSessionInHierarchy(Session $session): ?Session |
||
| 282 | } |
||
| 283 | } |
||
| 284 |