| Total Complexity | 50 |
| Total Lines | 444 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SettingsController 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 SettingsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | #[Route('/admin')] |
||
| 28 | class SettingsController extends BaseController |
||
| 29 | { |
||
| 30 | use ControllerTrait; |
||
| 31 | |||
| 32 | public function __construct( |
||
| 33 | private readonly EntityManagerInterface $entityManager, |
||
| 34 | private readonly TranslatorInterface $translator |
||
| 35 | ) {} |
||
| 36 | |||
| 37 | #[Route('/settings', name: 'admin_settings')] |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Toggle access_url_changeable for a given setting variable. |
||
| 45 | * Only platform admins on the main URL (ID = 1) are allowed to change it, |
||
| 46 | */ |
||
| 47 | #[IsGranted('ROLE_ADMIN')] |
||
| 48 | #[Route('/settings/toggle_changeable', name: 'settings_toggle_changeable', methods: ['POST'])] |
||
| 49 | public function toggleChangeable(Request $request, AccessUrlHelper $accessUrlHelper): JsonResponse |
||
| 50 | { |
||
| 51 | // Security: only admins. |
||
| 52 | if (!$this->isGranted('ROLE_ADMIN')) { |
||
| 53 | return $this->json([ |
||
| 54 | 'error' => 'Only platform admins can modify this flag.', |
||
| 55 | ], 403); |
||
| 56 | } |
||
| 57 | |||
| 58 | $currentUrl = $accessUrlHelper->getCurrent(); |
||
| 59 | $currentUrlId = $currentUrl->getId(); |
||
| 60 | |||
| 61 | // Only main URL (ID = 1) can toggle the flag. |
||
| 62 | if (1 !== $currentUrlId) { |
||
| 63 | return $this->json([ |
||
| 64 | 'error' => 'Only the main URL (ID 1) can toggle this setting.', |
||
| 65 | ], 403); |
||
| 66 | } |
||
| 67 | |||
| 68 | $payload = json_decode($request->getContent(), true); |
||
| 69 | |||
| 70 | if (!\is_array($payload) || !isset($payload['variable'], $payload['status'])) { |
||
| 71 | return $this->json([ |
||
| 72 | 'error' => 'Invalid payload.', |
||
| 73 | ], 400); |
||
| 74 | } |
||
| 75 | |||
| 76 | $variable = (string) $payload['variable']; |
||
| 77 | $status = (int) $payload['status']; |
||
| 78 | |||
| 79 | $repo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 80 | |||
| 81 | // We search by variable + current main AccessUrl entity. |
||
| 82 | $setting = $repo->findOneBy([ |
||
| 83 | 'variable' => $variable, |
||
| 84 | 'url' => $currentUrl, |
||
| 85 | ]); |
||
| 86 | |||
| 87 | if (!$setting) { |
||
| 88 | return $this->json([ |
||
| 89 | 'error' => 'Setting not found.', |
||
| 90 | ], 404); |
||
| 91 | } |
||
| 92 | |||
| 93 | try { |
||
| 94 | $setting->setAccessUrlChangeable($status); |
||
| 95 | $this->entityManager->flush(); |
||
| 96 | |||
| 97 | return $this->json([ |
||
| 98 | 'result' => 1, |
||
| 99 | 'status' => $status, |
||
| 100 | ]); |
||
| 101 | } catch (\Throwable $e) { |
||
| 102 | return $this->json([ |
||
| 103 | 'error' => 'Unable to update setting.', |
||
| 104 | 'details' => $e->getMessage(), |
||
| 105 | ], 500); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Edit configuration with given namespace (search page). |
||
| 111 | */ |
||
| 112 | #[IsGranted('ROLE_ADMIN')] |
||
| 113 | #[Route('/settings/search_settings', name: 'chamilo_platform_settings_search')] |
||
| 114 | public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper): Response |
||
| 115 | { |
||
| 116 | $manager = $this->getSettingsManager(); |
||
| 117 | |||
| 118 | $url = $accessUrlHelper->getCurrent(); |
||
| 119 | $manager->setUrl($url); |
||
| 120 | |||
| 121 | $formList = []; |
||
| 122 | $templateMap = []; |
||
| 123 | $templateMapByCategory = []; |
||
| 124 | $settings = []; |
||
| 125 | |||
| 126 | $keyword = trim((string) $request->query->get('keyword', '')); |
||
| 127 | |||
| 128 | $searchForm = $this->getSearchForm(); |
||
| 129 | $searchForm->handleRequest($request); |
||
| 130 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||
| 131 | $values = $searchForm->getData(); |
||
| 132 | $keyword = trim((string) ($values['keyword'] ?? '')); |
||
| 133 | } |
||
| 134 | |||
| 135 | $schemas = $manager->getSchemas(); |
||
| 136 | [$ordered, $labelMap] = $this->computeOrderedNamespacesByTranslatedLabel($schemas, $request); |
||
| 137 | |||
| 138 | // Template map for current URL (existing behavior – JSON helper) |
||
| 139 | $settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 140 | $settingsWithTemplate = $settingsRepo->findBy(['url' => $url]); |
||
| 141 | |||
| 142 | foreach ($settingsWithTemplate as $s) { |
||
| 143 | if ($s->getValueTemplate()) { |
||
| 144 | $templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // MultiURL changeable flags: read from main URL (ID = 1) only |
||
| 149 | $changeableMap = []; |
||
| 150 | $mainUrlRows = $settingsRepo->createQueryBuilder('sc') |
||
| 151 | ->join('sc.url', 'u') |
||
| 152 | ->andWhere('u.id = :mainId') |
||
| 153 | ->setParameter('mainId', 1) |
||
| 154 | ->getQuery() |
||
| 155 | ->getResult(); |
||
| 156 | |||
| 157 | foreach ($mainUrlRows as $row) { |
||
| 158 | if ($row instanceof SettingsCurrent) { |
||
| 159 | $changeableMap[$row->getVariable()] = $row->getAccessUrlChangeable(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | $currentUrlId = $url->getId(); |
||
| 164 | // Only platform admins on the main URL can toggle the MultiURL flag. |
||
| 165 | $canToggleMultiUrlSetting = $this->isGranted('ROLE_ADMIN') && 1 === $currentUrlId; |
||
| 166 | |||
| 167 | if ('' === $keyword) { |
||
| 168 | return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
||
| 169 | 'keyword' => $keyword, |
||
| 170 | 'schemas' => $schemas, |
||
| 171 | 'settings' => $settings, |
||
| 172 | 'form_list' => $formList, |
||
| 173 | 'search_form' => $searchForm->createView(), |
||
| 174 | 'template_map' => $templateMap, |
||
| 175 | 'template_map_by_category' => $templateMapByCategory, |
||
| 176 | 'ordered_namespaces' => $ordered, |
||
| 177 | 'namespace_labels' => $labelMap, |
||
| 178 | 'changeable_map' => $changeableMap, |
||
| 179 | 'current_url_id' => $currentUrlId, |
||
| 180 | 'can_toggle_multiurl_setting' => $canToggleMultiUrlSetting, |
||
| 181 | ]); |
||
| 182 | } |
||
| 183 | |||
| 184 | $settingsFromKeyword = $manager->getParametersFromKeywordOrderedByCategory($keyword); |
||
| 185 | if (!empty($settingsFromKeyword)) { |
||
| 186 | foreach ($settingsFromKeyword as $category => $parameterList) { |
||
| 187 | if (empty($category)) { |
||
| 188 | continue; |
||
| 189 | } |
||
| 190 | |||
| 191 | $variablesInCategory = []; |
||
| 192 | foreach ($parameterList as $parameter) { |
||
| 193 | $var = $parameter->getVariable(); |
||
| 194 | $variablesInCategory[] = $var; |
||
| 195 | if (isset($templateMap[$var])) { |
||
| 196 | $templateMapByCategory[$category][$var] = $templateMap[$var]; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | // Convert category to schema alias and validate it BEFORE loading/creating the form |
||
| 201 | $schemaAlias = $manager->convertNameSpaceToService($category); |
||
| 202 | |||
| 203 | // Skip unknown/legacy categories (e.g., "tools") |
||
| 204 | if (!isset($schemas[$schemaAlias])) { |
||
| 205 | continue; |
||
| 206 | } |
||
| 207 | |||
| 208 | $settings = $manager->load($category); |
||
| 209 | $form = $this->getSettingsFormFactory()->create($schemaAlias); |
||
| 210 | |||
| 211 | foreach (array_keys($settings->getParameters()) as $name) { |
||
| 212 | if (!\in_array($name, $variablesInCategory, true)) { |
||
| 213 | $form->remove($name); |
||
| 214 | $settings->remove($name); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | $form->setData($settings); |
||
| 218 | $formList[$category] = $form->createView(); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
||
| 223 | 'keyword' => $keyword, |
||
| 224 | 'schemas' => $schemas, |
||
| 225 | 'settings' => $settings, |
||
| 226 | 'form_list' => $formList, |
||
| 227 | 'search_form' => $searchForm->createView(), |
||
| 228 | 'template_map' => $templateMap, |
||
| 229 | 'template_map_by_category' => $templateMapByCategory, |
||
| 230 | 'ordered_namespaces' => $ordered, |
||
| 231 | 'namespace_labels' => $labelMap, |
||
| 232 | 'changeable_map' => $changeableMap, |
||
| 233 | 'current_url_id' => $currentUrlId, |
||
| 234 | 'can_toggle_multiurl_setting' => $canToggleMultiUrlSetting, |
||
| 235 | ]); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Edit configuration with given namespace (main settings page). |
||
| 240 | */ |
||
| 241 | #[IsGranted('ROLE_ADMIN')] |
||
| 358 | ]); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Sync settings from classes with the database. |
||
| 363 | */ |
||
| 364 | #[IsGranted('ROLE_ADMIN')] |
||
| 365 | #[Route('/settings_sync', name: 'sync_settings')] |
||
| 366 | public function syncSettings(AccessUrlHelper $accessUrlHelper): Response |
||
| 367 | { |
||
| 368 | $manager = $this->getSettingsManager(); |
||
| 369 | $url = $accessUrlHelper->getCurrent(); |
||
| 370 | $manager->setUrl($url); |
||
| 371 | $manager->installSchemas($url); |
||
| 372 | |||
| 373 | return new Response('Updated'); |
||
| 374 | } |
||
| 375 | |||
| 376 | #[IsGranted('ROLE_ADMIN')] |
||
| 377 | #[Route('/settings/template/{id}', name: 'chamilo_platform_settings_template')] |
||
| 378 | public function getTemplateExample(int $id): JsonResponse |
||
| 379 | { |
||
| 380 | $repo = $this->entityManager->getRepository(SettingsValueTemplate::class); |
||
| 381 | $template = $repo->find($id); |
||
| 382 | |||
| 383 | if (!$template) { |
||
| 384 | return $this->json([ |
||
| 385 | 'error' => $this->translator->trans('Template not found.'), |
||
| 386 | ], Response::HTTP_NOT_FOUND); |
||
| 387 | } |
||
| 388 | |||
| 389 | return $this->json([ |
||
| 390 | 'variable' => $template->getVariable(), |
||
| 391 | 'json_example' => $template->getJsonExample(), |
||
| 392 | 'description' => $template->getDescription(), |
||
| 393 | ]); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return FormInterface |
||
| 398 | */ |
||
| 399 | private function getSearchForm() |
||
| 406 | } |
||
| 407 | |||
| 408 | private function computeOrderedNamespacesByTranslatedLabel(array $schemas, Request $request): array |
||
| 471 | } |
||
| 472 | } |
||
| 473 |