| Total Complexity | 44 |
| Total Lines | 204 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ReservationCheckerEventSubscriber 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 ReservationCheckerEventSubscriber, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class ReservationCheckerEventSubscriber implements EventSubscriberInterface { |
||
| 33 | |||
| 34 | public function __construct(private readonly ValidatorInterface $validator, private readonly ResourceReservationRepositoryInterface $reservationRepository, |
||
| 35 | private readonly ExamRepositoryInterface $examRepository, private readonly TranslatorInterface $translator, |
||
| 36 | private readonly DateHelper $dateHelper, private readonly ResourceAvailabilityHelper $availabilityHelper, |
||
| 37 | private readonly UserRepositoryInterface $userRepository, private readonly NotificationService $notificationService, |
||
| 38 | private readonly UrlGeneratorInterface $urlGenerator) |
||
| 39 | { |
||
| 40 | } |
||
| 41 | |||
| 42 | public function onSubstitutionImportEvent(SubstitutionImportEvent $event): void { |
||
| 43 | $substitutions = array_merge($event->getAdded(), $event->getUpdated()); |
||
| 44 | /** @var DateTime $start */ |
||
| 45 | $start = null; |
||
| 46 | |||
| 47 | /** @var DateTime $end */ |
||
| 48 | $end = null; |
||
| 49 | |||
| 50 | /** @var Substitution $substitution */ |
||
| 51 | foreach($substitutions as $substitution) { |
||
| 52 | if($start === null || $substitution->getDate() < $start) { |
||
| 53 | $start = clone $substitution->getDate(); |
||
| 54 | } |
||
| 55 | |||
| 56 | if($end === null || $substitution->getDate() > $end) { |
||
| 57 | $end = clone $substitution->getDate(); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if($start === null || $end === null) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | $today = $this->dateHelper->getToday(); |
||
| 66 | |||
| 67 | while($start <= $end) { |
||
| 68 | $reservations = $this->reservationRepository->findAllByDate($start); |
||
| 69 | |||
| 70 | foreach($reservations as $reservation) { |
||
| 71 | if($reservation->getDate() >= $today) { |
||
| 72 | $violations = $this->validator->validate($reservation); |
||
| 73 | |||
| 74 | if (count($violations) > 0 && $this->handleViolation($reservation) === false) { |
||
| 75 | $this->sendViolationsEmail($reservation, $violations); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | $exams = $this->examRepository->findAllByDate($start); |
||
| 81 | |||
| 82 | foreach($exams as $exam) { |
||
| 83 | if(!empty($exam->getExternalId())) { |
||
| 84 | continue; // Disable check for external exams |
||
| 85 | } |
||
| 86 | |||
| 87 | $violations = $this->validator->validate($exam); |
||
| 88 | |||
| 89 | |||
| 90 | $reservationViolations = [ ]; |
||
| 91 | |||
| 92 | foreach($violations as $violation) { |
||
| 93 | if($violation instanceof ConstraintViolation && $violation->getConstraint() instanceof NoReservationCollision) { |
||
| 94 | $reservationViolations[] = $violation; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if(count($reservationViolations) > 0) { |
||
| 99 | $this->sendExamViolationsEmail($exam, $reservationViolations); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | $start->modify('+1 day'); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | private function handleViolation(ResourceReservation $reservation): bool { |
||
| 140 | } |
||
| 141 | |||
| 142 | private function sendReservationRemovedEmail(ResourceReservation $reservation): void { |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | private function sendViolationsEmail(ResourceReservation $reservation, ConstraintViolationListInterface $violationList): void { |
||
| 169 | if($reservation->getTeacher() === null) { |
||
| 170 | return; |
||
| 171 | } |
||
| 172 | |||
| 173 | foreach($this->userRepository->findAllTeachers([$reservation->getTeacher()]) as $recipient) { |
||
| 174 | $notification = new Notification( |
||
| 175 | $recipient, |
||
| 176 | $this->translator->trans('reservation.title', [], 'email'), |
||
| 177 | $this->translator->trans('reservation.content', [ |
||
| 178 | '%room%' => $reservation->getResource()->getName(), |
||
| 179 | '%date%' => $reservation->getDate()->format($this->translator->trans('date.format_short')), |
||
| 180 | '%lesson%' => $this->translator->trans('label.substitution_lessons', [ |
||
| 181 | '%start%' => $reservation->getLessonStart(), |
||
| 182 | '%end%' => $reservation->getLessonEnd(), |
||
| 183 | '%count%' => ($reservation->getLessonEnd() - $reservation->getLessonStart()) |
||
| 184 | ]) |
||
| 185 | ], 'email'), |
||
| 186 | $this->urlGenerator->generate('edit_room_reservation', ['uuid' => $reservation->getUuid()->toString()], UrlGeneratorInterface::ABSOLUTE_URL), |
||
| 187 | $this->translator->trans('reservation.link', [], 'email') |
||
| 188 | ); |
||
| 189 | |||
| 190 | $this->notificationService->notify($notification); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param ConstraintViolationInterface[] $violationList |
||
| 196 | */ |
||
| 197 | private function sendExamViolationsEmail(Exam $exam, array $violationList): void { |
||
| 198 | $teachers = [ ]; |
||
| 199 | |||
| 200 | /** @var Tuition $tuition */ |
||
| 201 | foreach($exam->getTuitions() as $tuition) { |
||
| 202 | foreach($tuition->getTeachers() as $teacher) { |
||
| 203 | if(!in_array($teacher, $teachers)) { |
||
| 204 | $teachers[] = $teacher; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | foreach($this->userRepository->findAllTeachers($teachers) as $recipient) { |
||
| 210 | $notification = new Notification( |
||
| 211 | $recipient, |
||
| 212 | $this->translator->trans('reservation.title', [], 'email'), |
||
| 213 | $this->translator->trans('reservation.content_exam', [ |
||
| 214 | '%room%' => $exam->getRoom()?->getName(), |
||
| 215 | '%date%' => $exam->getDate()->format($this->translator->trans('date.format_short')), |
||
| 216 | '%lesson%' => $this->translator->trans('label.substitution_lessons', [ |
||
| 217 | '%start%' => $exam->getLessonStart(), |
||
| 218 | '%end%' => $exam->getLessonEnd(), |
||
| 219 | '%count%' => ($exam->getLessonEnd() - $exam->getLessonStart()) |
||
| 220 | ]) |
||
| 221 | ], 'email'), |
||
| 222 | $this->urlGenerator->generate('edit_exam', ['uuid' => $exam->getUuid()->toString()], UrlGeneratorInterface::ABSOLUTE_URL), |
||
| 223 | $this->translator->trans('reservation.link', [], 'email') |
||
| 224 | ); |
||
| 225 | |||
| 226 | $this->notificationService->notify($notification); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @inheritDoc |
||
| 232 | */ |
||
| 233 | public static function getSubscribedEvents(): array { |
||
| 236 | ]; |
||
| 237 | } |
||
| 238 | } |