| Total Complexity | 49 |
| Total Lines | 302 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OC_Defaults 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 OC_Defaults, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class OC_Defaults { |
||
| 41 | private $theme; |
||
| 42 | |||
| 43 | private $defaultEntity; |
||
| 44 | private $defaultName; |
||
| 45 | private $defaultTitle; |
||
| 46 | private $defaultBaseUrl; |
||
| 47 | private $defaultSyncClientUrl; |
||
| 48 | private $defaultiOSClientUrl; |
||
| 49 | private $defaultiTunesAppId; |
||
| 50 | private $defaultAndroidClientUrl; |
||
| 51 | private $defaultDocBaseUrl; |
||
| 52 | private $defaultDocVersion; |
||
| 53 | private $defaultSlogan; |
||
| 54 | private $defaultColorPrimary; |
||
| 55 | private $defaultTextColorPrimary; |
||
| 56 | private $defaultProductName; |
||
| 57 | |||
| 58 | public function __construct() { |
||
| 59 | $config = \OC::$server->getConfig(); |
||
| 60 | |||
| 61 | $this->defaultEntity = 'Nextcloud'; /* e.g. company name, used for footers and copyright notices */ |
||
| 62 | $this->defaultName = 'Nextcloud'; /* short name, used when referring to the software */ |
||
| 63 | $this->defaultTitle = 'Nextcloud'; /* can be a longer name, for titles */ |
||
| 64 | $this->defaultBaseUrl = 'https://nextcloud.com'; |
||
| 65 | $this->defaultSyncClientUrl = $config->getSystemValue('customclient_desktop', 'https://nextcloud.com/install/#install-clients'); |
||
| 66 | $this->defaultiOSClientUrl = $config->getSystemValue('customclient_ios', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8'); |
||
| 67 | $this->defaultiTunesAppId = $config->getSystemValue('customclient_ios_appid', '1125420102'); |
||
| 68 | $this->defaultAndroidClientUrl = $config->getSystemValue('customclient_android', 'https://play.google.com/store/apps/details?id=com.nextcloud.client'); |
||
| 69 | $this->defaultDocBaseUrl = 'https://docs.nextcloud.com'; |
||
| 70 | $this->defaultDocVersion = \OC_Util::getVersion()[0]; // used to generate doc links |
||
| 71 | $this->defaultColorPrimary = '#0082c9'; |
||
| 72 | $this->defaultTextColorPrimary = '#ffffff'; |
||
| 73 | $this->defaultProductName = 'Nextcloud'; |
||
| 74 | |||
| 75 | $themePath = OC::$SERVERROOT . '/themes/' . OC_Util::getTheme() . '/defaults.php'; |
||
| 76 | if (file_exists($themePath)) { |
||
| 77 | // prevent defaults.php from printing output |
||
| 78 | ob_start(); |
||
| 79 | require_once $themePath; |
||
| 80 | ob_end_clean(); |
||
| 81 | if (class_exists('OC_Theme')) { |
||
| 82 | $this->theme = new OC_Theme(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $method |
||
| 89 | */ |
||
| 90 | private function themeExist($method) { |
||
| 91 | if (isset($this->theme) && method_exists($this->theme, $method)) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the base URL |
||
| 99 | * @return string URL |
||
| 100 | */ |
||
| 101 | public function getBaseUrl() { |
||
| 102 | if ($this->themeExist('getBaseUrl')) { |
||
| 103 | return $this->theme->getBaseUrl(); |
||
| 104 | } else { |
||
| 105 | return $this->defaultBaseUrl; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the URL where the sync clients are listed |
||
| 111 | * @return string URL |
||
| 112 | */ |
||
| 113 | public function getSyncClientUrl() { |
||
| 114 | if ($this->themeExist('getSyncClientUrl')) { |
||
| 115 | return $this->theme->getSyncClientUrl(); |
||
| 116 | } else { |
||
| 117 | return $this->defaultSyncClientUrl; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the URL to the App Store for the iOS Client |
||
| 123 | * @return string URL |
||
| 124 | */ |
||
| 125 | public function getiOSClientUrl() { |
||
| 126 | if ($this->themeExist('getiOSClientUrl')) { |
||
| 127 | return $this->theme->getiOSClientUrl(); |
||
| 128 | } else { |
||
| 129 | return $this->defaultiOSClientUrl; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns the AppId for the App Store for the iOS Client |
||
| 135 | * @return string AppId |
||
| 136 | */ |
||
| 137 | public function getiTunesAppId() { |
||
| 138 | if ($this->themeExist('getiTunesAppId')) { |
||
| 139 | return $this->theme->getiTunesAppId(); |
||
| 140 | } else { |
||
| 141 | return $this->defaultiTunesAppId; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the URL to Google Play for the Android Client |
||
| 147 | * @return string URL |
||
| 148 | */ |
||
| 149 | public function getAndroidClientUrl() { |
||
| 150 | if ($this->themeExist('getAndroidClientUrl')) { |
||
| 151 | return $this->theme->getAndroidClientUrl(); |
||
| 152 | } else { |
||
| 153 | return $this->defaultAndroidClientUrl; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the documentation URL |
||
| 159 | * @return string URL |
||
| 160 | */ |
||
| 161 | public function getDocBaseUrl() { |
||
| 162 | if ($this->themeExist('getDocBaseUrl')) { |
||
| 163 | return $this->theme->getDocBaseUrl(); |
||
| 164 | } else { |
||
| 165 | return $this->defaultDocBaseUrl; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the title |
||
| 171 | * @return string title |
||
| 172 | */ |
||
| 173 | public function getTitle() { |
||
| 174 | if ($this->themeExist('getTitle')) { |
||
| 175 | return $this->theme->getTitle(); |
||
| 176 | } else { |
||
| 177 | return $this->defaultTitle; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the short name of the software |
||
| 183 | * @return string title |
||
| 184 | */ |
||
| 185 | public function getName() { |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the short name of the software containing HTML strings |
||
| 195 | * @return string title |
||
| 196 | */ |
||
| 197 | public function getHTMLName() { |
||
| 198 | if ($this->themeExist('getHTMLName')) { |
||
| 199 | return $this->theme->getHTMLName(); |
||
| 200 | } else { |
||
| 201 | return $this->defaultName; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns entity (e.g. company name) - used for footer, copyright |
||
| 207 | * @return string entity name |
||
| 208 | */ |
||
| 209 | public function getEntity() { |
||
| 210 | if ($this->themeExist('getEntity')) { |
||
| 211 | return $this->theme->getEntity(); |
||
| 212 | } else { |
||
| 213 | return $this->defaultEntity; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns slogan |
||
| 219 | * @return string slogan |
||
| 220 | */ |
||
| 221 | public function getSlogan(?string $lang = null) { |
||
| 222 | if ($this->themeExist('getSlogan')) { |
||
| 223 | return $this->theme->getSlogan($lang); |
||
| 224 | } else { |
||
| 225 | if ($this->defaultSlogan === null) { |
||
| 226 | $l10n = \OC::$server->getL10N('lib', $lang); |
||
| 227 | $this->defaultSlogan = $l10n->t('a safe home for all your data'); |
||
| 228 | } |
||
| 229 | return $this->defaultSlogan; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns logo claim |
||
| 235 | * @return string logo claim |
||
| 236 | * @deprecated 13.0.0 |
||
| 237 | */ |
||
| 238 | public function getLogoClaim() { |
||
| 239 | return ''; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns short version of the footer |
||
| 244 | * @return string short footer |
||
| 245 | */ |
||
| 246 | public function getShortFooter() { |
||
| 247 | if ($this->themeExist('getShortFooter')) { |
||
| 248 | $footer = $this->theme->getShortFooter(); |
||
| 249 | } else { |
||
| 250 | $footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' . |
||
| 251 | ' rel="noreferrer noopener">' .$this->getEntity() . '</a>'. |
||
| 252 | ' – ' . $this->getSlogan(); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $footer; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns long version of the footer |
||
| 260 | * @return string long footer |
||
| 261 | */ |
||
| 262 | public function getLongFooter() { |
||
| 263 | if ($this->themeExist('getLongFooter')) { |
||
| 264 | $footer = $this->theme->getLongFooter(); |
||
| 265 | } else { |
||
| 266 | $footer = $this->getShortFooter(); |
||
| 267 | } |
||
| 268 | |||
| 269 | return $footer; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $key |
||
| 274 | * @return string URL to doc with key |
||
| 275 | */ |
||
| 276 | public function buildDocLinkToKey($key) { |
||
| 277 | if ($this->themeExist('buildDocLinkToKey')) { |
||
| 278 | return $this->theme->buildDocLinkToKey($key); |
||
| 279 | } |
||
| 280 | return $this->getDocBaseUrl() . '/server/' . $this->defaultDocVersion . '/go.php?to=' . $key; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns primary color |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getColorPrimary() { |
||
| 288 | if ($this->themeExist('getColorPrimary')) { |
||
| 289 | return $this->theme->getColorPrimary(); |
||
| 290 | } |
||
| 291 | if ($this->themeExist('getMailHeaderColor')) { |
||
| 292 | return $this->theme->getMailHeaderColor(); |
||
| 293 | } |
||
| 294 | return $this->defaultColorPrimary; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return array scss variables to overwrite |
||
| 299 | */ |
||
| 300 | public function getScssVariables() { |
||
| 305 | } |
||
| 306 | |||
| 307 | public function shouldReplaceIcons() { |
||
| 308 | return false; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Themed logo url |
||
| 313 | * |
||
| 314 | * @param bool $useSvg Whether to point to the SVG image or a fallback |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getLogo($useSvg = true) { |
||
| 318 | if ($this->themeExist('getLogo')) { |
||
| 319 | return $this->theme->getLogo($useSvg); |
||
| 320 | } |
||
| 321 | |||
| 322 | if ($useSvg) { |
||
| 323 | $logo = \OC::$server->getURLGenerator()->imagePath('core', 'logo/logo.svg'); |
||
| 324 | } else { |
||
| 325 | $logo = \OC::$server->getURLGenerator()->imagePath('core', 'logo/logo.png'); |
||
| 326 | } |
||
| 327 | return $logo . '?v=' . hash('sha1', implode('.', \OCP\Util::getVersion())); |
||
| 328 | } |
||
| 329 | |||
| 330 | public function getTextColorPrimary() { |
||
| 331 | if ($this->themeExist('getTextColorPrimary')) { |
||
| 332 | return $this->theme->getTextColorPrimary(); |
||
| 333 | } |
||
| 334 | return $this->defaultTextColorPrimary; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function getProductName() { |
||
| 342 | } |
||
| 343 | } |
||
| 344 |