Total Complexity | 44 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ThemingController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ThemingController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | class ThemingController extends Controller { |
||
68 | private ThemingDefaults $themingDefaults; |
||
69 | private IL10N $l10n; |
||
70 | private IConfig $config; |
||
71 | private ITempManager $tempManager; |
||
72 | private IAppData $appData; |
||
73 | private IURLGenerator $urlGenerator; |
||
74 | private IAppManager $appManager; |
||
75 | private ImageManager $imageManager; |
||
76 | private ThemesService $themesService; |
||
77 | |||
78 | public function __construct( |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @AuthorizedAdminSetting(settings=OCA\Theming\Settings\Admin) |
||
106 | * @param string $setting |
||
107 | * @param string $value |
||
108 | * @return DataResponse |
||
109 | * @throws NotPermittedException |
||
110 | */ |
||
111 | public function updateStylesheet($setting, $value) { |
||
176 | ]); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Check that a string is a valid http/https url |
||
181 | */ |
||
182 | private function isValidUrl(string $url): bool { |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @AuthorizedAdminSetting(settings=OCA\Theming\Settings\Admin) |
||
189 | * @return DataResponse |
||
190 | * @throws NotPermittedException |
||
191 | */ |
||
192 | public function uploadImage(): DataResponse { |
||
193 | $key = $this->request->getParam('key'); |
||
194 | $image = $this->request->getUploadedFile('image'); |
||
195 | $error = null; |
||
196 | $phpFileUploadErrors = [ |
||
197 | UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
||
198 | UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
||
199 | UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
||
200 | UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
||
201 | UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
||
202 | UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
||
203 | UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
||
204 | UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
||
205 | ]; |
||
206 | if (empty($image)) { |
||
207 | $error = $this->l10n->t('No file uploaded'); |
||
208 | } |
||
209 | if (!empty($image) && array_key_exists('error', $image) && $image['error'] !== UPLOAD_ERR_OK) { |
||
210 | $error = $phpFileUploadErrors[$image['error']]; |
||
211 | } |
||
212 | |||
213 | if ($error !== null) { |
||
214 | return new DataResponse( |
||
215 | [ |
||
216 | 'data' => [ |
||
217 | 'message' => $error |
||
218 | ], |
||
219 | 'status' => 'failure', |
||
220 | ], |
||
221 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | try { |
||
226 | $mime = $this->imageManager->updateImage($key, $image['tmp_name']); |
||
227 | $this->themingDefaults->set($key . 'Mime', $mime); |
||
228 | } catch (\Exception $e) { |
||
229 | return new DataResponse( |
||
230 | [ |
||
231 | 'data' => [ |
||
232 | 'message' => $e->getMessage() |
||
233 | ], |
||
234 | 'status' => 'failure', |
||
235 | ], |
||
236 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
237 | ); |
||
238 | } |
||
239 | |||
240 | $name = $image['name']; |
||
241 | |||
242 | return new DataResponse( |
||
243 | [ |
||
244 | 'data' => |
||
245 | [ |
||
246 | 'name' => $name, |
||
247 | 'url' => $this->imageManager->getImageUrl($key), |
||
248 | 'message' => $this->l10n->t('Saved'), |
||
249 | ], |
||
250 | 'status' => 'success' |
||
251 | ] |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Revert setting to default value |
||
257 | * @AuthorizedAdminSetting(settings=OCA\Theming\Settings\Admin) |
||
258 | * |
||
259 | * @param string $setting setting which should be reverted |
||
260 | * @return DataResponse |
||
261 | * @throws NotPermittedException |
||
262 | */ |
||
263 | public function undo(string $setting): DataResponse { |
||
264 | $value = $this->themingDefaults->undo($setting); |
||
265 | |||
266 | return new DataResponse( |
||
267 | [ |
||
268 | 'data' => |
||
269 | [ |
||
270 | 'value' => $value, |
||
271 | 'message' => $this->l10n->t('Saved'), |
||
272 | ], |
||
273 | 'status' => 'success' |
||
274 | ] |
||
275 | ); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @PublicPage |
||
280 | * @NoCSRFRequired |
||
281 | * @NoSameSiteCookieRequired |
||
282 | * |
||
283 | * @param string $key |
||
284 | * @param bool $useSvg |
||
285 | * @return FileDisplayResponse|NotFoundResponse |
||
286 | * @throws NotPermittedException |
||
287 | */ |
||
288 | public function getImage(string $key, bool $useSvg = true) { |
||
289 | try { |
||
290 | $file = $this->imageManager->getImage($key, $useSvg); |
||
291 | } catch (NotFoundException $e) { |
||
292 | return new NotFoundResponse(); |
||
293 | } |
||
294 | |||
295 | $response = new FileDisplayResponse($file); |
||
296 | $csp = new Http\ContentSecurityPolicy(); |
||
297 | $csp->allowInlineStyle(); |
||
298 | $response->setContentSecurityPolicy($csp); |
||
299 | $response->cacheFor(3600); |
||
300 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
301 | $response->addHeader('Content-Disposition', 'attachment; filename="' . $key . '"'); |
||
302 | if (!$useSvg) { |
||
303 | $response->addHeader('Content-Type', 'image/png'); |
||
304 | } else { |
||
305 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
306 | } |
||
307 | return $response; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @NoCSRFRequired |
||
312 | * @PublicPage |
||
313 | * @NoSameSiteCookieRequired |
||
314 | * @NoTwoFactorRequired |
||
315 | * |
||
316 | * @return DataDisplayResponse|NotFoundResponse |
||
317 | */ |
||
318 | public function getThemeStylesheet(string $themeId, bool $plain = false, bool $withCustomCss = false) { |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @NoCSRFRequired |
||
354 | * @PublicPage |
||
355 | * |
||
356 | * @return Http\JSONResponse |
||
357 | */ |
||
358 | public function getManifest($app) { |
||
403 | } |
||
404 | } |
||
405 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.