Total Complexity | 45 |
Total Lines | 418 |
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 |
||
64 | class ThemingController extends Controller { |
||
65 | /** @var ThemingDefaults */ |
||
66 | private $themingDefaults; |
||
67 | /** @var Util */ |
||
68 | private $util; |
||
69 | /** @var IL10N */ |
||
70 | private $l10n; |
||
71 | /** @var IConfig */ |
||
72 | private $config; |
||
73 | /** @var ITempManager */ |
||
74 | private $tempManager; |
||
75 | /** @var IAppData */ |
||
76 | private $appData; |
||
77 | /** @var SCSSCacher */ |
||
78 | private $scssCacher; |
||
79 | /** @var IURLGenerator */ |
||
80 | private $urlGenerator; |
||
81 | /** @var IAppManager */ |
||
82 | private $appManager; |
||
83 | /** @var ImageManager */ |
||
84 | private $imageManager; |
||
85 | |||
86 | /** |
||
87 | * ThemingController constructor. |
||
88 | * |
||
89 | * @param string $appName |
||
90 | * @param IRequest $request |
||
91 | * @param IConfig $config |
||
92 | * @param ThemingDefaults $themingDefaults |
||
93 | * @param Util $util |
||
94 | * @param IL10N $l |
||
95 | * @param ITempManager $tempManager |
||
96 | * @param IAppData $appData |
||
97 | * @param SCSSCacher $scssCacher |
||
98 | * @param IURLGenerator $urlGenerator |
||
99 | * @param IAppManager $appManager |
||
100 | * @param ImageManager $imageManager |
||
101 | */ |
||
102 | public function __construct( |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $setting |
||
132 | * @param string $value |
||
133 | * @return DataResponse |
||
134 | * @throws NotPermittedException |
||
135 | */ |
||
136 | public function updateStylesheet($setting, $value) { |
||
137 | $value = trim($value); |
||
138 | $error = null; |
||
139 | switch ($setting) { |
||
140 | case 'name': |
||
141 | if (strlen($value) > 250) { |
||
142 | $error = $this->l10n->t('The given name is too long'); |
||
143 | } |
||
144 | break; |
||
145 | case 'url': |
||
146 | if (strlen($value) > 500) { |
||
147 | $error = $this->l10n->t('The given web address is too long'); |
||
148 | } |
||
149 | if (!$this->isValidUrl($value)) { |
||
150 | $error = $this->l10n->t('The given web address is not a valid URL'); |
||
151 | } |
||
152 | break; |
||
153 | case 'imprintUrl': |
||
154 | if (strlen($value) > 500) { |
||
155 | $error = $this->l10n->t('The given legal notice address is too long'); |
||
156 | } |
||
157 | if (!$this->isValidUrl($value)) { |
||
158 | $error = $this->l10n->t('The given legal notice address is not a valid URL'); |
||
159 | } |
||
160 | break; |
||
161 | case 'privacyUrl': |
||
162 | if (strlen($value) > 500) { |
||
163 | $error = $this->l10n->t('The given privacy policy address is too long'); |
||
164 | } |
||
165 | if (!$this->isValidUrl($value)) { |
||
166 | $error = $this->l10n->t('The given privacy policy address is not a valid URL'); |
||
167 | } |
||
168 | break; |
||
169 | case 'slogan': |
||
170 | if (strlen($value) > 500) { |
||
171 | $error = $this->l10n->t('The given slogan is too long'); |
||
172 | } |
||
173 | break; |
||
174 | case 'color': |
||
175 | if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) { |
||
176 | $error = $this->l10n->t('The given color is invalid'); |
||
177 | } |
||
178 | break; |
||
179 | } |
||
180 | if ($error !== null) { |
||
181 | return new DataResponse([ |
||
182 | 'data' => [ |
||
183 | 'message' => $error, |
||
184 | ], |
||
185 | 'status' => 'error' |
||
186 | ], Http::STATUS_BAD_REQUEST); |
||
187 | } |
||
188 | |||
189 | $this->themingDefaults->set($setting, $value); |
||
190 | |||
191 | // reprocess server scss for preview |
||
192 | $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/css-variables.scss', 'core'); |
||
|
|||
193 | |||
194 | return new DataResponse( |
||
195 | [ |
||
196 | 'data' => |
||
197 | [ |
||
198 | 'message' => $this->l10n->t('Saved'), |
||
199 | 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/css-variables.scss')) |
||
200 | ], |
||
201 | 'status' => 'success' |
||
202 | ] |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Check that a string is a valid http/https url |
||
208 | */ |
||
209 | private function isValidUrl(string $url): bool { |
||
210 | return ((strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) && |
||
211 | filter_var($url, FILTER_VALIDATE_URL) !== false); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return DataResponse |
||
216 | * @throws NotPermittedException |
||
217 | */ |
||
218 | public function uploadImage(): DataResponse { |
||
219 | // logo / background |
||
220 | // new: favicon logo-header |
||
221 | // |
||
222 | $key = $this->request->getParam('key'); |
||
223 | $image = $this->request->getUploadedFile('image'); |
||
224 | $error = null; |
||
225 | $phpFileUploadErrors = [ |
||
226 | UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
||
227 | UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
||
228 | UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
||
229 | UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
||
230 | UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
||
231 | UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
||
232 | UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
||
233 | UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
||
234 | ]; |
||
235 | if (empty($image)) { |
||
236 | $error = $this->l10n->t('No file uploaded'); |
||
237 | } |
||
238 | if (!empty($image) && array_key_exists('error', $image) && $image['error'] !== UPLOAD_ERR_OK) { |
||
239 | $error = $phpFileUploadErrors[$image['error']]; |
||
240 | } |
||
241 | |||
242 | if ($error !== null) { |
||
243 | return new DataResponse( |
||
244 | [ |
||
245 | 'data' => [ |
||
246 | 'message' => $error |
||
247 | ], |
||
248 | 'status' => 'failure', |
||
249 | ], |
||
250 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | $name = ''; |
||
255 | try { |
||
256 | $folder = $this->appData->getFolder('images'); |
||
257 | } catch (NotFoundException $e) { |
||
258 | $folder = $this->appData->newFolder('images'); |
||
259 | } |
||
260 | |||
261 | $this->imageManager->delete($key); |
||
262 | |||
263 | $target = $folder->newFile($key); |
||
264 | $supportedFormats = $this->getSupportedUploadImageFormats($key); |
||
265 | $detectedMimeType = mime_content_type($image['tmp_name']); |
||
266 | if (!in_array($image['type'], $supportedFormats) || !in_array($detectedMimeType, $supportedFormats)) { |
||
267 | return new DataResponse( |
||
268 | [ |
||
269 | 'data' => [ |
||
270 | 'message' => $this->l10n->t('Unsupported image type'), |
||
271 | ], |
||
272 | 'status' => 'failure', |
||
273 | ], |
||
274 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
275 | ); |
||
276 | } |
||
277 | |||
278 | $resizeKeys = ['background']; |
||
279 | if (in_array($key, $resizeKeys, true)) { |
||
280 | // Optimize the image since some people may upload images that will be |
||
281 | // either to big or are not progressive rendering. |
||
282 | $newImage = @imagecreatefromstring(file_get_contents($image['tmp_name'], 'r')); |
||
283 | |||
284 | $tmpFile = $this->tempManager->getTemporaryFile(); |
||
285 | $newWidth = imagesx($newImage) < 4096 ? imagesx($newImage) : 4096; |
||
286 | $newHeight = imagesy($newImage) / (imagesx($newImage) / $newWidth); |
||
287 | $outputImage = imagescale($newImage, $newWidth, $newHeight); |
||
288 | |||
289 | imageinterlace($outputImage, 1); |
||
290 | imagejpeg($outputImage, $tmpFile, 75); |
||
291 | imagedestroy($outputImage); |
||
292 | |||
293 | $target->putContent(file_get_contents($tmpFile, 'r')); |
||
294 | } else { |
||
295 | $target->putContent(file_get_contents($image['tmp_name'], 'r')); |
||
296 | } |
||
297 | $name = $image['name']; |
||
298 | |||
299 | $this->themingDefaults->set($key.'Mime', $image['type']); |
||
300 | |||
301 | $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/css-variables.scss', 'core'); |
||
302 | |||
303 | return new DataResponse( |
||
304 | [ |
||
305 | 'data' => |
||
306 | [ |
||
307 | 'name' => $name, |
||
308 | 'url' => $this->imageManager->getImageUrl($key), |
||
309 | 'message' => $this->l10n->t('Saved'), |
||
310 | 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/css-variables.scss')) |
||
311 | ], |
||
312 | 'status' => 'success' |
||
313 | ] |
||
314 | ); |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Returns a list of supported mime types for image uploads. |
||
319 | * "favicon" images are only allowed to be SVG when imagemagick with SVG support is available. |
||
320 | * |
||
321 | * @param string $key The image key, e.g. "favicon" |
||
322 | * @return array |
||
323 | */ |
||
324 | private function getSupportedUploadImageFormats(string $key): array { |
||
325 | $supportedFormats = ['image/jpeg', 'image/png', 'image/gif',]; |
||
326 | |||
327 | if ($key !== 'favicon' || $this->imageManager->shouldReplaceIcons() === true) { |
||
328 | $supportedFormats[] = 'image/svg+xml'; |
||
329 | $supportedFormats[] = 'image/svg'; |
||
330 | } |
||
331 | |||
332 | return $supportedFormats; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Revert setting to default value |
||
337 | * |
||
338 | * @param string $setting setting which should be reverted |
||
339 | * @return DataResponse |
||
340 | * @throws NotPermittedException |
||
341 | */ |
||
342 | public function undo(string $setting): DataResponse { |
||
343 | $value = $this->themingDefaults->undo($setting); |
||
344 | // reprocess server scss for preview |
||
345 | $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/css-variables.scss', 'core'); |
||
346 | |||
347 | if (strpos($setting, 'Mime') !== -1) { |
||
348 | $imageKey = str_replace('Mime', '', $setting); |
||
349 | $this->imageManager->delete($imageKey); |
||
350 | } |
||
351 | |||
352 | return new DataResponse( |
||
353 | [ |
||
354 | 'data' => |
||
355 | [ |
||
356 | 'value' => $value, |
||
357 | 'message' => $this->l10n->t('Saved'), |
||
358 | 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/css-variables.scss')) |
||
359 | ], |
||
360 | 'status' => 'success' |
||
361 | ] |
||
362 | ); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @PublicPage |
||
367 | * @NoCSRFRequired |
||
368 | * |
||
369 | * @param string $key |
||
370 | * @param bool $useSvg |
||
371 | * @return FileDisplayResponse|NotFoundResponse |
||
372 | * @throws NotPermittedException |
||
373 | */ |
||
374 | public function getImage(string $key, bool $useSvg = true) { |
||
375 | try { |
||
376 | $file = $this->imageManager->getImage($key, $useSvg); |
||
377 | } catch (NotFoundException $e) { |
||
378 | return new NotFoundResponse(); |
||
379 | } |
||
380 | |||
381 | $response = new FileDisplayResponse($file); |
||
382 | $response->cacheFor(3600); |
||
383 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
384 | $response->addHeader('Content-Disposition', 'attachment; filename="' . $key . '"'); |
||
385 | if (!$useSvg) { |
||
386 | $response->addHeader('Content-Type', 'image/png'); |
||
387 | } else { |
||
388 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
389 | } |
||
390 | return $response; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @NoCSRFRequired |
||
395 | * @PublicPage |
||
396 | * @NoSameSiteCookieRequired |
||
397 | * |
||
398 | * @return FileDisplayResponse|NotFoundResponse |
||
399 | * @throws NotPermittedException |
||
400 | * @throws \Exception |
||
401 | * @throws \OCP\App\AppPathNotFoundException |
||
402 | */ |
||
403 | public function getStylesheet() { |
||
422 | } |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @NoCSRFRequired |
||
427 | * @PublicPage |
||
428 | * @NoSameSiteCookieRequired |
||
429 | * |
||
430 | * @return DataDownloadResponse |
||
431 | */ |
||
432 | public function getJavascript() { |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * @NoCSRFRequired |
||
453 | * @PublicPage |
||
454 | * |
||
455 | * @return Http\JSONResponse |
||
456 | */ |
||
457 | public function getManifest($app) { |
||
482 | } |
||
483 | } |
||
484 |