| Total Complexity | 64 |
| Total Lines | 390 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ThemingDefaults 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 ThemingDefaults, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class ThemingDefaults extends \OC_Defaults { |
||
| 53 | |||
| 54 | /** @var IConfig */ |
||
| 55 | private $config; |
||
| 56 | /** @var IL10N */ |
||
| 57 | private $l; |
||
| 58 | /** @var ImageManager */ |
||
| 59 | private $imageManager; |
||
| 60 | /** @var IURLGenerator */ |
||
| 61 | private $urlGenerator; |
||
| 62 | /** @var ICacheFactory */ |
||
| 63 | private $cacheFactory; |
||
| 64 | /** @var Util */ |
||
| 65 | private $util; |
||
| 66 | /** @var IAppManager */ |
||
| 67 | private $appManager; |
||
| 68 | /** @var INavigationManager */ |
||
| 69 | private $navigationManager; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | private $name; |
||
| 73 | /** @var string */ |
||
| 74 | private $title; |
||
| 75 | /** @var string */ |
||
| 76 | private $entity; |
||
| 77 | /** @var string */ |
||
| 78 | private $url; |
||
| 79 | /** @var string */ |
||
| 80 | private $color; |
||
| 81 | |||
| 82 | /** @var string */ |
||
| 83 | private $iTunesAppId; |
||
| 84 | /** @var string */ |
||
| 85 | private $iOSClientUrl; |
||
| 86 | /** @var string */ |
||
| 87 | private $AndroidClientUrl; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * ThemingDefaults constructor. |
||
| 91 | * |
||
| 92 | * @param IConfig $config |
||
| 93 | * @param IL10N $l |
||
| 94 | * @param ImageManager $imageManager |
||
| 95 | * @param IURLGenerator $urlGenerator |
||
| 96 | * @param ICacheFactory $cacheFactory |
||
| 97 | * @param Util $util |
||
| 98 | * @param IAppManager $appManager |
||
| 99 | */ |
||
| 100 | public function __construct(IConfig $config, |
||
| 127 | } |
||
| 128 | |||
| 129 | public function getName() { |
||
| 130 | return strip_tags($this->config->getAppValue('theming', 'name', $this->name)); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function getHTMLName() { |
||
| 134 | return $this->config->getAppValue('theming', 'name', $this->name); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function getTitle() { |
||
| 138 | return strip_tags($this->config->getAppValue('theming', 'name', $this->title)); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function getEntity() { |
||
| 142 | return strip_tags($this->config->getAppValue('theming', 'name', $this->entity)); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getBaseUrl() { |
||
| 146 | return $this->config->getAppValue('theming', 'url', $this->url); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function getSlogan(?string $lang = null) { |
||
| 150 | return \OCP\Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', parent::getSlogan($lang))); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getImprintUrl() { |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getPrivacyUrl() { |
||
| 158 | return (string)$this->config->getAppValue('theming', 'privacyUrl', ''); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function getShortFooter() { |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Color that is used for the header as well as for mail headers |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getColorPrimary() { |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Themed logo url |
||
| 221 | * |
||
| 222 | * @param bool $useSvg Whether to point to the SVG image or a fallback |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getLogo($useSvg = true): string { |
||
| 226 | $logo = $this->config->getAppValue('theming', 'logoMime', false); |
||
|
|
|||
| 227 | |||
| 228 | // short cut to avoid setting up the filesystem just to check if the logo is there |
||
| 229 | // |
||
| 230 | // explanation: if an SVG is requested and the app config value for logoMime is set then the logo is there. |
||
| 231 | // otherwise we need to check it and maybe also generate a PNG from the SVG (that's done in getImage() which |
||
| 232 | // needs to be called then) |
||
| 233 | if ($useSvg === true && $logo !== false) { |
||
| 234 | $logoExists = true; |
||
| 235 | } else { |
||
| 236 | try { |
||
| 237 | $this->imageManager->getImage('logo', $useSvg); |
||
| 238 | $logoExists = true; |
||
| 239 | } catch (\Exception $e) { |
||
| 240 | $logoExists = false; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 245 | |||
| 246 | if (!$logo || !$logoExists) { |
||
| 247 | if ($useSvg) { |
||
| 248 | $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg'); |
||
| 249 | } else { |
||
| 250 | $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png'); |
||
| 251 | } |
||
| 252 | return $logo . '?v=' . $cacheBusterCounter; |
||
| 253 | } |
||
| 254 | |||
| 255 | return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Themed background image url |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getBackground(): string { |
||
| 264 | return $this->imageManager->getImageUrl('background'); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getiTunesAppId() { |
||
| 271 | return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getiOSClientUrl() { |
||
| 278 | return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getAndroidClientUrl() { |
||
| 286 | } |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * @return array scss variables to overwrite |
||
| 291 | */ |
||
| 292 | public function getScssVariables() { |
||
| 293 | $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl()); |
||
| 294 | if ($value = $cache->get('getScssVariables')) { |
||
| 295 | return $value; |
||
| 296 | } |
||
| 297 | |||
| 298 | $variables = [ |
||
| 299 | 'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'", |
||
| 300 | 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'", |
||
| 301 | 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'", |
||
| 302 | 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'", |
||
| 303 | 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'" |
||
| 304 | ]; |
||
| 305 | |||
| 306 | $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')"; |
||
| 307 | $variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')"; |
||
| 308 | $variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')"; |
||
| 309 | $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')"; |
||
| 310 | $variables['image-login-plain'] = 'false'; |
||
| 311 | |||
| 312 | if ($this->config->getAppValue('theming', 'color', null) !== null) { |
||
| 313 | $variables['color-primary'] = $this->getColorPrimary(); |
||
| 314 | $variables['color-primary-text'] = $this->getTextColorPrimary(); |
||
| 315 | $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary()); |
||
| 316 | } |
||
| 317 | |||
| 318 | if ($this->config->getAppValue('theming', 'backgroundMime', null) === 'backgroundColor') { |
||
| 319 | $variables['image-login-plain'] = 'true'; |
||
| 320 | } |
||
| 321 | |||
| 322 | $variables['has-legal-links'] = 'false'; |
||
| 323 | if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') { |
||
| 324 | $variables['has-legal-links'] = 'true'; |
||
| 325 | } |
||
| 326 | |||
| 327 | $cache->set('getScssVariables', $variables); |
||
| 328 | return $variables; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Check if the image should be replaced by the theming app |
||
| 333 | * and return the new image location then |
||
| 334 | * |
||
| 335 | * @param string $app name of the app |
||
| 336 | * @param string $image filename of the image |
||
| 337 | * @return bool|string false if image should not replaced, otherwise the location of the image |
||
| 338 | */ |
||
| 339 | public function replaceImagePath($app, $image) { |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Increases the cache buster key |
||
| 381 | */ |
||
| 382 | private function increaseCacheBuster() { |
||
| 383 | $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 384 | $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey + 1); |
||
| 385 | $this->cacheFactory->createDistributed('theming-')->clear(); |
||
| 386 | $this->cacheFactory->createDistributed('imagePath')->clear(); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Update setting in the database |
||
| 391 | * |
||
| 392 | * @param string $setting |
||
| 393 | * @param string $value |
||
| 394 | */ |
||
| 395 | public function set($setting, $value) { |
||
| 396 | $this->config->setAppValue('theming', $setting, $value); |
||
| 397 | $this->increaseCacheBuster(); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Revert settings to the default value |
||
| 402 | * |
||
| 403 | * @param string $setting setting which should be reverted |
||
| 404 | * @return string default value |
||
| 405 | */ |
||
| 406 | public function undo($setting) { |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Color of text in the header and primary buttons |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getTextColorPrimary() { |
||
| 442 | } |
||
| 443 | } |
||
| 444 |