| Total Complexity | 65 |
| Total Lines | 564 |
| 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 |
||
| 33 | #[Route('/admin')] |
||
| 34 | class SettingsController extends BaseController |
||
| 35 | { |
||
| 36 | use ControllerTrait; |
||
| 37 | |||
| 38 | public function __construct( |
||
| 39 | private readonly EntityManagerInterface $entityManager, |
||
| 40 | private readonly TranslatorInterface $translator, |
||
| 41 | private readonly SearchIndexPathResolver $searchIndexPathResolver |
||
| 42 | ) {} |
||
| 43 | |||
| 44 | #[Route('/settings', name: 'admin_settings')] |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Toggle access_url_changeable for a given setting variable. |
||
| 52 | * Only platform admins on the main URL (ID = 1) are allowed to change it, |
||
| 53 | */ |
||
| 54 | #[IsGranted('ROLE_ADMIN')] |
||
| 55 | #[Route('/settings/toggle_changeable', name: 'settings_toggle_changeable', methods: ['POST'])] |
||
| 56 | public function toggleChangeable(Request $request, AccessUrlHelper $accessUrlHelper): JsonResponse |
||
| 57 | { |
||
| 58 | // Security: only admins. |
||
| 59 | if (!$this->isGranted('ROLE_ADMIN')) { |
||
| 60 | return $this->json([ |
||
| 61 | 'error' => 'Only platform admins can modify this flag.', |
||
| 62 | ], 403); |
||
| 63 | } |
||
| 64 | |||
| 65 | $currentUrl = $accessUrlHelper->getCurrent(); |
||
| 66 | if (!$currentUrl) { |
||
| 67 | return $this->json([ |
||
| 68 | 'error' => 'Access URL not resolved.', |
||
| 69 | ], 500); |
||
| 70 | } |
||
| 71 | |||
| 72 | $currentUrlId = (int) $currentUrl->getId(); |
||
| 73 | |||
| 74 | if (1 !== $currentUrlId) { |
||
| 75 | return $this->json([ |
||
| 76 | 'error' => 'Only the main URL (ID 1) can toggle this setting.', |
||
| 77 | ], 403); |
||
| 78 | } |
||
| 79 | |||
| 80 | $payload = json_decode($request->getContent(), true); |
||
| 81 | |||
| 82 | if (!\is_array($payload) || !isset($payload['variable'], $payload['status'])) { |
||
| 83 | return $this->json([ |
||
| 84 | 'error' => 'Invalid payload.', |
||
| 85 | ], 400); |
||
| 86 | } |
||
| 87 | |||
| 88 | $variable = trim((string) $payload['variable']); |
||
| 89 | $status = (int) $payload['status']; |
||
| 90 | $status = $status === 1 ? 1 : 0; |
||
| 91 | |||
| 92 | if ('' === $variable) { |
||
| 93 | return $this->json([ |
||
| 94 | 'error' => 'Invalid variable.', |
||
| 95 | ], 400); |
||
| 96 | } |
||
| 97 | |||
| 98 | $repo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 99 | |||
| 100 | // We search by variable + current main AccessUrl entity. |
||
| 101 | $setting = $repo->findOneBy([ |
||
| 102 | 'variable' => $variable, |
||
| 103 | 'url' => $currentUrl, |
||
| 104 | ]); |
||
| 105 | |||
| 106 | if (!$setting) { |
||
| 107 | return $this->json([ |
||
| 108 | 'error' => 'Setting not found.', |
||
| 109 | ], 404); |
||
| 110 | } |
||
| 111 | |||
| 112 | try { |
||
| 113 | $setting->setAccessUrlChangeable($status); |
||
| 114 | $this->entityManager->flush(); |
||
| 115 | |||
| 116 | return $this->json([ |
||
| 117 | 'result' => 1, |
||
| 118 | 'status' => $status, |
||
| 119 | ]); |
||
| 120 | } catch (\Throwable $e) { |
||
| 121 | return $this->json([ |
||
| 122 | 'error' => 'Unable to update setting.', |
||
| 123 | 'details' => $e->getMessage(), |
||
| 124 | ], 500); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Edit configuration with given namespace (search page). |
||
| 130 | */ |
||
| 131 | #[IsGranted('ROLE_ADMIN')] |
||
| 132 | #[Route('/settings/search_settings', name: 'chamilo_platform_settings_search')] |
||
| 133 | public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper): Response |
||
| 134 | { |
||
| 135 | $manager = $this->getSettingsManager(); |
||
| 136 | |||
| 137 | $url = $accessUrlHelper->getCurrent(); |
||
| 138 | $manager->setUrl($url); |
||
| 139 | |||
| 140 | $formList = []; |
||
| 141 | $templateMap = []; |
||
| 142 | $templateMapByCategory = []; |
||
| 143 | $settings = []; |
||
| 144 | |||
| 145 | $keyword = trim((string) $request->query->get('keyword', '')); |
||
| 146 | |||
| 147 | $searchForm = $this->getSearchForm(); |
||
| 148 | $searchForm->handleRequest($request); |
||
| 149 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||
| 150 | $values = $searchForm->getData(); |
||
| 151 | $keyword = trim((string) ($values['keyword'] ?? '')); |
||
| 152 | } |
||
| 153 | |||
| 154 | $schemas = $manager->getSchemas(); |
||
| 155 | [$ordered, $labelMap] = $this->computeOrderedNamespacesByTranslatedLabel($schemas, $request); |
||
| 156 | |||
| 157 | // Template map for current URL (existing behavior – JSON helper) |
||
| 158 | $settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 159 | |||
| 160 | // Build template map: current URL overrides main URL when missing. |
||
| 161 | $currentUrlId = (int) $url->getId(); |
||
| 162 | $mainUrl = $this->entityManager->getRepository(AccessUrl::class)->find(1); |
||
| 163 | |||
| 164 | if ($mainUrl instanceof AccessUrl && 1 !== $currentUrlId) { |
||
| 165 | $mainRows = $settingsRepo->findBy(['url' => $mainUrl]); |
||
| 166 | foreach ($mainRows as $s) { |
||
| 167 | if ($s->getValueTemplate()) { |
||
| 168 | $templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | $currentRows = $settingsRepo->findBy(['url' => $url]); |
||
| 174 | foreach ($currentRows as $s) { |
||
| 175 | if ($s->getValueTemplate()) { |
||
| 176 | $templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | // MultiURL changeable flags: read from main URL (ID = 1) only |
||
| 181 | $changeableMap = []; |
||
| 182 | $mainUrlRows = $settingsRepo->createQueryBuilder('sc') |
||
| 183 | ->join('sc.url', 'u') |
||
| 184 | ->andWhere('u.id = :mainId') |
||
| 185 | ->setParameter('mainId', 1) |
||
| 186 | ->getQuery() |
||
| 187 | ->getResult(); |
||
| 188 | |||
| 189 | foreach ($mainUrlRows as $row) { |
||
| 190 | if ($row instanceof SettingsCurrent) { |
||
| 191 | $changeableMap[$row->getVariable()] = $row->getAccessUrlChangeable(); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | // Only platform admins on the main URL can toggle the MultiURL flag. |
||
| 196 | $canToggleMultiUrlSetting = $this->isGranted('ROLE_ADMIN') && 1 === $currentUrlId; |
||
| 197 | |||
| 198 | if ('' === $keyword) { |
||
| 199 | return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
||
| 200 | 'keyword' => $keyword, |
||
| 201 | 'schemas' => $schemas, |
||
| 202 | 'settings' => $settings, |
||
| 203 | 'form_list' => $formList, |
||
| 204 | 'search_form' => $searchForm->createView(), |
||
| 205 | 'template_map' => $templateMap, |
||
| 206 | 'template_map_by_category' => $templateMapByCategory, |
||
| 207 | 'ordered_namespaces' => $ordered, |
||
| 208 | 'namespace_labels' => $labelMap, |
||
| 209 | 'changeable_map' => $changeableMap, |
||
| 210 | 'current_url_id' => $currentUrlId, |
||
| 211 | 'can_toggle_multiurl_setting' => $canToggleMultiUrlSetting, |
||
| 212 | ]); |
||
| 213 | } |
||
| 214 | |||
| 215 | $settingsFromKeyword = $manager->getParametersFromKeywordOrderedByCategory($keyword); |
||
| 216 | if (!empty($settingsFromKeyword)) { |
||
| 217 | foreach ($settingsFromKeyword as $category => $parameterList) { |
||
| 218 | if (empty($category)) { |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | $variablesInCategory = []; |
||
| 223 | foreach ($parameterList as $parameter) { |
||
| 224 | $var = $parameter->getVariable(); |
||
| 225 | $variablesInCategory[] = $var; |
||
| 226 | if (isset($templateMap[$var])) { |
||
| 227 | $templateMapByCategory[$category][$var] = $templateMap[$var]; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | // Convert category to schema alias and validate it BEFORE loading/creating the form |
||
| 232 | $schemaAlias = $manager->convertNameSpaceToService($category); |
||
| 233 | |||
| 234 | // skip unknown/legacy categories (e.g., "tools") |
||
| 235 | if (!isset($schemas[$schemaAlias])) { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | |||
| 239 | $settings = $manager->load($category); |
||
| 240 | $form = $this->getSettingsFormFactory()->create($schemaAlias); |
||
| 241 | |||
| 242 | foreach (array_keys($settings->getParameters()) as $name) { |
||
| 243 | if (!\in_array($name, $variablesInCategory, true)) { |
||
| 244 | $form->remove($name); |
||
| 245 | $settings->remove($name); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | $form->setData($settings); |
||
| 249 | $formList[$category] = $form->createView(); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
||
| 254 | 'keyword' => $keyword, |
||
| 255 | 'schemas' => $schemas, |
||
| 256 | 'settings' => $settings, |
||
| 257 | 'form_list' => $formList, |
||
| 258 | 'search_form' => $searchForm->createView(), |
||
| 259 | 'template_map' => $templateMap, |
||
| 260 | 'template_map_by_category' => $templateMapByCategory, |
||
| 261 | 'ordered_namespaces' => $ordered, |
||
| 262 | 'namespace_labels' => $labelMap, |
||
| 263 | 'changeable_map' => $changeableMap, |
||
| 264 | 'current_url_id' => $currentUrlId, |
||
| 265 | 'can_toggle_multiurl_setting' => $canToggleMultiUrlSetting, |
||
| 266 | ]); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Edit configuration with given namespace. |
||
| 271 | */ |
||
| 272 | #[IsGranted('ROLE_ADMIN')] |
||
| 407 | ]); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Sync settings from classes with the database. |
||
| 412 | */ |
||
| 413 | #[IsGranted('ROLE_ADMIN')] |
||
| 414 | #[Route('/settings_sync', name: 'sync_settings')] |
||
| 415 | public function syncSettings(AccessUrlHelper $accessUrlHelper): Response |
||
| 416 | { |
||
| 417 | $manager = $this->getSettingsManager(); |
||
| 418 | $url = $accessUrlHelper->getCurrent(); |
||
| 419 | $manager->setUrl($url); |
||
| 420 | $manager->installSchemas($url); |
||
| 421 | |||
| 422 | return new Response('Updated'); |
||
| 423 | } |
||
| 424 | |||
| 425 | #[IsGranted('ROLE_ADMIN')] |
||
| 426 | #[Route('/settings/template/{id}', name: 'chamilo_platform_settings_template')] |
||
| 427 | public function getTemplateExample(int $id): JsonResponse |
||
| 428 | { |
||
| 429 | $repo = $this->entityManager->getRepository(SettingsValueTemplate::class); |
||
| 430 | $template = $repo->find($id); |
||
| 431 | |||
| 432 | if (!$template) { |
||
| 433 | return $this->json([ |
||
| 434 | 'error' => $this->translator->trans('Template not found.'), |
||
| 435 | ], Response::HTTP_NOT_FOUND); |
||
| 436 | } |
||
| 437 | |||
| 438 | return $this->json([ |
||
| 439 | 'variable' => $template->getVariable(), |
||
| 440 | 'json_example' => $template->getJsonExample(), |
||
| 441 | 'description' => $template->getDescription(), |
||
| 442 | ]); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return FormInterface |
||
| 447 | */ |
||
| 448 | private function getSearchForm() |
||
| 455 | } |
||
| 456 | |||
| 457 | private function computeOrderedNamespacesByTranslatedLabel(array $schemas, Request $request): array |
||
| 458 | { |
||
| 459 | // Extract raw namespaces from schema service ids |
||
| 460 | $namespaces = array_map( |
||
| 461 | static fn ($k) => str_replace('chamilo_core.settings.', '', $k), |
||
| 462 | array_keys($schemas) |
||
| 463 | ); |
||
| 464 | |||
| 465 | $transform = [ |
||
| 466 | 'announcement' => 'Announcements', |
||
| 467 | 'attendance' => 'Attendances', |
||
| 468 | 'cas' => 'CAS', |
||
| 469 | 'certificate' => 'Certificates', |
||
| 470 | 'course' => 'Courses', |
||
| 471 | 'document' => 'Documents', |
||
| 472 | 'exercise' => 'Tests', |
||
| 473 | 'forum' => 'Forums', |
||
| 474 | 'group' => 'Groups', |
||
| 475 | 'language' => 'Internationalization', |
||
| 476 | 'lp' => 'Learning paths', |
||
| 477 | 'mail' => 'E-mail', |
||
| 478 | 'message' => 'Messages', |
||
| 479 | 'profile' => 'User profiles', |
||
| 480 | 'session' => 'Sessions', |
||
| 481 | 'skill' => 'Skills', |
||
| 482 | 'social' => 'Social network', |
||
| 483 | 'survey' => 'Surveys', |
||
| 484 | 'work' => 'Assignments', |
||
| 485 | 'ticket' => 'Support tickets', |
||
| 486 | 'tracking' => 'Reporting', |
||
| 487 | 'webservice' => 'Webservices', |
||
| 488 | 'catalog' => 'Catalogue', |
||
| 489 | 'catalogue' => 'Catalogue', |
||
| 490 | 'ai_helpers' => 'AI helpers', |
||
| 491 | ]; |
||
| 492 | |||
| 493 | // Build label map (translated). For keys not in $transform, use Title Case of ns. |
||
| 494 | $labelMap = []; |
||
| 495 | foreach ($namespaces as $ns) { |
||
| 496 | if (isset($transform[$ns])) { |
||
| 497 | $labelMap[$ns] = $this->translator->trans($transform[$ns]); |
||
| 498 | } else { |
||
| 499 | $key = ucfirst(str_replace('_', ' ', $ns)); |
||
| 500 | $labelMap[$ns] = $this->translator->trans($key); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | // Sort by translated label (locale-aware) |
||
| 505 | $collator = class_exists(Collator::class) ? new Collator($request->getLocale()) : null; |
||
| 506 | usort($namespaces, function ($a, $b) use ($labelMap, $collator) { |
||
| 507 | return $collator |
||
| 508 | ? $collator->compare($labelMap[$a], $labelMap[$b]) |
||
| 509 | : strcasecmp($labelMap[$a], $labelMap[$b]); |
||
| 510 | }); |
||
| 511 | |||
| 512 | // Optional: keep AI helpers near the top (second position) |
||
| 513 | $idx = array_search('ai_helpers', $namespaces, true); |
||
| 514 | if (false !== $idx) { |
||
| 515 | array_splice($namespaces, $idx, 1); |
||
| 516 | array_splice($namespaces, 1, 0, ['ai_helpers']); |
||
| 517 | } |
||
| 518 | |||
| 519 | return [$namespaces, $labelMap]; |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Build environment diagnostics for the "search" settings page. |
||
| 524 | */ |
||
| 525 | private function buildSearchDiagnostics(SettingsManager $manager): array |
||
| 597 | ]; |
||
| 598 | } |
||
| 599 | } |
||
| 600 |
If you suppress an error, we recommend checking for the error condition explicitly: