Total Complexity | 43 |
Total Lines | 268 |
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 |
||
53 | { |
||
54 | $debug = $input->getOption('debug'); |
||
55 | |||
56 | $sessions = $this->sessionRepository->findAll(); |
||
57 | |||
58 | foreach ($sessions as $session) { |
||
59 | if ($session->getValidityInDays() === null || $session->getValidityInDays() === 0) { |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | $users = $this->getUsersForSession($session); |
||
64 | |||
65 | foreach ($users as $user) { |
||
66 | if ($debug) { |
||
67 | $output->writeln(sprintf('Processing user %d in session %d.', $user->getId(), $session->getId())); |
||
68 | } |
||
69 | |||
70 | if ($this->isUserReinscribed($user, $session)) { |
||
71 | continue; |
||
72 | } |
||
73 | |||
74 | if ($this->isUserAlreadyEnrolledInChildSession($user, $session)) { |
||
75 | if ($debug) { |
||
76 | $output->writeln(sprintf('User %d is already enrolled in a valid child session.', $user->getId())); |
||
77 | } |
||
78 | continue; |
||
79 | } |
||
80 | |||
81 | $certificates = $this->getUserCertificatesForSession($user, $session); |
||
82 | |||
83 | if ($this->hasUserValidatedAllGradebooks($session, $certificates)) { |
||
84 | $latestValidationDate = $this->getLatestCertificateDate($certificates); |
||
85 | |||
86 | if ($latestValidationDate !== null) { |
||
87 | $reinscriptionDate = (clone $latestValidationDate)->modify("+{$session->getValidityInDays()} days"); |
||
88 | |||
89 | if ($debug) { |
||
90 | $output->writeln(sprintf( |
||
91 | 'User %d - Latest certificate date: %s, Reinscription date: %s', |
||
92 | $user->getId(), |
||
93 | $latestValidationDate->format('Y-m-d'), |
||
94 | $reinscriptionDate->format('Y-m-d') |
||
95 | )); |
||
96 | } |
||
97 | |||
98 | if (new \DateTime() >= $reinscriptionDate) { |
||
99 | $validSession = $this->findValidSessionInHierarchy($session); |
||
100 | |||
101 | if ($validSession) { |
||
102 | $this->enrollUserInSession($user, $validSession, $session); |
||
103 | if ($debug) { |
||
104 | $output->writeln(sprintf( |
||
105 | 'User %d re-enrolled into session %d.', |
||
106 | $user->getId(), |
||
107 | $validSession->getId() |
||
108 | )); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | } else { |
||
113 | if ($debug) { |
||
114 | $output->writeln(sprintf( |
||
115 | 'User %d has no valid certificates for session %d.', |
||
116 | $user->getId(), |
||
117 | $session->getId() |
||
118 | )); |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | |||
125 | return Command::SUCCESS; |
||
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 |
||
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 |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Returns the latest certificate creation date. |
||
207 | */ |
||
208 | private function getLatestCertificateDate(array $certificates): ?\DateTime |
||
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 | foreach ($newSession->getCourses() as $sessionRelCourse) { |
||
230 | $course = $sessionRelCourse->getCourse(); |
||
231 | if ($course) { |
||
232 | $newSession->addUserInCourse(Session::STUDENT, $user, $course); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | $subscription = $this->findUserSubscriptionInSession($user, $oldSession); |
||
237 | if ($subscription) { |
||
238 | $subscription->setNewSubscriptionSessionId($newSession->getId()); |
||
239 | } |
||
240 | |||
241 | $this->entityManager->persist($newSession); |
||
242 | $this->entityManager->flush(); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Determines if the user has already been reinscribed. |
||
248 | */ |
||
249 | private function isUserReinscribed($user, Session $session): bool |
||
250 | { |
||
251 | $subscription = $this->findUserSubscriptionInSession($user, $session); |
||
252 | return $subscription && $subscription->getNewSubscriptionSessionId() !== null; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Finds the user's subscription in the specified session. |
||
257 | */ |
||
258 | private function findUserSubscriptionInSession($user, $session) |
||
264 | ]); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Finds a valid session within the session hierarchy. |
||
269 | */ |
||
270 | private function findValidSessionInHierarchy(Session $session): ?Session |
||
291 |