| Conditions | 11 |
| Paths | 56 |
| Total Lines | 72 |
| Code Lines | 49 |
| 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 |
||
| 113 | public function updateSettingAction(Request $request, $namespace): Response |
||
| 114 | { |
||
| 115 | $manager = $this->getSettingsManager(); |
||
| 116 | // @todo improve get the current url entity |
||
| 117 | $urlId = $request->getSession()->get('access_url_id'); |
||
| 118 | $url = $this->getDoctrine()->getRepository('ChamiloCoreBundle:AccessUrl')->find($urlId); |
||
| 119 | $manager->setUrl($url); |
||
| 120 | $schemaAlias = $manager->convertNameSpaceToService($namespace); |
||
| 121 | $searchForm = $this->getSearchForm(); |
||
| 122 | |||
| 123 | $keyword = ''; |
||
| 124 | $searchForm->handleRequest($request); |
||
| 125 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||
| 126 | $values = $searchForm->getData(); |
||
| 127 | $keyword = $values['keyword']; |
||
| 128 | $settingsFromKeyword = $manager->getParametersFromKeyword( |
||
| 129 | $schemaAlias, |
||
| 130 | $keyword |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | $keywordFromGet = $request->query->get('keyword'); |
||
| 135 | if ($keywordFromGet) { |
||
| 136 | $keyword = $keywordFromGet; |
||
| 137 | $searchForm->setData(['keyword' => $keyword]); |
||
| 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 ($params as $name => $value) { |
||
| 150 | if (!in_array($name, array_keys($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 $exception) { |
||
| 166 | $message = $this->trans($exception->getMessage(), [], 'validators'); |
||
| 167 | $messageType = 'error'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->addFlash($messageType, $message); |
||
| 171 | if (!empty($keywordFromGet)) { |
||
| 172 | return $this->redirect($request->headers->get('referer')); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | $schemas = $manager->getSchemas(); |
||
| 176 | |||
| 177 | return $this->render( |
||
| 178 | '@ChamiloCore/Admin/Settings/default.html.twig', |
||
| 179 | [ |
||
| 180 | 'schemas' => $schemas, |
||
| 181 | 'settings' => $settings, |
||
| 182 | 'form' => $form->createView(), |
||
| 183 | 'keyword' => $keyword, |
||
| 184 | 'search_form' => $searchForm->createView(), |
||
| 185 | ] |
||
| 214 |