| Total Complexity | 58 |
| Total Lines | 352 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BookExporter 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 BookExporter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class BookExporter { |
||
| 34 | |||
| 35 | public function __construct(private readonly EntryOverviewHelper $overviewHelper, private readonly StudentInfoResolver $studentInfoResolver, |
||
| 36 | private readonly Sorter $sorter, private readonly SerializerInterface $serializer, |
||
| 37 | private readonly GradeOverviewHelper $gradeOverviewHelper, private readonly TuitionRepositoryInterface $tuitionRepository) |
||
| 38 | { |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param StudentEntity[] $students |
||
| 43 | */ |
||
| 44 | private function export(Book $book, array $students, ?TuitionEntity $tuition, ?GradeEntity $grade, SectionEntity $section, EntryOverview $overview): Book { |
||
| 45 | $book |
||
| 46 | ->setStart($section->getStart()) |
||
|
|
|||
| 47 | ->setEnd($section->getEnd()) |
||
| 48 | ->setSection($this->castSection($section)); |
||
| 49 | |||
| 50 | if($book->getTuition() !== null && $tuition !== null) { |
||
| 51 | /** @var TeacherEntity $teacher */ |
||
| 52 | foreach ($tuition->getTeachers() as $teacher) { |
||
| 53 | $book->getTuition()->addTeacher( |
||
| 54 | (new Teacher()) |
||
| 55 | ->setAcronym($teacher->getAcronym()) |
||
| 56 | ->setId($teacher->getUuid()->toString()) |
||
| 57 | ->setFirstname($teacher->getFirstname()) |
||
| 58 | ->setLastname($teacher->getLastname()) |
||
| 59 | ->setTitle($teacher->getTitle()) |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | if($tuition !== null) { |
||
| 65 | $grades = []; |
||
| 66 | |||
| 67 | foreach ($tuition->getStudyGroup()->getGrades() as $tuitionGrade) { |
||
| 68 | $exportGrade = (new Grade()) |
||
| 69 | ->setName($tuitionGrade->getName()); |
||
| 70 | |||
| 71 | /** @var GradeTeacher $teacher */ |
||
| 72 | foreach ($tuitionGrade->getTeachers() as $teacher) { |
||
| 73 | $exportGrade->addTeacher( |
||
| 74 | (new Teacher()) |
||
| 75 | ->setAcronym($teacher->getTeacher()->getAcronym()) |
||
| 76 | ->setId($teacher->getTeacher()->getUuid()->toString()) |
||
| 77 | ->setFirstname($teacher->getTeacher()->getFirstname()) |
||
| 78 | ->setLastname($teacher->getTeacher()->getLastname()) |
||
| 79 | ->setTitle($teacher->getTeacher()->getTitle()) |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | $grades[] = $exportGrade; |
||
| 84 | } |
||
| 85 | $book->setGrades($grades); |
||
| 86 | } |
||
| 87 | |||
| 88 | $gradeOverview = null; |
||
| 89 | $tuitions = [ ]; |
||
| 90 | |||
| 91 | if($tuition !== null) { |
||
| 92 | $tuitions = [ $tuition ]; |
||
| 93 | $gradeOverview = $this->gradeOverviewHelper->computeOverviewForTuition($tuition); |
||
| 94 | } else if($grade !== null) { |
||
| 95 | $gradeOverview = $this->gradeOverviewHelper->computeForGrade($grade, $section); |
||
| 96 | foreach($gradeOverview->getCategories() as $category) { |
||
| 97 | $tuitions[$category->getTuition()->getId()] = $category->getTuition(); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if($gradeOverview !== null) { |
||
| 102 | foreach($tuitions as $gradeTuition) { |
||
| 103 | $studentGrades = new StudentGrades(); |
||
| 104 | $studentGrades->setTuition($this->castTuition($gradeTuition)); |
||
| 105 | |||
| 106 | foreach ($gradeOverview->getCategories() as $category) { |
||
| 107 | if($category->getTuition() !== $gradeTuition) { |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($category->getCategory()->isExportable()) { |
||
| 112 | $studentGrades->addCategory( |
||
| 113 | (new TuitionGradeCategory()) |
||
| 114 | ->setUuid($category->getCategory()->getUuid()->toString()) |
||
| 115 | ->setDisplayName($category->getCategory()->getDisplayName()) |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach ($gradeOverview->getRows() as $row) { |
||
| 121 | foreach ($gradeOverview->getCategories() as $category) { |
||
| 122 | if($category->getTuition() !== $gradeTuition) { |
||
| 123 | continue; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($category->getCategory()->isExportable()) { |
||
| 127 | $studentGrades->addGrade((new TuitionGrade()) |
||
| 128 | ->setStudent($row->getTuitionOrStudent()->getExternalId()) |
||
| 129 | ->setGradeCategory($category->getCategory()->getUuid()->toString()) |
||
| 130 | ->setEncryptedGrade($row->getGrade($category->getTuition(), $category->getCategory())?->getEncryptedGrade())); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | $book->addStudentGrades($studentGrades); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->sorter->sort($students, StudentStrategy::class); |
||
| 140 | |||
| 141 | $studentInfo = [ ]; |
||
| 142 | $tuitionsToConsider = $tuition !== null ? [ $tuition ] : $this->tuitionRepository->findAllByGrades([$grade], $section); |
||
| 143 | |||
| 144 | foreach($students as $student) { |
||
| 145 | $info = $this->studentInfoResolver->resolveStudentInfo($student, $section, $tuitionsToConsider); |
||
| 146 | $studentInfo[$student->getId()] = $info; |
||
| 147 | $book->addStudentSummary( |
||
| 148 | (new StudentSummary()) |
||
| 149 | ->setStudent($this->castStudent($student, $section)) |
||
| 150 | ->setLateMinutesCount($info->getLateMinutesCount()) |
||
| 151 | ->setAbsentLessonsCount($info->getAbsentLessonsCount()) |
||
| 152 | ->setExcuseStatusNotSetLessonCount($info->getNotExcusedOrNotSetLessonsCount()) |
||
| 153 | ->setNotExcusedAbsentLessonCount($info->getNotExcusedAbsentLessonsCount()) |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | |||
| 157 | $weeks = [ ]; |
||
| 158 | foreach($overview->getDays() as $day) { |
||
| 159 | $weekNumber = (int)$day->getDate()->format('W'); |
||
| 160 | |||
| 161 | if(!isset($weeks[$weekNumber])) { |
||
| 162 | $weeks[$weekNumber] = (new Week()) |
||
| 163 | ->setStart(clone $day->getDate()) |
||
| 164 | ->setEnd((clone $day->getDate())->modify('+6 days')) |
||
| 165 | ->setWeekNumber($weekNumber); |
||
| 166 | } |
||
| 167 | |||
| 168 | $exportDay = (new Day()) |
||
| 169 | ->setDate(clone $day->getDate()); |
||
| 170 | |||
| 171 | foreach($overview->getComments($day->getDate()) as $comment) { |
||
| 172 | $exportDay->addComment($this->castComment($comment, $section)); |
||
| 173 | } |
||
| 174 | |||
| 175 | $lessons = [ ]; |
||
| 176 | |||
| 177 | foreach($day->getLessons() as $lesson) { |
||
| 178 | if($lesson->getEntry() === null) { |
||
| 179 | $lessons[] = $this->castLesson($lesson->getLesson(), $lesson->getLessonNumber()); |
||
| 180 | } else { |
||
| 181 | $lessons[$lesson->getEntry()->getUuid()->toString()] = $this->castEntry($lesson->getEntry(), $section, $studentInfo); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | foreach($lessons as $lesson) { |
||
| 186 | $exportDay->addLesson($lesson); |
||
| 187 | } |
||
| 188 | |||
| 189 | $weeks[$weekNumber]->addDay($exportDay); |
||
| 190 | } |
||
| 191 | |||
| 192 | foreach($weeks as $week) { |
||
| 193 | $book->addWeek($week); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $book; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function exportGrade(GradeEntity $grade, SectionEntity $section): Book { |
||
| 200 | $book = (new Book()) |
||
| 201 | ->setGrades([$this->castGrade($grade, $section)]); |
||
| 202 | |||
| 203 | $students = $grade->getMemberships()->filter(fn(GradeMembership $membership) => $membership->getSection() === $section)->map(fn(GradeMembership $membership) => $membership->getStudent())->toArray(); |
||
| 204 | $overview = $this->overviewHelper->computeOverviewForGrade($grade, $section->getStart(), $section->getEnd()); |
||
| 205 | |||
| 206 | return $this->export($book, $students, null, $grade, $section, $overview); |
||
| 207 | } |
||
| 208 | |||
| 209 | public function exportTuition(TuitionEntity $tuition, SectionEntity $section): Book { |
||
| 210 | $book = (new Book()) |
||
| 211 | ->setTuition($this->castTuition($tuition)); |
||
| 212 | |||
| 213 | $students = $tuition->getStudyGroup()->getMemberships()->map(fn(StudyGroupMembership $membership) => $membership->getStudent())->toArray(); |
||
| 214 | |||
| 215 | $overview = $this->overviewHelper->computeOverviewForTuition($tuition, $section->getStart(), $section->getEnd()); |
||
| 216 | |||
| 217 | return $this->export($book, $students, $tuition, null, $section, $overview); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function exportGradeXml(GradeEntity $grade, SectionEntity $section): string { |
||
| 221 | return $this->serializer->serialize( |
||
| 222 | $this->exportGrade($grade, $section), |
||
| 223 | 'xml' |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function exportGradeJson(GradeEntity $grade, SectionEntity $section): string { |
||
| 228 | return $this->serializer->serialize( |
||
| 229 | $this->exportGrade($grade, $section), |
||
| 230 | 'json' |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function exportTuitionXml(TuitionEntity $tuition, SectionEntity $section): string { |
||
| 235 | return $this->serializer->serialize( |
||
| 236 | $this->exportTuition($tuition, $section), |
||
| 237 | 'xml' |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function exportTuitionJson(TuitionEntity $tuition, SectionEntity $section): string { |
||
| 242 | return $this->serializer->serialize( |
||
| 243 | $this->exportTuition($tuition, $section), |
||
| 244 | 'json' |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | |||
| 248 | private function castLesson(TimetableLesson $lessonEntity, int $lessonNumber): Lesson { |
||
| 249 | $subject = $lessonEntity->getTuition()->getSubject(); |
||
| 250 | |||
| 251 | $lesson = (new Lesson()) |
||
| 252 | ->setIsMissing(true) |
||
| 253 | ->setStart($lessonNumber) |
||
| 254 | ->setEnd($lessonNumber) |
||
| 255 | ->setSubject($subject !== null ? $subject->getAbbreviation() : null) |
||
| 256 | ->setTeacher($this->castTeacher($lessonEntity->getTuition()->getTeachers()->first())); |
||
| 257 | |||
| 258 | return $lesson; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param StudentInfo[] $studentInfo |
||
| 263 | */ |
||
| 264 | private function castEntry(LessonEntryEntity $entry, SectionEntity $section, array $studentInfo): Lesson { |
||
| 265 | $subject = $entry->getTuition()->getSubject(); |
||
| 266 | |||
| 267 | $lesson = (new Lesson()) |
||
| 268 | ->setStart($entry->getLessonStart()) |
||
| 269 | ->setEnd($entry->getLessonEnd()) |
||
| 270 | ->setSubject($subject !== null ? $subject->getAbbreviation() : null) |
||
| 271 | ->setReplacementSubject($entry->getReplacementSubject()) |
||
| 272 | ->setTeacher($this->castTeacher($entry->getTeacher())) |
||
| 273 | ->setReplacementTeacher($this->castTeacher($entry->getReplacementTeacher())) |
||
| 274 | ->setWasCancelled($entry->isCancelled()) |
||
| 275 | ->setTopic($entry->isCancelled() ? $entry->getCancelReason() : $entry->getTopic()) |
||
| 276 | ->setComment($entry->getComment()) |
||
| 277 | ->setExercises($entry->getExercises()) |
||
| 278 | ->setIsMissing(false); |
||
| 279 | |||
| 280 | /** @var LessonAttendanceEntity $attendance */ |
||
| 281 | foreach($entry->getAttendances() as $attendance) { |
||
| 282 | $exportAttendance = (new Attendance()) |
||
| 283 | ->setComment($attendance->getComment()) |
||
| 284 | ->setLateMinutesCount($attendance->getLateMinutes()) |
||
| 285 | ->setStudent($this->castStudent($attendance->getStudent(), $section)) |
||
| 286 | ->setType($this->castAttendanceType($attendance->getType())); |
||
| 287 | |||
| 288 | // check if lesson is excused |
||
| 289 | if($attendance->getType() === LessonAttendanceType::Absent) { |
||
| 290 | $exportAttendance->setAbsentLessonCount( |
||
| 291 | ($entry->getLessonEnd() - $attendance->getAbsentLessons()) < $entry->getLessonStart() ? 1 : 0 |
||
| 292 | ); |
||
| 293 | |||
| 294 | // check info |
||
| 295 | $info = $studentInfo[$attendance->getStudent()->getId()] ?? null; |
||
| 296 | |||
| 297 | if($info !== null) { |
||
| 298 | /** @var LessonAttendance $possibleAttendance */ |
||
| 299 | $possibleAttendance = ArrayUtils::first($info->getAbsentLessonAttendances(), fn(LessonAttendance $lessonAttendance) => $lessonAttendance->getAttendance()->getId() === $attendance->getId() |
||
| 300 | && $lessonAttendance->getAttendance()->getEntry() === $entry); |
||
| 301 | |||
| 302 | if($possibleAttendance !== null) { |
||
| 303 | $exportAttendance->setIsExcused($possibleAttendance->isExcused()); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $lesson->addAttendance($exportAttendance); |
||
| 309 | } |
||
| 310 | |||
| 311 | return $lesson; |
||
| 312 | } |
||
| 313 | |||
| 314 | private function castAttendanceType(int $type): string |
||
| 315 | { |
||
| 316 | return match ($type) { |
||
| 317 | LessonAttendanceType::Absent => 'absent', |
||
| 318 | LessonAttendanceType::Present => 'present', |
||
| 319 | LessonAttendanceType::Late => 'late', |
||
| 320 | default => throw new InvalidArgumentException(sprintf('$type must be either 0, 1 or 2, %d given', $type)), |
||
| 321 | }; |
||
| 322 | } |
||
| 323 | |||
| 324 | private function castSection(SectionEntity $section): Section { |
||
| 325 | return (new Section()) |
||
| 326 | ->setName($section->getDisplayName()) |
||
| 327 | ->setYear($section->getYear()) |
||
| 328 | ->setNumber($section->getNumber()); |
||
| 329 | } |
||
| 330 | |||
| 331 | private function castGrade(GradeEntity $gradeEntity, SectionEntity $section): Grade { |
||
| 332 | $grade = (new Grade()) |
||
| 333 | ->setName($gradeEntity->getName()); |
||
| 334 | |||
| 335 | /** @var GradeTeacher $gradeTeacher */ |
||
| 336 | foreach($gradeEntity->getTeachers() as $gradeTeacher) { |
||
| 337 | if($gradeTeacher->getSection()->getId() === $section->getId()) { |
||
| 338 | $grade->addTeacher($this->castTeacher($gradeTeacher->getTeacher())); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | return $grade; |
||
| 343 | } |
||
| 344 | |||
| 345 | private function castComment(CommentEntity $comment, SectionEntity $section): Comment { |
||
| 346 | return (new Comment()) |
||
| 347 | ->setDate($comment->getDate()) |
||
| 348 | ->setTeacher($this->castTeacher($comment->getTeacher())) |
||
| 349 | ->setStudents( |
||
| 350 | $comment->getStudents()->map(fn(StudentEntity $student) => $this->castStudent($student, $section))->toArray() |
||
| 351 | ) |
||
| 352 | ->setComment($comment->getText()); |
||
| 353 | } |
||
| 354 | |||
| 355 | private function castTuition(TuitionEntity $tuition): Tuition { |
||
| 362 | } |
||
| 363 | |||
| 364 | private function castTeacher(?TeacherEntity $teacher): ?Teacher { |
||
| 365 | if($teacher === null) { |
||
| 366 | return null; |
||
| 367 | } |
||
| 368 | |||
| 369 | return (new Teacher()) |
||
| 370 | ->setId($teacher->getExternalId()) |
||
| 371 | ->setAcronym($teacher->getAcronym()) |
||
| 372 | ->setFirstname($teacher->getFirstname()) |
||
| 373 | ->setLastname($teacher->getLastname()) |
||
| 374 | ->setTitle($teacher->getTitle()); |
||
| 375 | } |
||
| 376 | |||
| 377 | private function castStudent(StudentEntity $student, SectionEntity $section): Student { |
||
| 385 | } |
||
| 386 | } |