| Total Complexity | 66 |
| Total Lines | 537 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ExportDataCommand 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 ExportDataCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | #[AsCommand('app:export', description: 'Exportiert alle importierten Daten, sodass diese (z.B. auf einem anderen Server importiert werden können).')] |
||
| 66 | class ExportDataCommand extends Command { |
||
| 67 | |||
| 68 | public function __construct(private readonly KernelInterface $kernel, private readonly EntityManagerInterface $em, private readonly SerializerInterface $serializer, string $name = null) { |
||
| 69 | parent::__construct($name); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function execute(InputInterface $input, OutputInterface $output): int { |
||
| 73 | $io = new SymfonyStyle($input, $output); |
||
| 74 | |||
| 75 | $this->exportStudents($io); |
||
| 76 | $this->exportTeachers($io); |
||
| 77 | $this->exportSubjects($io); |
||
| 78 | $this->exportRooms($io); |
||
| 79 | $this->exportPrivacyCategories($io); |
||
| 80 | $this->exportGrades($io); |
||
| 81 | $this->exportGradeMemberships($io); |
||
| 82 | $this->exportGradeTeachers($io); |
||
| 83 | $this->exportStudyGroups($io); |
||
| 84 | $this->exportStudyGroupMemberships($io); |
||
| 85 | $this->exportTuitions($io); |
||
| 86 | $this->exportExams($io); |
||
| 87 | $this->exportAppointmentCategories($io); |
||
| 88 | $this->exportAppointments($io); |
||
| 89 | $this->exportTimetable($io); |
||
| 90 | $this->exportSupervisions($io); |
||
| 91 | |||
| 92 | $io->success('Export erfolgreich - bitte das Verzeichnis export/ prüfen'); |
||
| 93 | |||
| 94 | return Command::SUCCESS; |
||
| 95 | } |
||
| 96 | |||
| 97 | private function writeFile(string $filename, $object): void { |
||
| 98 | $json = $this->serializer->serialize($object, 'json'); |
||
| 99 | $target = sprintf('%s/%s', $this->kernel->getProjectDir(), 'export'); |
||
| 100 | if(!is_dir($target)) { |
||
| 101 | mkdir($target); |
||
| 102 | $handle = fopen(sprintf('%s/.gitignore', $target), 'w'); |
||
| 103 | fwrite($handle, '*'); |
||
| 104 | fclose($handle); |
||
| 105 | } |
||
| 106 | |||
| 107 | $handle = fopen(sprintf('%s/%s', $target, $filename), 'w'); |
||
| 108 | fwrite($handle, $json); |
||
| 109 | fclose($handle); |
||
| 110 | } |
||
| 111 | |||
| 112 | private function getSectionKey(Section $section): string { |
||
| 113 | return sprintf('%d_%d', $section->getYear(), $section->getNumber()); |
||
| 114 | } |
||
| 115 | |||
| 116 | private function exportStudents(SymfonyStyle $io): void { |
||
| 117 | $io->section('Exportiere Lernende'); |
||
| 118 | |||
| 119 | $students = [ ]; |
||
| 120 | |||
| 121 | /** @var Section[] $sections */ |
||
| 122 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 123 | |||
| 124 | /** @var Student $student */ |
||
| 125 | foreach($this->em->getRepository(Student::class)->findAll() as $student) { |
||
| 126 | foreach($student->getSections() as $section) { |
||
| 127 | $key = $this->getSectionKey($section); |
||
| 128 | |||
| 129 | if(!isset($students[$key])) { |
||
| 130 | $students[$key] = [ ]; |
||
| 131 | } |
||
| 132 | |||
| 133 | $students[$key][] = (new StudentData()) |
||
| 134 | ->setId($student->getExternalId()) |
||
| 135 | ->setFirstname($student->getFirstname()) |
||
| 136 | ->setLastname($student->getLastname()) |
||
| 137 | ->setEmail($student->getEmail()) |
||
| 138 | ->setBirthday($student->getBirthday()) |
||
| 139 | ->setGender($student->getGender()->value) |
||
| 140 | ->setStatus($student->getStatus()); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | foreach($sections as $section) { |
||
| 145 | $data = $students[$this->getSectionKey($section)] ?? [ ]; |
||
| 146 | |||
| 147 | $this->writeFile( |
||
| 148 | sprintf('students_%s.json', $this->getSectionKey($section)), |
||
| 149 | (new StudentsData())->setSection($section->getNumber())->setYear($section->getYear())->setStudents($data) |
||
| 150 | ); |
||
| 151 | |||
| 152 | $io->success(sprintf('%d Lernende exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | private function exportPrivacyCategories(SymfonyStyle $io): void { |
||
| 173 | } |
||
| 174 | |||
| 175 | private function exportTeachers(SymfonyStyle $io): void { |
||
| 176 | $io->section('Exportiere Lehrkräfte'); |
||
| 177 | |||
| 178 | $teachers = [ ]; |
||
| 179 | |||
| 180 | /** @var Section[] $sections */ |
||
| 181 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 182 | |||
| 183 | /** @var Teacher $teacher */ |
||
| 184 | foreach($this->em->getRepository(Teacher::class)->findAll() as $teacher) { |
||
| 185 | foreach($teacher->getSections() as $section) { |
||
| 186 | $key = $this->getSectionKey($section); |
||
| 187 | |||
| 188 | if (!isset($teachers[$key])) { |
||
| 189 | $teachers[$key] = []; |
||
| 190 | } |
||
| 191 | |||
| 192 | $teachers[$key][] = (new TeacherData()) |
||
| 193 | ->setId($teacher->getExternalId()) |
||
| 194 | ->setAcronym($teacher->getAcronym()) |
||
| 195 | ->setFirstname($teacher->getFirstname()) |
||
| 196 | ->setLastname($teacher->getLastname()) |
||
| 197 | ->setEmail($teacher->getEmail()) |
||
| 198 | ->setGender($teacher->getGender()->value) |
||
| 199 | ->setTitle($teacher->getTitle()) |
||
| 200 | ->setSubjects($teacher->getSubjects()->map(fn(Subject $subject) => $subject->getExternalId())->toArray()); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | foreach($sections as $section) { |
||
| 205 | $data = $teachers[$this->getSectionKey($section)] ?? [ ]; |
||
| 206 | |||
| 207 | $this->writeFile( |
||
| 208 | sprintf('teachers_%s.json', $this->getSectionKey($section)), |
||
| 209 | (new TeachersData())->setSection($section->getNumber())->setYear($section->getYear())->setTeachers($data) |
||
| 210 | ); |
||
| 211 | |||
| 212 | $io->success(sprintf('%d Lehrkräfte exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | private function exportSubjects(SymfonyStyle $io): void { |
||
| 217 | $io->section('Exportiere Fächer'); |
||
| 218 | $subjects = [ ]; |
||
| 219 | |||
| 220 | /** @var Subject $subject */ |
||
| 221 | foreach($this->em->getRepository(Subject::class)->findAll() as $subject) { |
||
| 222 | if($subject->getExternalId() === null) { |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | |||
| 226 | $subjects[] = (new SubjectData()) |
||
| 227 | ->setId($subject->getExternalId()) |
||
| 228 | ->setAbbreviation($subject->getAbbreviation()) |
||
| 229 | ->setName($subject->getName()); |
||
| 230 | } |
||
| 231 | |||
| 232 | $data = (new SubjectsData())->setSubjects($subjects); |
||
| 233 | $this->writeFile('subjects.json', $data); |
||
| 234 | |||
| 235 | $io->success(sprintf('%d Fächer exportiert', count($subjects))); |
||
| 236 | } |
||
| 237 | |||
| 238 | private function exportRooms(SymfonyStyle $io): void { |
||
| 239 | $io->section('Exportiere Räume'); |
||
| 240 | $rooms = [ ]; |
||
| 241 | |||
| 242 | /** @var Room $room */ |
||
| 243 | foreach($this->em->getRepository(Room::class)->findAll() as $room) { |
||
| 244 | $rooms[] = (new RoomData()) |
||
| 245 | ->setId($room->getExternalId()) |
||
| 246 | ->setName($room->getName()) |
||
| 247 | ->setCapacity($room->getCapacity()) |
||
| 248 | ->setDescription($room->getDescription()); |
||
| 249 | } |
||
| 250 | |||
| 251 | $data = (new RoomsData())->setRooms($rooms); |
||
| 252 | $this->writeFile('rooms.json', $data); |
||
| 253 | |||
| 254 | $io->success(sprintf('%d Räume exportiert', count($rooms))); |
||
| 255 | } |
||
| 256 | |||
| 257 | private function exportGrades(SymfonyStyle $io): void { |
||
| 273 | } |
||
| 274 | |||
| 275 | private function exportGradeMemberships(SymfonyStyle $io): void { |
||
| 276 | $io->section('Exportiere Klassenmitgliedschaften'); |
||
| 277 | |||
| 278 | $memberships = [ ]; |
||
| 279 | |||
| 280 | /** @var Section[] $sections */ |
||
| 281 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 282 | |||
| 283 | /** @var GradeMembership $membership */ |
||
| 284 | foreach($this->em->getRepository(GradeMembership::class)->findAll() as $membership) { |
||
| 285 | $key = $this->getSectionKey($membership->getSection()); |
||
| 286 | |||
| 287 | if(!isset($memberships[$key])) { |
||
| 288 | $memberships[$key] = [ ]; |
||
| 289 | } |
||
| 290 | |||
| 291 | $memberships[$key][] = (new GradeMembershipData()) |
||
| 292 | ->setGrade($membership->getGrade()->getExternalId()) |
||
| 293 | ->setStudent($membership->getStudent()->getExternalId()); |
||
| 294 | } |
||
| 295 | |||
| 296 | foreach($sections as $section) { |
||
| 297 | $data = $memberships[$this->getSectionKey($section)] ?? [ ]; |
||
| 298 | |||
| 299 | $this->writeFile( |
||
| 300 | sprintf('grade_memberships_%s.json', $this->getSectionKey($section)), |
||
| 301 | (new GradeMembershipsData())->setSection($section->getNumber())->setYear($section->getYear())->setMemberships($data) |
||
| 302 | ); |
||
| 303 | |||
| 304 | $io->success(sprintf('%d Klassenmitgliedschaften exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | private function exportGradeTeachers(SymfonyStyle $io): void { |
||
| 309 | $io->section('Exportiere Klassenleitungen'); |
||
| 310 | |||
| 311 | $gradeTeachers = [ ]; |
||
| 312 | |||
| 313 | /** @var Section[] $sections */ |
||
| 314 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 315 | |||
| 316 | /** @var GradeTeacher $gradeTeacher */ |
||
| 317 | foreach($this->em->getRepository(GradeTeacher::class)->findAll() as $gradeTeacher) { |
||
| 318 | $key = $this->getSectionKey($gradeTeacher->getSection()); |
||
| 319 | |||
| 320 | if(!isset($gradeTeachers[$key])) { |
||
| 321 | $gradeTeachers[$key] = [ ]; |
||
| 322 | } |
||
| 323 | |||
| 324 | $gradeTeachers[$key][] = (new GradeTeacherData()) |
||
| 325 | ->setGrade($gradeTeacher->getGrade()->getExternalId()) |
||
| 326 | ->setTeacher($gradeTeacher->getTeacher()->getExternalId()) |
||
| 327 | ->setType($gradeTeacher->getType()->value); |
||
| 328 | } |
||
| 329 | |||
| 330 | foreach($sections as $section) { |
||
| 331 | $data = $gradeTeachers[$this->getSectionKey($section)] ?? [ ]; |
||
| 332 | |||
| 333 | $this->writeFile( |
||
| 334 | sprintf('grade_teachers%s.json', $this->getSectionKey($section)), |
||
| 335 | (new GradeTeachersData())->setSection($section->getNumber())->setYear($section->getYear())->setGradeTeachers($data) |
||
| 336 | ); |
||
| 337 | |||
| 338 | $io->success(sprintf('%d Klassenleitungen exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | private function exportStudyGroups(SymfonyStyle $io): void { |
||
| 343 | $io->section('Exportiere Lerngruppen'); |
||
| 344 | |||
| 345 | $studyGroups = [ ]; |
||
| 346 | |||
| 347 | /** @var Section[] $sections */ |
||
| 348 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 349 | |||
| 350 | /** @var StudyGroup $studyGroup */ |
||
| 351 | foreach($this->em->getRepository(StudyGroup::class)->findAll() as $studyGroup) { |
||
| 352 | $key = $this->getSectionKey($studyGroup->getSection()); |
||
| 353 | |||
| 354 | if(!isset($studyGroups[$key])) { |
||
| 355 | $studyGroups[$key] = [ ]; |
||
| 356 | } |
||
| 357 | |||
| 358 | $studyGroups[$key][] = (new StudyGroupData()) |
||
| 359 | ->setId($studyGroup->getExternalId()) |
||
| 360 | ->setName($studyGroup->getName()) |
||
| 361 | ->setType($studyGroup->getType()->value) |
||
| 362 | ->setGrades($studyGroup->getGrades()->map(fn(Grade $grade) => $grade->getExternalId())->toArray()); |
||
| 363 | } |
||
| 364 | |||
| 365 | foreach($sections as $section) { |
||
| 366 | $data = $studyGroups[$this->getSectionKey($section)] ?? [ ]; |
||
| 367 | |||
| 368 | $this->writeFile( |
||
| 369 | sprintf('studygroups_%s.json', $this->getSectionKey($section)), |
||
| 370 | (new StudyGroupsData())->setSection($section->getNumber())->setYear($section->getYear())->setStudyGroups($data) |
||
| 371 | ); |
||
| 372 | |||
| 373 | $io->success(sprintf('%d Lerngruppen exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | private function exportStudyGroupMemberships(SymfonyStyle $io): void { |
||
| 378 | $io->section('Exportiere Lerngruppenmitgliedschaften'); |
||
| 379 | |||
| 380 | $memberships = [ ]; |
||
| 381 | |||
| 382 | /** @var Section[] $sections */ |
||
| 383 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 384 | |||
| 385 | /** @var StudyGroupMembership $membership */ |
||
| 386 | foreach($this->em->getRepository(StudyGroupMembership::class)->findAll() as $membership) { |
||
| 387 | $key = $this->getSectionKey($membership->getStudyGroup()->getSection()); |
||
| 388 | |||
| 389 | if(!isset($memberships[$key])) { |
||
| 390 | $memberships[$key] = [ ]; |
||
| 391 | } |
||
| 392 | |||
| 393 | $memberships[$key][] = (new StudyGroupMembershipData()) |
||
| 394 | ->setStudent($membership->getStudent()->getExternalId()) |
||
| 395 | ->setStudyGroup($membership->getStudyGroup()->getExternalId()) |
||
| 396 | ->setType($membership->getType()); |
||
| 397 | } |
||
| 398 | |||
| 399 | foreach($sections as $section) { |
||
| 400 | $data = $memberships[$this->getSectionKey($section)] ?? [ ]; |
||
| 401 | |||
| 402 | $this->writeFile( |
||
| 403 | sprintf('studygroup_memberships_%s.json', $this->getSectionKey($section)), |
||
| 404 | (new StudyGroupMembershipsData())->setSection($section->getNumber())->setYear($section->getYear())->setMemberships($data) |
||
| 405 | ); |
||
| 406 | |||
| 407 | $io->success(sprintf('%d Lerngruppenmitgliedschaften exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | private function exportTuitions(SymfonyStyle $io): void { |
||
| 412 | $io->section('Exportiere Unterrichte'); |
||
| 413 | |||
| 414 | $tuitions = [ ]; |
||
| 415 | |||
| 416 | /** @var Section[] $sections */ |
||
| 417 | $sections = $this->em->getRepository(Section::class)->findAll(); |
||
| 418 | |||
| 419 | /** @var Tuition $tuition */ |
||
| 420 | foreach($this->em->getRepository(Tuition::class)->findAll() as $tuition) { |
||
| 421 | $key = $this->getSectionKey($tuition->getSection()); |
||
| 422 | |||
| 423 | if(!isset($tuitions[$key])) { |
||
| 424 | $tuitions[$key] = [ ]; |
||
| 425 | } |
||
| 426 | |||
| 427 | $tuitions[$key][] = (new TuitionData()) |
||
| 428 | ->setId($tuition->getExternalId()) |
||
| 429 | ->setName($tuition->getName()) |
||
| 430 | ->setDisplayName($tuition->getDisplayName()) |
||
| 431 | ->setSubject($tuition->getSubject()->getExternalId()) |
||
| 432 | ->setStudyGroup($tuition->getStudyGroup()->getExternalId()) |
||
| 433 | ->setTeachers($tuition->getTeachers()->map(fn(Teacher $teacher) => $teacher->getExternalId())->toArray()); |
||
| 434 | } |
||
| 435 | |||
| 436 | foreach($sections as $section) { |
||
| 437 | $data = $tuitions[$this->getSectionKey($section)] ?? [ ]; |
||
| 438 | |||
| 439 | $this->writeFile( |
||
| 440 | sprintf('tuitions_%s.json', $this->getSectionKey($section)), |
||
| 441 | (new TuitionsData())->setSection($section->getNumber())->setYear($section->getYear())->setTuitions($data) |
||
| 442 | ); |
||
| 443 | |||
| 444 | $io->success(sprintf('%d Unterrichte exportiert (%s)', count($data), $section->getDisplayName())); |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | private function exportExams(SymfonyStyle $io): void { |
||
| 494 | } |
||
| 495 | |||
| 496 | private function exportAppointmentCategories(SymfonyStyle $io): void { |
||
| 497 | $io->section('Exportiere Terminkategorien'); |
||
| 498 | |||
| 499 | $categories = [ ]; |
||
| 500 | |||
| 501 | /** @var AppointmentCategory $category */ |
||
| 502 | foreach($this->em->getRepository(AppointmentCategory::class)->findAll() as $category) { |
||
| 503 | $categories[] = (new AppointmentCategoryData()) |
||
| 504 | ->setId($category->getExternalId()) |
||
| 505 | ->setColor($category->getColor()) |
||
| 506 | ->setName($category->getName()); |
||
| 507 | } |
||
| 508 | |||
| 509 | $data = (new AppointmentCategoriesData())->setCategories($categories); |
||
| 510 | $this->writeFile('appointment_categories.json', $data); |
||
| 511 | |||
| 512 | $io->success(sprintf('%d Terminkategorien exportiert', count($categories))); |
||
| 513 | } |
||
| 514 | |||
| 515 | private function exportAppointments(SymfonyStyle $io): void { |
||
| 516 | $io->section('Exportiere Termine'); |
||
| 517 | $appointments = [ ]; |
||
| 518 | |||
| 519 | /** @var Appointment $appointment */ |
||
| 520 | foreach($this->em->getRepository(Appointment::class)->findAll() as $appointment) { |
||
| 521 | if($appointment->getExternalId() === null) { |
||
| 522 | continue; |
||
| 523 | } |
||
| 524 | |||
| 525 | $appointments[] = (new AppointmentData()) |
||
| 526 | ->setId($appointment->getExternalId()) |
||
| 527 | ->setStart($appointment->getStart()) |
||
| 528 | ->setEnd($appointment->getEnd()) |
||
| 529 | ->setContent($appointment->getContent()) |
||
| 530 | ->setIsAllDay($appointment->isAllDay()) |
||
| 531 | ->setLocation($appointment->getLocation()) |
||
| 532 | ->setSubject($appointment->getTitle()) |
||
| 533 | ->setCategory($appointment->getCategory()->getExternalId()) |
||
| 534 | ->setExternalOrganizers($appointment->getExternalOrganizers()) |
||
| 535 | ->setOrganizers($appointment->getOrganizers()->map(fn(Teacher $teacher) => $teacher->getExternalId())->toArray()) |
||
| 536 | ->setStudyGroups($appointment->getStudyGroups()->map(fn(StudyGroup $studyGroup) => $studyGroup->getExternalId())->toArray()) |
||
| 537 | ->setVisibilities($appointment->getVisibilities()->map(fn(UserTypeEntity $type) => $type->getUserType()->value)->toArray()); |
||
| 538 | } |
||
| 539 | |||
| 540 | $data = (new AppointmentsData())->setAppointments($appointments); |
||
| 541 | $this->writeFile('appointment.json', $data); |
||
| 542 | |||
| 543 | $io->success(sprintf('%d Termine exportiert', count($appointments))); |
||
| 544 | } |
||
| 545 | |||
| 546 | private function exportTimetable(SymfonyStyle $io): void { |
||
| 573 | } |
||
| 574 | } |
||
| 575 | |||
| 576 | private function exportSupervisions(SymfonyStyle $io): void { |
||
| 602 | } |
||
| 603 | } |
||
| 604 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths