| Total Complexity | 41 |
| Total Lines | 331 |
| 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( |
||
| 79 | $appName, |
||
| 80 | IRequest $request, |
||
| 81 | IConfig $config, |
||
| 82 | ThemingDefaults $themingDefaults, |
||
| 83 | IL10N $l, |
||
| 84 | ITempManager $tempManager, |
||
| 85 | IAppData $appData, |
||
| 86 | IURLGenerator $urlGenerator, |
||
| 87 | IAppManager $appManager, |
||
| 88 | ImageManager $imageManager, |
||
| 89 | ThemesService $themesService |
||
| 90 | ) { |
||
| 91 | parent::__construct($appName, $request); |
||
| 92 | |||
| 93 | $this->themingDefaults = $themingDefaults; |
||
| 94 | $this->l10n = $l; |
||
| 95 | $this->config = $config; |
||
| 96 | $this->tempManager = $tempManager; |
||
| 97 | $this->appData = $appData; |
||
| 98 | $this->urlGenerator = $urlGenerator; |
||
| 99 | $this->appManager = $appManager; |
||
| 100 | $this->imageManager = $imageManager; |
||
| 101 | $this->themesService = $themesService; |
||
| 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) { |
||
| 171 | ]); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Check that a string is a valid http/https url |
||
| 176 | */ |
||
| 177 | private function isValidUrl(string $url): bool { |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @AuthorizedAdminSetting(settings=OCA\Theming\Settings\Admin) |
||
| 184 | * @return DataResponse |
||
| 185 | * @throws NotPermittedException |
||
| 186 | */ |
||
| 187 | public function uploadImage(): DataResponse { |
||
| 188 | $key = $this->request->getParam('key'); |
||
| 189 | $image = $this->request->getUploadedFile('image'); |
||
| 190 | $error = null; |
||
| 191 | $phpFileUploadErrors = [ |
||
| 192 | UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
||
| 193 | UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
||
| 194 | UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
||
| 195 | UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
||
| 196 | UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
||
| 197 | UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
||
| 198 | UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
||
| 199 | UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
||
| 200 | ]; |
||
| 201 | if (empty($image)) { |
||
| 202 | $error = $this->l10n->t('No file uploaded'); |
||
| 203 | } |
||
| 204 | if (!empty($image) && array_key_exists('error', $image) && $image['error'] !== UPLOAD_ERR_OK) { |
||
| 205 | $error = $phpFileUploadErrors[$image['error']]; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($error !== null) { |
||
| 209 | return new DataResponse( |
||
| 210 | [ |
||
| 211 | 'data' => [ |
||
| 212 | 'message' => $error |
||
| 213 | ], |
||
| 214 | 'status' => 'failure', |
||
| 215 | ], |
||
| 216 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | try { |
||
| 221 | $mime = $this->imageManager->updateImage($key, $image['tmp_name']); |
||
| 222 | $this->themingDefaults->set($key . 'Mime', $mime); |
||
| 223 | } catch (\Exception $e) { |
||
| 224 | return new DataResponse( |
||
| 225 | [ |
||
| 226 | 'data' => [ |
||
| 227 | 'message' => $e->getMessage() |
||
| 228 | ], |
||
| 229 | 'status' => 'failure', |
||
| 230 | ], |
||
| 231 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | $name = $image['name']; |
||
| 236 | |||
| 237 | return new DataResponse( |
||
| 238 | [ |
||
| 239 | 'data' => |
||
| 240 | [ |
||
| 241 | 'name' => $name, |
||
| 242 | 'url' => $this->imageManager->getImageUrl($key), |
||
| 243 | 'message' => $this->l10n->t('Saved'), |
||
| 244 | ], |
||
| 245 | 'status' => 'success' |
||
| 246 | ] |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Revert setting to default value |
||
| 252 | * @AuthorizedAdminSetting(settings=OCA\Theming\Settings\Admin) |
||
| 253 | * |
||
| 254 | * @param string $setting setting which should be reverted |
||
| 255 | * @return DataResponse |
||
| 256 | * @throws NotPermittedException |
||
| 257 | */ |
||
| 258 | public function undo(string $setting): DataResponse { |
||
| 259 | $value = $this->themingDefaults->undo($setting); |
||
| 260 | |||
| 261 | return new DataResponse( |
||
| 262 | [ |
||
| 263 | 'data' => |
||
| 264 | [ |
||
| 265 | 'value' => $value, |
||
| 266 | 'message' => $this->l10n->t('Saved'), |
||
| 267 | ], |
||
| 268 | 'status' => 'success' |
||
| 269 | ] |
||
| 270 | ); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @PublicPage |
||
| 275 | * @NoCSRFRequired |
||
| 276 | * @NoSameSiteCookieRequired |
||
| 277 | * |
||
| 278 | * @param string $key |
||
| 279 | * @param bool $useSvg |
||
| 280 | * @return FileDisplayResponse|NotFoundResponse |
||
| 281 | * @throws NotPermittedException |
||
| 282 | */ |
||
| 283 | public function getImage(string $key, bool $useSvg = true) { |
||
| 284 | try { |
||
| 285 | $file = $this->imageManager->getImage($key, $useSvg); |
||
| 286 | } catch (NotFoundException $e) { |
||
| 287 | return new NotFoundResponse(); |
||
| 288 | } |
||
| 289 | |||
| 290 | $response = new FileDisplayResponse($file); |
||
| 291 | $csp = new Http\ContentSecurityPolicy(); |
||
| 292 | $csp->allowInlineStyle(); |
||
| 293 | $response->setContentSecurityPolicy($csp); |
||
| 294 | $response->cacheFor(3600); |
||
| 295 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
| 296 | $response->addHeader('Content-Disposition', 'attachment; filename="' . $key . '"'); |
||
| 297 | if (!$useSvg) { |
||
| 298 | $response->addHeader('Content-Type', 'image/png'); |
||
| 299 | } else { |
||
| 300 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
| 301 | } |
||
| 302 | return $response; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @NoCSRFRequired |
||
| 307 | * @PublicPage |
||
| 308 | * @NoSameSiteCookieRequired |
||
| 309 | * @NoTwoFactorRequired |
||
| 310 | * |
||
| 311 | * @return DataDisplayResponse|NotFoundResponse |
||
| 312 | */ |
||
| 313 | public function getThemeStylesheet(string $themeId, bool $plain = false, bool $withCustomCss = false) { |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @NoCSRFRequired |
||
| 349 | * @PublicPage |
||
| 350 | * |
||
| 351 | * @return Http\JSONResponse |
||
| 352 | */ |
||
| 353 | public function getManifest($app) { |
||
| 398 | } |
||
| 399 | } |
||
| 400 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.