Conditions | 27 |
Paths | 2880 |
Total Lines | 260 |
Code Lines | 164 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
80 | public function settingsAction(Request $request) |
||
81 | { |
||
82 | $this->denyAccessUnlessGranted('ROLE_ADMIN'); |
||
83 | |||
84 | $requiredResetText = 'THIS DELETES ALL TICKETS'; |
||
85 | |||
86 | $settingKeys = [ |
||
87 | 'freeze' => false, |
||
88 | 'freezeMessage' => '', |
||
89 | 'remotesUrl' => '/', |
||
90 | 'upcomingCount' => 3, |
||
91 | 'songInPreview' => false, |
||
92 | 'selfSubmission' => false, |
||
93 | 'selfSubmissionKey' => '' |
||
94 | ]; |
||
95 | |||
96 | $formDefaults = $settingKeys; |
||
97 | |||
98 | $dataStore = $this->getDataStore(); |
||
99 | |||
100 | foreach ($settingKeys as $key => $default) { |
||
101 | $value = $dataStore->fetchSetting($key); |
||
102 | if (is_null($value)) { |
||
103 | $value = $default; |
||
104 | } else { |
||
105 | switch (gettype($default)) { |
||
106 | case 'integer': |
||
107 | $value = (int)$value; |
||
108 | break; |
||
109 | case 'boolean': |
||
110 | $value = (bool)$value; |
||
111 | break; |
||
112 | } |
||
113 | } |
||
114 | $formDefaults[$key] = $value; // fixme handle type better |
||
115 | } |
||
116 | |||
117 | $formFactory = Forms::createFormFactoryBuilder() |
||
118 | ->addExtension(new HttpFoundationExtension()) |
||
119 | ->getFormFactory(); |
||
120 | |||
121 | $settingsSubmit = 'Save Settings'; |
||
122 | $settingsForm = $formFactory->createNamedBuilder('settingsForm', FormType::class, $formDefaults) |
||
123 | ->add( |
||
124 | 'freeze', |
||
125 | CheckboxType::class, |
||
126 | ['label' => 'Display "Queue Frozen" message', 'required' => false] |
||
127 | ) |
||
128 | ->add( |
||
129 | 'freezeMessage', |
||
130 | TextType::class, |
||
131 | ['label' => 'Customise "Queue Frozen" message', 'required' => false] |
||
132 | ) |
||
133 | ->add( |
||
134 | 'remotesUrl', |
||
135 | TextType::class, |
||
136 | ['label' => 'URL to display on remote screens', 'required' => false] |
||
137 | ) |
||
138 | ->add( |
||
139 | 'upcomingCount', |
||
140 | NumberType::class, |
||
141 | ['label' => 'Upcoming songs to display', 'required' => false] |
||
142 | ) |
||
143 | ->add( |
||
144 | 'songInPreview', |
||
145 | CheckboxType::class, |
||
146 | ['label' => 'Display song titles on public queue', 'required' => false] |
||
147 | ) |
||
148 | ->add( |
||
149 | 'selfSubmission', |
||
150 | CheckboxType::class, |
||
151 | ['label' => 'Enable self-submission', 'required' => false] |
||
152 | )->add( |
||
153 | 'selfSubmissionKey', |
||
154 | TextType::class, |
||
155 | ['label' => 'Code required for self-submission (if any)', 'required' => false] |
||
156 | ) |
||
157 | ->add($settingsSubmit, SubmitType::class) |
||
158 | ->getForm(); |
||
159 | |||
160 | $settingsFormSaved = false; |
||
161 | |||
162 | if ($request->request->has('settingsForm')) { |
||
163 | $settingsForm->handleRequest($request); |
||
164 | |||
165 | /** |
||
166 | * @noinspection PhpUndefinedMethodInspection |
||
167 | */ // isClicked on Submit |
||
168 | if ($settingsForm->isSubmitted() |
||
169 | && $settingsForm->isValid() |
||
170 | && $settingsForm->get($settingsSubmit)->isClicked() |
||
171 | ) { |
||
172 | $data = $settingsForm->getData(); |
||
173 | foreach ($data as $key => $default) { |
||
174 | $dataStore->updateSetting($key, $default); |
||
175 | } |
||
176 | $settingsFormSaved = true; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | // ---------------- |
||
181 | |||
182 | $resetSubmit = 'Reset all'; |
||
183 | $resetForm = $formFactory->createNamedBuilder('resetForm', FormType::class) |
||
184 | ->add('resetMessage', TextType::class) |
||
185 | ->add($resetSubmit, SubmitType::class) |
||
186 | ->getForm(); |
||
187 | |||
188 | |||
189 | $resetFormSaved = false; |
||
190 | |||
191 | if ($request->request->has('resetForm')) { |
||
192 | $resetForm->handleRequest($request); |
||
193 | |||
194 | /** |
||
195 | * @noinspection PhpUndefinedMethodInspection |
||
196 | */ // isClicked on Submit |
||
197 | if ($resetForm->isSubmitted() |
||
198 | && $resetForm->isValid() |
||
199 | && $resetForm->get($resetSubmit)->isClicked() |
||
200 | ) { |
||
201 | $data = $resetForm->getData(); |
||
202 | if (trim($data['resetMessage']) === $requiredResetText) { |
||
203 | $dataStore->resetAllSessionData(); |
||
204 | $resetFormSaved = true; |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | |||
209 | // ------------------- |
||
210 | |||
211 | $rowMapperManager = $this->container->get('songloader.rowmappermanager'); |
||
212 | /** @var SongLoader\RowMapperManager $rowMapperManager */ |
||
213 | $mappers = $rowMapperManager->getRowMappers(); |
||
214 | $mapperInput = []; |
||
215 | foreach ($mappers as $mapper) { |
||
216 | $mapperInput[$mapper->getFormatterName()] = $mapper->getShortName(); |
||
217 | } |
||
218 | |||
219 | $songListSubmit = 'Upload song list'; |
||
220 | $songListForm = $formFactory->createNamedBuilder('songListForm', FormType::class) |
||
221 | ->add('rowMapper', ChoiceType::class, ['choices' => $mapperInput, 'label' => 'Input formatter']) |
||
222 | ->add('songListFile', FileType::class) |
||
223 | ->add($songListSubmit, SubmitType::class) |
||
224 | ->getForm(); |
||
225 | |||
226 | $songFormSaved = false; |
||
227 | $songsLoaded = false; |
||
228 | |||
229 | if ($request->request->has('songListForm')) { |
||
230 | $songListForm->handleRequest($request); |
||
231 | |||
232 | /** |
||
233 | * @noinspection PhpUndefinedMethodInspection |
||
234 | */ // isClicked on Submit |
||
235 | if ($songListForm->isSubmitted() |
||
236 | && $songListForm->isValid() |
||
237 | && $songListForm->get($songListSubmit)->isClicked() |
||
238 | ) { |
||
239 | $data = $songListForm->getData(); |
||
240 | |||
241 | $file = $data['songListFile']; |
||
242 | /** |
||
243 | * @var UploadedFile $file |
||
244 | */ |
||
245 | |||
246 | $loader = new SongLoader(); |
||
247 | $loader->setRowMapperClass($rowMapperManager->getRowMapperClassByShortName($data['rowMapper'])); |
||
248 | $songsLoaded = $loader->run($file->getPathname(), $this->get('database_connection')); |
||
249 | |||
250 | $songFormSaved = true; |
||
251 | } |
||
252 | } |
||
253 | |||
254 | // ------------------- |
||
255 | |||
256 | $defaults = ['customCss' => $dataStore->fetchSetting('customCss')]; |
||
257 | |||
258 | $stylingSubmit = 'Update styles'; |
||
259 | $stylingForm = $formFactory->createNamedBuilder('stylingForm', FormType::class, $defaults) |
||
260 | ->add('backgroundImageFile', FileType::class, ['label' => 'New background image', 'required' => false]) |
||
261 | ->add('removeImageFile', CheckboxType::class, ['label' => 'Remove background image', 'required' => false]) |
||
262 | ->add( |
||
263 | 'customCss', |
||
264 | TextareaType::class, |
||
265 | ['attr' => ['rows' => 8, 'cols' => 60, 'label' => 'Custom CSS'], 'required' => false] |
||
266 | ) |
||
267 | ->add($stylingSubmit, SubmitType::class) |
||
268 | ->getForm(); |
||
269 | |||
270 | $styleFormSaved = false; |
||
271 | $backgroundUpdated = false; |
||
272 | |||
273 | if ($request->request->has('stylingForm')) { |
||
274 | $stylingForm->handleRequest($request); |
||
275 | |||
276 | /** |
||
277 | * @noinspection PhpUndefinedMethodInspection |
||
278 | */ // isClicked on Submit |
||
279 | if ($stylingForm->isSubmitted() |
||
280 | && $stylingForm->isValid() |
||
281 | && $stylingForm->get($stylingSubmit)->isClicked() |
||
282 | ) { |
||
283 | $data = $stylingForm->getData(); |
||
284 | |||
285 | if ($data['removeImageFile']) { |
||
286 | $this->deleteBackgroundImageFiles(); |
||
287 | $dataStore->updateSetting('backgroundFilename', null); |
||
288 | } |
||
289 | |||
290 | $file = $data['backgroundImageFile']; |
||
291 | if ($file) { |
||
292 | $mimeType = $file->getMimeType(); |
||
293 | $pathName = $file->getPathName(); |
||
294 | /** |
||
295 | * @var UploadedFile $file |
||
296 | */ |
||
297 | $suffixByMimeType = [ |
||
298 | 'image/jpeg' => 'jpg', |
||
299 | 'image/gif' => 'gif', |
||
300 | 'image/png' => 'png', |
||
301 | ]; |
||
302 | |||
303 | if (array_key_exists($mimeType, $suffixByMimeType)) { |
||
304 | $suffix = $suffixByMimeType[$mimeType]; |
||
305 | $targetFile = 'background.' . $suffix; |
||
306 | $destination = $this->getBackgroundImageDir() . $targetFile; |
||
307 | move_uploaded_file($pathName, $destination); |
||
308 | $dataStore->updateSetting('backgroundFilename', $targetFile); |
||
309 | } else { |
||
310 | throw new \UnexpectedValueException("Invalid mimetype '$mimeType'"); |
||
311 | } |
||
312 | $backgroundUpdated = true; |
||
313 | } |
||
314 | |||
315 | $dataStore->updateSetting('customCss', $data['customCss']); |
||
316 | |||
317 | $styleFormSaved = true; |
||
318 | } |
||
319 | } |
||
320 | |||
321 | // ------------------- |
||
322 | |||
323 | return $this->render( |
||
324 | ':admin:settings.html.twig', |
||
325 | [ |
||
326 | 'settingsFormSaved' => $settingsFormSaved, |
||
327 | 'settingsForm' => $settingsForm->createView(), |
||
328 | 'resetForm' => $resetForm->createView(), |
||
329 | 'resetFormSaved' => $resetFormSaved, |
||
330 | 'resetRequiredText' => $requiredResetText, |
||
331 | 'songListForm' => $songListForm->createView(), |
||
332 | 'songFormSaved' => $songFormSaved, |
||
333 | 'songsLoaded' => $songsLoaded, |
||
334 | 'stylingForm' => $stylingForm->createView(), |
||
335 | 'styleFormSaved' => $styleFormSaved, |
||
336 | 'backgroundUpdated' => $backgroundUpdated, |
||
337 | ] |
||
338 | ); |
||
339 | } |
||
340 | |||
357 |