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