| Conditions | 11 |
| Paths | 56 |
| Total Lines | 76 |
| Code Lines | 52 |
| 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 |
||
| 95 | #[IsGranted('ROLE_ADMIN')] |
||
| 96 | #[Route('/settings/{namespace}', name: 'chamilo_platform_settings')] |
||
| 97 | public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper, string $namespace): Response |
||
| 98 | { |
||
| 99 | $manager = $this->getSettingsManager(); |
||
| 100 | $url = $accessUrlHelper->getCurrent(); |
||
| 101 | $manager->setUrl($url); |
||
| 102 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 103 | $searchForm = $this->getSearchForm(); |
||
| 104 | |||
| 105 | $keyword = ''; |
||
| 106 | $settingsFromKeyword = null; |
||
| 107 | $searchForm->handleRequest($request); |
||
| 108 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||
| 109 | $values = $searchForm->getData(); |
||
| 110 | $keyword = $values['keyword']; |
||
| 111 | $settingsFromKeyword = $manager->getParametersFromKeyword( |
||
| 112 | $schemaAlias, |
||
| 113 | $keyword |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | $keywordFromGet = $request->query->get('keyword'); |
||
| 118 | if ($keywordFromGet) { |
||
| 119 | $keyword = $keywordFromGet; |
||
| 120 | $searchForm->setData([ |
||
| 121 | 'keyword' => $keyword, |
||
| 122 | ]); |
||
| 123 | $settingsFromKeyword = $manager->getParametersFromKeyword( |
||
| 124 | $schemaAlias, |
||
| 125 | $keywordFromGet |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | $settings = $manager->load($namespace); |
||
| 130 | $form = $this->getSettingsFormFactory()->create($schemaAlias); |
||
| 131 | |||
| 132 | if (!empty($keyword)) { |
||
| 133 | $params = $settings->getParameters(); |
||
| 134 | foreach (array_keys($params) as $name) { |
||
| 135 | if (!\array_key_exists($name, $settingsFromKeyword)) { |
||
| 136 | $form->remove($name); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $form->setData($settings); |
||
| 142 | $form->handleRequest($request); |
||
| 143 | |||
| 144 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 145 | $messageType = 'success'; |
||
| 146 | |||
| 147 | try { |
||
| 148 | $manager->save($form->getData()); |
||
| 149 | $message = $this->trans('Settings have been successfully updated'); |
||
| 150 | } catch (ValidatorException $validatorException) { |
||
| 151 | // $message = $this->trans($exception->getMessage(), [], 'validators'); |
||
| 152 | $message = $this->trans($validatorException->getMessage()); |
||
| 153 | $messageType = 'error'; |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->addFlash($messageType, $message); |
||
| 157 | if (!empty($keywordFromGet)) { |
||
| 158 | return $this->redirect($request->headers->get('referer')); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | $schemas = $manager->getSchemas(); |
||
| 162 | |||
| 163 | return $this->render( |
||
| 164 | '@ChamiloCore/Admin/Settings/default.html.twig', |
||
| 165 | [ |
||
| 166 | 'schemas' => $schemas, |
||
| 167 | 'settings' => $settings, |
||
| 168 | 'form' => $form->createView(), |
||
| 169 | 'keyword' => $keyword, |
||
| 170 | 'search_form' => $searchForm, |
||
| 171 | ] |
||
| 202 |