| Conditions | 13 |
| Paths | 420 |
| Total Lines | 201 |
| Code Lines | 149 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 32 | #[Route(path: '/timetable', name: 'admin_settings_timetable')] |
||
| 33 | public function timetable(Request $request, TimetableSettings $timetableSettings, GradeRepositoryInterface $gradeRepository, |
||
| 34 | AppointmentCategoryRepositoryInterface $appointmentCategoryRepository, EnumStringConverter $enumStringConverter, TranslatorInterface $translator): Response { |
||
| 35 | $builder = $this->createFormBuilder(); |
||
| 36 | $builder |
||
| 37 | ->add('days', ChoiceType::class, [ |
||
| 38 | 'label' => 'admin.settings.timetable.days.label', |
||
| 39 | 'help' => 'admin.settings.timetable.days.help', |
||
| 40 | 'data' => $timetableSettings->getDays(), |
||
| 41 | 'choices' => [ |
||
| 42 | 'date.days.0' => 0, |
||
| 43 | 'date.days.1' => 1, |
||
| 44 | 'date.days.2' => 2, |
||
| 45 | 'date.days.3' => 3, |
||
| 46 | 'date.days.4' => 4, |
||
| 47 | 'date.days.5' => 5, |
||
| 48 | 'date.days.6' => 6 |
||
| 49 | ], |
||
| 50 | 'expanded' => true, |
||
| 51 | 'multiple' => true, |
||
| 52 | 'label_attr' => [ |
||
| 53 | 'class' => 'checkbox-custom' |
||
| 54 | ] |
||
| 55 | ]) |
||
| 56 | ->add('lessons', IntegerType::class, [ |
||
| 57 | 'label' => 'admin.settings.timetable.max_lessons.label', |
||
| 58 | 'help' => 'admin.settings.timetable.max_lessons.help', |
||
| 59 | 'constraints' => [ |
||
| 60 | new Type(['type' => 'integer']), |
||
| 61 | new GreaterThanOrEqual(['value' => 0]) |
||
| 62 | ], |
||
| 63 | 'data' => $timetableSettings->getMaxLessons() |
||
| 64 | ]) |
||
| 65 | ->add('categories', ChoiceType::class, [ |
||
| 66 | 'label' => 'admin.settings.timetable.no_school_category.label', |
||
| 67 | 'help' => 'admin.settings.timetable.no_school_category.help', |
||
| 68 | 'choices' => ArrayUtils::createArrayWithKeysAndValues($appointmentCategoryRepository->findAll(), fn(AppointmentCategory $category) => $category->getName(), fn(AppointmentCategory $category) => $category->getId()), |
||
| 69 | 'placeholder' => 'admin.settings.timetable.no_school_category.none', |
||
| 70 | 'required' => false, |
||
| 71 | 'multiple' => true, |
||
| 72 | 'data' => $timetableSettings->getCategoryIds(), |
||
| 73 | 'expanded' => true, |
||
| 74 | 'label_attr' => [ |
||
| 75 | 'class' => 'checkbox-custom' |
||
| 76 | ] |
||
| 77 | ]) |
||
| 78 | ->add('grades_course_names', ChoiceType::class, [ |
||
| 79 | 'label' => 'admin.settings.timetable.grades_course_names.label', |
||
| 80 | 'help' => 'admin.settings.timetable.grades_course_names.help', |
||
| 81 | 'choices' => ArrayUtils::createArrayWithKeysAndValues($gradeRepository->findAll(), fn(Grade $grade) => $grade->getName(), fn(Grade $grade) => $grade->getId()), |
||
| 82 | 'multiple' => true, |
||
| 83 | 'attr' => [ |
||
| 84 | 'data-choice' => 'true' |
||
| 85 | ], |
||
| 86 | 'data' => $timetableSettings->getGradeIdsWithCourseNames() |
||
| 87 | ]) |
||
| 88 | ->add('grades_membership_types', ChoiceType::class, [ |
||
| 89 | 'label' => 'admin.settings.timetable.grades_membership_types.label', |
||
| 90 | 'help' => 'admin.settings.timetable.grades_membership_types.help', |
||
| 91 | 'choices' => ArrayUtils::createArrayWithKeysAndValues($gradeRepository->findAll(), fn(Grade $grade) => $grade->getName(), fn(Grade $grade) => $grade->getId()), |
||
| 92 | 'multiple' => true, |
||
| 93 | 'attr' => [ |
||
| 94 | 'data-choice' => 'true' |
||
| 95 | ], |
||
| 96 | 'data' => $timetableSettings->getGradeIdsWithMembershipTypes() |
||
| 97 | ]); |
||
| 98 | |||
| 99 | $userTypes = UserType::cases(); |
||
| 100 | |||
| 101 | foreach($userTypes as $name => $userType) { |
||
| 102 | $builder |
||
| 103 | ->add(sprintf('start_%s', $name), DateType::class, [ |
||
| 104 | 'label' => 'admin.settings.appointments.start.label', |
||
| 105 | 'label_translation_parameters' => [ |
||
| 106 | '%type%' => $enumStringConverter->convert($userType) |
||
| 107 | ], |
||
| 108 | 'help' => 'admin.settings.appointments.start.help', |
||
| 109 | 'data' => $timetableSettings->getStartDate($userType), |
||
| 110 | 'widget' => 'single_text', |
||
| 111 | 'required' => false |
||
| 112 | ]) |
||
| 113 | ->add(sprintf('end_%s', $name), DateType::class, [ |
||
| 114 | 'label' => 'admin.settings.appointments.end.label', |
||
| 115 | 'label_translation_parameters' => [ |
||
| 116 | '%type%' => $enumStringConverter->convert($userType) |
||
| 117 | ], |
||
| 118 | 'help' => 'admin.settings.appointments.end.help', |
||
| 119 | 'data' => $timetableSettings->getEndDate($userType), |
||
| 120 | 'widget' => 'single_text', |
||
| 121 | 'required' => false |
||
| 122 | ]); |
||
| 123 | } |
||
| 124 | |||
| 125 | for($lesson = 1; $lesson <= $timetableSettings->getMaxLessons(); $lesson++) { |
||
| 126 | $builder |
||
| 127 | ->add(sprintf('lesson_%d_start', $lesson), TimeType::class, [ |
||
| 128 | 'label' => $translator->trans('admin.settings.timetable.lesson.start', [ '%lesson%' => $lesson ]), |
||
| 129 | 'data' => $timetableSettings->getStart($lesson), |
||
| 130 | 'widget' => 'single_text', |
||
| 131 | 'required' => false, |
||
| 132 | 'input' => 'string', |
||
| 133 | 'input_format' => 'H:i' |
||
| 134 | ]) |
||
| 135 | ->add(sprintf('lesson_%d_end', $lesson), TimeType::class, [ |
||
| 136 | 'label' => $translator->trans('admin.settings.timetable.lesson.end', [ '%lesson%' => $lesson ]), |
||
| 137 | 'data' => $timetableSettings->getEnd($lesson), |
||
| 138 | 'widget' => 'single_text', |
||
| 139 | 'required' => false, |
||
| 140 | 'input' => 'string', |
||
| 141 | 'input_format' => 'H:i' |
||
| 142 | ]); |
||
| 143 | |||
| 144 | if($lesson > 1) { |
||
| 145 | $builder |
||
| 146 | ->add(sprintf('lesson_%d_collapsible', $lesson), CheckboxType::class, [ |
||
| 147 | 'label' => $translator->trans('admin.settings.timetable.lesson.collapsible', ['%lesson%' => $lesson]), |
||
| 148 | 'data' => $timetableSettings->isCollapsible($lesson), |
||
| 149 | 'required' => false, |
||
| 150 | 'label_attr' => [ |
||
| 151 | 'class' => 'checkbox-custom' |
||
| 152 | ] |
||
| 153 | ]); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $builder |
||
| 158 | ->add('supervision_label', TextType::class, [ |
||
| 159 | 'label' => 'admin.settings.timetable.supervision.label', |
||
| 160 | 'required' => true, |
||
| 161 | 'data' => $timetableSettings->getSupervisionLabel(), |
||
| 162 | ]) |
||
| 163 | ->add('supervision_begin', TimeType::class, [ |
||
| 164 | 'label' => 'admin.settings.timetable.supervision.start', |
||
| 165 | 'data' => $timetableSettings->getStart(0), |
||
| 166 | 'widget' => 'single_text', |
||
| 167 | 'input' => 'string' |
||
| 168 | ]) |
||
| 169 | ->add('supervision_color', ColorType::class, [ |
||
| 170 | 'label' => 'admin.settings.timetable.supervision.color', |
||
| 171 | 'data' => $timetableSettings->getSupervisionColor(), |
||
| 172 | 'required' => false |
||
| 173 | ]); |
||
| 174 | |||
| 175 | for($lesson = 1; $lesson <= $timetableSettings->getMaxLessons(); $lesson++) { |
||
| 176 | $builder |
||
| 177 | ->add(sprintf('supervision_label_before_%d', $lesson), TextType::class, [ |
||
| 178 | 'label' => $translator->trans('admin.settings.timetable.supervision.label_before.label', [ '%lesson%' => $lesson ]), |
||
| 179 | 'help' => $translator->trans('admin.settings.timetable.supervision.label_before.help', [ '%lesson%' => $lesson ]), |
||
| 180 | 'data' => $timetableSettings->getDescriptionBeforeLesson($lesson), |
||
| 181 | 'required' => false |
||
| 182 | ]); |
||
| 183 | } |
||
| 184 | |||
| 185 | $form = $builder->getForm(); |
||
| 186 | $form->handleRequest($request); |
||
| 187 | |||
| 188 | if($form->isSubmitted() && $form->isValid()) { |
||
| 189 | $timetableSettings->setMaxLessons($form->get('lessons')->getData()); |
||
| 190 | $timetableSettings->setCategoryIds($form->get('categories')->getData()); |
||
| 191 | $timetableSettings->setSupervisionLabel($form->get('supervision_label')->getData()); |
||
| 192 | $timetableSettings->setSupervisionColor($form->get('supervision_color')->getData()); |
||
| 193 | $timetableSettings->setStart(0, $form->get('supervision_begin')->getData()); |
||
| 194 | $timetableSettings->setGradeIdsWithCourseNames($form->get('grades_course_names')->getData()); |
||
| 195 | $timetableSettings->setGradeIdsWithMembershipTypes($form->get('grades_membership_types')->getData()); |
||
| 196 | |||
| 197 | foreach($userTypes as $name => $userType) { |
||
| 198 | $timetableSettings->setStartDate($userType, $form->get(sprintf('start_%s', $name))->getData()); |
||
| 199 | $timetableSettings->setEndDate($userType, $form->get(sprintf('end_%s', $name))->getData()); |
||
| 200 | } |
||
| 201 | |||
| 202 | for($lesson = 1; $lesson <= $timetableSettings->getMaxLessons(); $lesson++) { |
||
| 203 | $startKey = sprintf('lesson_%d_start', $lesson); |
||
| 204 | $endKey = sprintf('lesson_%d_end', $lesson); |
||
| 205 | $collapsibleKey = sprintf('lesson_%d_collapsible', $lesson); |
||
| 206 | $supervisionKey = sprintf('supervision_label_before_%d', $lesson); |
||
| 207 | |||
| 208 | if($form->has($startKey)) { |
||
| 209 | $timetableSettings->setStart($lesson, $form->get($startKey)->getData()); |
||
| 210 | } |
||
| 211 | |||
| 212 | if($form->has($endKey)) { |
||
| 213 | $timetableSettings->setEnd($lesson, $form->get($endKey)->getData()); |
||
| 214 | } |
||
| 215 | |||
| 216 | if($form->has($collapsibleKey)) { |
||
| 217 | $timetableSettings->setCollapsible($lesson, $form->get($collapsibleKey)->getData()); |
||
| 218 | } |
||
| 219 | |||
| 220 | if($form->has($supervisionKey)) { |
||
| 221 | $timetableSettings->setDescriptionBeforeLesson($lesson, $form->get($supervisionKey)->getData()); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->addFlash('success', 'admin.settings.success'); |
||
| 226 | return $this->redirectToRoute('admin_settings_timetable'); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $this->render('admin/settings/timetable.html.twig', [ |
||
| 230 | 'form' => $form->createView(), |
||
| 231 | 'maxLessons' => $timetableSettings->getMaxLessons(), |
||
| 232 | 'userTypes' => $userTypes |
||
| 233 | ]); |
||
| 235 | } |