| Total Complexity | 62 |
| Total Lines | 381 |
| 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() { |
||
| 162 | $slogan = $this->getSlogan(); |
||
| 163 | $baseUrl = $this->getBaseUrl(); |
||
| 164 | if ($baseUrl !== '') { |
||
| 165 | $footer = '<a href="' . $baseUrl . '" target="_blank"' . |
||
| 166 | ' rel="noreferrer noopener" class="entity-name">' . $this->getEntity() . '</a>'; |
||
| 167 | } else { |
||
| 168 | $footer = '<span class="entity-name">' .$this->getEntity() . '</span>'; |
||
| 169 | } |
||
| 170 | $footer .= ($slogan !== '' ? ' – ' . $slogan : ''); |
||
| 171 | |||
| 172 | $links = [ |
||
| 173 | [ |
||
| 174 | 'text' => $this->l->t('Legal notice'), |
||
| 175 | 'url' => (string)$this->getImprintUrl() |
||
| 176 | ], |
||
| 177 | [ |
||
| 178 | 'text' => $this->l->t('Privacy policy'), |
||
| 179 | 'url' => (string)$this->getPrivacyUrl() |
||
| 180 | ], |
||
| 181 | ]; |
||
| 182 | |||
| 183 | $navigation = $this->navigationManager->getAll(INavigationManager::TYPE_GUEST); |
||
| 184 | $guestNavigation = array_map(function ($nav) { |
||
| 185 | return [ |
||
| 186 | 'text' => $nav['name'], |
||
| 187 | 'url' => $nav['href'] |
||
| 188 | ]; |
||
| 189 | }, $navigation); |
||
| 190 | $links = array_merge($links, $guestNavigation); |
||
| 191 | |||
| 192 | $legalLinks = ''; |
||
| 193 | $divider = ''; |
||
| 194 | foreach ($links as $link) { |
||
| 195 | if ($link['url'] !== '' |
||
| 196 | && filter_var($link['url'], FILTER_VALIDATE_URL) |
||
| 197 | ) { |
||
| 198 | $legalLinks .= $divider . '<a href="' . $link['url'] . '" class="legal" target="_blank"' . |
||
| 199 | ' rel="noreferrer noopener">' . $link['text'] . '</a>'; |
||
| 200 | $divider = ' · '; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | if ($legalLinks !== '') { |
||
| 204 | $footer .= '<br/>' . $legalLinks; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $footer; |
||
| 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() { |
||
| 216 | return $this->config->getAppValue('theming', 'color', $this->color); |
||
| 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 | $logoExists = true; |
||
| 229 | try { |
||
| 230 | $this->imageManager->getImage('logo', $useSvg); |
||
| 231 | } catch (\Exception $e) { |
||
| 232 | $logoExists = false; |
||
| 233 | } |
||
| 234 | |||
| 235 | $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 236 | |||
| 237 | if (!$logo || !$logoExists) { |
||
| 238 | if ($useSvg) { |
||
| 239 | $logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg'); |
||
| 240 | } else { |
||
| 241 | $logo = $this->urlGenerator->imagePath('core', 'logo/logo.png'); |
||
| 242 | } |
||
| 243 | return $logo . '?v=' . $cacheBusterCounter; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Themed background image url |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getBackground(): string { |
||
| 255 | return $this->imageManager->getImageUrl('background'); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function getiTunesAppId() { |
||
| 262 | return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getiOSClientUrl() { |
||
| 269 | return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function getAndroidClientUrl() { |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array scss variables to overwrite |
||
| 282 | */ |
||
| 283 | public function getScssVariables() { |
||
| 284 | $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl()); |
||
| 285 | if ($value = $cache->get('getScssVariables')) { |
||
| 286 | return $value; |
||
| 287 | } |
||
| 288 | |||
| 289 | $variables = [ |
||
| 290 | 'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'", |
||
| 291 | 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'", |
||
| 292 | 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'", |
||
| 293 | 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'", |
||
| 294 | 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'" |
||
| 295 | ]; |
||
| 296 | |||
| 297 | $variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')"; |
||
| 298 | $variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')"; |
||
| 299 | $variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')"; |
||
| 300 | $variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')"; |
||
| 301 | $variables['image-login-plain'] = 'false'; |
||
| 302 | |||
| 303 | if ($this->config->getAppValue('theming', 'color', null) !== null) { |
||
| 304 | $variables['color-primary'] = $this->getColorPrimary(); |
||
| 305 | $variables['color-primary-text'] = $this->getTextColorPrimary(); |
||
| 306 | $variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary()); |
||
| 307 | } |
||
| 308 | |||
| 309 | if ($this->config->getAppValue('theming', 'backgroundMime', null) === 'backgroundColor') { |
||
| 310 | $variables['image-login-plain'] = 'true'; |
||
| 311 | } |
||
| 312 | |||
| 313 | $variables['has-legal-links'] = 'false'; |
||
| 314 | if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') { |
||
| 315 | $variables['has-legal-links'] = 'true'; |
||
| 316 | } |
||
| 317 | |||
| 318 | $cache->set('getScssVariables', $variables); |
||
| 319 | return $variables; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Check if the image should be replaced by the theming app |
||
| 324 | * and return the new image location then |
||
| 325 | * |
||
| 326 | * @param string $app name of the app |
||
| 327 | * @param string $image filename of the image |
||
| 328 | * @return bool|string false if image should not replaced, otherwise the location of the image |
||
| 329 | */ |
||
| 330 | public function replaceImagePath($app, $image) { |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Increases the cache buster key |
||
| 372 | */ |
||
| 373 | private function increaseCacheBuster() { |
||
| 374 | $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
| 375 | $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1); |
||
| 376 | $this->cacheFactory->createDistributed('theming-')->clear(); |
||
| 377 | $this->cacheFactory->createDistributed('imagePath')->clear(); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Update setting in the database |
||
| 382 | * |
||
| 383 | * @param string $setting |
||
| 384 | * @param string $value |
||
| 385 | */ |
||
| 386 | public function set($setting, $value) { |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Revert settings to the default value |
||
| 393 | * |
||
| 394 | * @param string $setting setting which should be reverted |
||
| 395 | * @return string default value |
||
| 396 | */ |
||
| 397 | public function undo($setting) { |
||
| 398 | $this->config->deleteAppValue('theming', $setting); |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Color of text in the header and primary buttons |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getTextColorPrimary() { |
||
| 435 |