| Conditions | 13 |
| Paths | 72 |
| Total Lines | 96 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 109 | #[IsGranted('ROLE_ADMIN')] |
||
| 110 | #[Route('/settings/{namespace}', name: 'chamilo_platform_settings')] |
||
| 111 | public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper, string $namespace): Response |
||
| 112 | { |
||
| 113 | $manager = $this->getSettingsManager(); |
||
| 114 | $url = $accessUrlHelper->getCurrent(); |
||
| 115 | $manager->setUrl($url); |
||
| 116 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 117 | $searchForm = $this->getSearchForm(); |
||
| 118 | |||
| 119 | $keyword = ''; |
||
| 120 | $settingsFromKeyword = null; |
||
| 121 | |||
| 122 | $searchForm->handleRequest($request); |
||
| 123 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||
| 124 | $values = $searchForm->getData(); |
||
| 125 | $keyword = $values['keyword']; |
||
| 126 | $settingsFromKeyword = $manager->getParametersFromKeyword( |
||
| 127 | $schemaAlias, |
||
| 128 | $keyword |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | $keywordFromGet = $request->query->get('keyword'); |
||
| 133 | if ($keywordFromGet) { |
||
| 134 | $keyword = $keywordFromGet; |
||
| 135 | $searchForm->setData([ |
||
| 136 | 'keyword' => $keyword, |
||
| 137 | ]); |
||
| 138 | $settingsFromKeyword = $manager->getParametersFromKeyword( |
||
| 139 | $schemaAlias, |
||
| 140 | $keywordFromGet |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | $settings = $manager->load($namespace); |
||
| 145 | $form = $this->getSettingsFormFactory()->create($schemaAlias); |
||
| 146 | |||
| 147 | if (!empty($keyword)) { |
||
| 148 | $params = $settings->getParameters(); |
||
| 149 | foreach (array_keys($params) as $name) { |
||
| 150 | if (!\array_key_exists($name, $settingsFromKeyword)) { |
||
| 151 | $form->remove($name); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $form->setData($settings); |
||
| 157 | $form->handleRequest($request); |
||
| 158 | |||
| 159 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 160 | $messageType = 'success'; |
||
| 161 | |||
| 162 | try { |
||
| 163 | $manager->save($form->getData()); |
||
| 164 | $message = $this->trans('Settings have been successfully updated'); |
||
| 165 | } catch (ValidatorException $validatorException) { |
||
| 166 | $message = $this->trans($validatorException->getMessage()); |
||
| 167 | $messageType = 'error'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->addFlash($messageType, $message); |
||
| 171 | |||
| 172 | if (!empty($keyword)) { |
||
| 173 | return $this->redirectToRoute('chamilo_platform_settings_search', [ |
||
| 174 | 'keyword' => $keyword, |
||
| 175 | ]); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $this->redirectToRoute('chamilo_platform_settings', [ |
||
| 179 | 'namespace' => $namespace, |
||
| 180 | ]); |
||
| 181 | } |
||
| 182 | |||
| 183 | $schemas = $manager->getSchemas(); |
||
| 184 | |||
| 185 | $templateMap = []; |
||
| 186 | $settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 187 | |||
| 188 | $settingsWithTemplate = $settingsRepo->findBy(['url' => $url]); |
||
| 189 | |||
| 190 | foreach ($settingsWithTemplate as $s) { |
||
| 191 | if ($s->getValueTemplate()) { |
||
| 192 | $templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | return $this->render( |
||
| 197 | '@ChamiloCore/Admin/Settings/default.html.twig', |
||
| 198 | [ |
||
| 199 | 'schemas' => $schemas, |
||
| 200 | 'settings' => $settings, |
||
| 201 | 'form' => $form->createView(), |
||
| 202 | 'keyword' => $keyword, |
||
| 203 | 'search_form' => $searchForm, |
||
| 204 | 'template_map' => $templateMap, |
||
| 205 | ] |
||
| 255 |