Passed
Push — master ( f0238f...56ece0 )
by Joas
15:37 queued 13s
created

OC_Defaults   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 307
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 130
c 1
b 0
f 0
dl 0
loc 307
rs 8.4
wmc 50

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getAndroidClientUrl() 0 5 2
A getName() 0 5 2
A getiOSClientUrl() 0 5 2
A getHTMLName() 0 5 2
A getiTunesAppId() 0 5 2
A getSyncClientUrl() 0 5 2
A getDocBaseUrl() 0 5 2
A getBaseUrl() 0 5 2
A themeExist() 0 5 3
A getTitle() 0 5 2
A getEntity() 0 5 2
A getSlogan() 0 9 3
A getFDroidClientUrl() 0 5 2
A __construct() 0 26 3
A getScssVariables() 0 5 2
A getProductName() 0 5 2
A getShortFooter() 0 10 2
A getColorPrimary() 0 8 3
A buildDocLinkToKey() 0 5 2
A shouldReplaceIcons() 0 2 1
A getTextColorPrimary() 0 5 2
A getLongFooter() 0 8 2
A getLogo() 0 11 3

How to fix   Complexity   

Complex Class

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
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Daniel Kesselberg <[email protected]>
8
 * @author Jan-Christoph Borchardt <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Julius Haertl <[email protected]>
11
 * @author Julius Härtl <[email protected]>
12
 * @author Lukas Reschke <[email protected]>
13
 * @author Markus Staab <[email protected]>
14
 * @author Michael Weimann <[email protected]>
15
 * @author Morris Jobke <[email protected]>
16
 * @author Pascal de Bruijn <[email protected]>
17
 * @author Robin Appelman <[email protected]>
18
 * @author Robin McCorkell <[email protected]>
19
 * @author Roeland Jago Douma <[email protected]>
20
 * @author scolebrook <[email protected]>
21
 * @author Thomas Müller <[email protected]>
22
 * @author Volkan Gezer <[email protected]>
23
 *
24
 * @license AGPL-3.0
25
 *
26
 * This code is free software: you can redistribute it and/or modify
27
 * it under the terms of the GNU Affero General Public License, version 3,
28
 * as published by the Free Software Foundation.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33
 * GNU Affero General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU Affero General Public License, version 3,
36
 * along with this program. If not, see <http://www.gnu.org/licenses/>
37
 *
38
 */
39
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 $defaultFDroidClientUrl;
52
	private $defaultDocBaseUrl;
53
	private $defaultDocVersion;
54
	private $defaultSlogan;
55
	private $defaultColorPrimary;
56
	private $defaultTextColorPrimary;
57
	private $defaultProductName;
58
59
	public function __construct() {
60
		$config = \OC::$server->getConfig();
61
62
		$this->defaultEntity = 'Nextcloud'; /* e.g. company name, used for footers and copyright notices */
63
		$this->defaultName = 'Nextcloud'; /* short name, used when referring to the software */
64
		$this->defaultTitle = 'Nextcloud'; /* can be a longer name, for titles */
65
		$this->defaultBaseUrl = 'https://nextcloud.com';
66
		$this->defaultSyncClientUrl = $config->getSystemValue('customclient_desktop', 'https://nextcloud.com/install/#install-clients');
67
		$this->defaultiOSClientUrl = $config->getSystemValue('customclient_ios', 'https://geo.itunes.apple.com/us/app/nextcloud/id1125420102?mt=8');
68
		$this->defaultiTunesAppId = $config->getSystemValue('customclient_ios_appid', '1125420102');
69
		$this->defaultAndroidClientUrl = $config->getSystemValue('customclient_android', 'https://play.google.com/store/apps/details?id=com.nextcloud.client');
70
		$this->defaultFDroidClientUrl = $config->getSystemValue('customclient_fdroid', 'https://f-droid.org/packages/com.nextcloud.client/');
71
		$this->defaultDocBaseUrl = 'https://docs.nextcloud.com';
72
		$this->defaultDocVersion = \OC_Util::getVersion()[0]; // used to generate doc links
73
		$this->defaultColorPrimary = '#0082c9';
74
		$this->defaultTextColorPrimary = '#ffffff';
75
		$this->defaultProductName = 'Nextcloud';
76
77
		$themePath = OC::$SERVERROOT . '/themes/' . OC_Util::getTheme() . '/defaults.php';
78
		if (file_exists($themePath)) {
79
			// prevent defaults.php from printing output
80
			ob_start();
81
			require_once $themePath;
82
			ob_end_clean();
83
			if (class_exists('OC_Theme')) {
84
				$this->theme = new OC_Theme();
85
			}
86
		}
87
	}
88
89
	/**
90
	 * @param string $method
91
	 */
92
	private function themeExist($method) {
93
		if (isset($this->theme) && method_exists($this->theme, $method)) {
94
			return true;
95
		}
96
		return false;
97
	}
98
99
	/**
100
	 * Returns the base URL
101
	 * @return string URL
102
	 */
103
	public function getBaseUrl() {
104
		if ($this->themeExist('getBaseUrl')) {
105
			return $this->theme->getBaseUrl();
106
		} else {
107
			return $this->defaultBaseUrl;
108
		}
109
	}
110
111
	/**
112
	 * Returns the URL where the sync clients are listed
113
	 * @return string URL
114
	 */
115
	public function getSyncClientUrl() {
116
		if ($this->themeExist('getSyncClientUrl')) {
117
			return $this->theme->getSyncClientUrl();
118
		} else {
119
			return $this->defaultSyncClientUrl;
120
		}
121
	}
122
123
	/**
124
	 * Returns the URL to the App Store for the iOS Client
125
	 * @return string URL
126
	 */
127
	public function getiOSClientUrl() {
128
		if ($this->themeExist('getiOSClientUrl')) {
129
			return $this->theme->getiOSClientUrl();
130
		} else {
131
			return $this->defaultiOSClientUrl;
132
		}
133
	}
134
135
	/**
136
	 * Returns the AppId for the App Store for the iOS Client
137
	 * @return string AppId
138
	 */
139
	public function getiTunesAppId() {
140
		if ($this->themeExist('getiTunesAppId')) {
141
			return $this->theme->getiTunesAppId();
142
		} else {
143
			return $this->defaultiTunesAppId;
144
		}
145
	}
146
147
	/**
148
	 * Returns the URL to Google Play for the Android Client
149
	 * @return string URL
150
	 */
151
	public function getAndroidClientUrl() {
152
		if ($this->themeExist('getAndroidClientUrl')) {
153
			return $this->theme->getAndroidClientUrl();
154
		} else {
155
			return $this->defaultAndroidClientUrl;
156
		}
157
	}
158
159
	/**
160
	 * Returns the URL to Google Play for the Android Client
161
	 * @return string URL
162
	 */
163
	public function getFDroidClientUrl() {
164
		if ($this->themeExist('getFDroidClientUrl')) {
165
			return $this->theme->getFDroidClientUrl();
166
		} else {
167
			return $this->defaultFDroidClientUrl;
168
		}
169
	}
170
171
	/**
172
	 * Returns the documentation URL
173
	 * @return string URL
174
	 */
175
	public function getDocBaseUrl() {
176
		if ($this->themeExist('getDocBaseUrl')) {
177
			return $this->theme->getDocBaseUrl();
178
		} else {
179
			return $this->defaultDocBaseUrl;
180
		}
181
	}
182
183
	/**
184
	 * Returns the title
185
	 * @return string title
186
	 */
187
	public function getTitle() {
188
		if ($this->themeExist('getTitle')) {
189
			return $this->theme->getTitle();
190
		} else {
191
			return $this->defaultTitle;
192
		}
193
	}
194
195
	/**
196
	 * Returns the short name of the software
197
	 * @return string title
198
	 */
199
	public function getName() {
200
		if ($this->themeExist('getName')) {
201
			return $this->theme->getName();
202
		} else {
203
			return $this->defaultName;
204
		}
205
	}
206
207
	/**
208
	 * Returns the short name of the software containing HTML strings
209
	 * @return string title
210
	 */
211
	public function getHTMLName() {
212
		if ($this->themeExist('getHTMLName')) {
213
			return $this->theme->getHTMLName();
214
		} else {
215
			return $this->defaultName;
216
		}
217
	}
218
219
	/**
220
	 * Returns entity (e.g. company name) - used for footer, copyright
221
	 * @return string entity name
222
	 */
223
	public function getEntity() {
224
		if ($this->themeExist('getEntity')) {
225
			return $this->theme->getEntity();
226
		} else {
227
			return $this->defaultEntity;
228
		}
229
	}
230
231
	/**
232
	 * Returns slogan
233
	 * @return string slogan
234
	 */
235
	public function getSlogan(?string $lang = null) {
236
		if ($this->themeExist('getSlogan')) {
237
			return $this->theme->getSlogan($lang);
238
		} else {
239
			if ($this->defaultSlogan === null) {
240
				$l10n = \OC::$server->getL10N('lib', $lang);
241
				$this->defaultSlogan = $l10n->t('a safe home for all your data');
242
			}
243
			return $this->defaultSlogan;
244
		}
245
	}
246
247
	/**
248
	 * Returns short version of the footer
249
	 * @return string short footer
250
	 */
251
	public function getShortFooter() {
252
		if ($this->themeExist('getShortFooter')) {
253
			$footer = $this->theme->getShortFooter();
254
		} else {
255
			$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
256
				' rel="noreferrer noopener">' .$this->getEntity() . '</a>'.
257
				' – ' . $this->getSlogan();
258
		}
259
260
		return $footer;
261
	}
262
263
	/**
264
	 * Returns long version of the footer
265
	 * @return string long footer
266
	 */
267
	public function getLongFooter() {
268
		if ($this->themeExist('getLongFooter')) {
269
			$footer = $this->theme->getLongFooter();
270
		} else {
271
			$footer = $this->getShortFooter();
272
		}
273
274
		return $footer;
275
	}
276
277
	/**
278
	 * @param string $key
279
	 * @return string URL to doc with key
280
	 */
281
	public function buildDocLinkToKey($key) {
282
		if ($this->themeExist('buildDocLinkToKey')) {
283
			return $this->theme->buildDocLinkToKey($key);
284
		}
285
		return $this->getDocBaseUrl() . '/server/' . $this->defaultDocVersion . '/go.php?to=' . $key;
286
	}
287
288
	/**
289
	 * Returns primary color
290
	 * @return string
291
	 */
292
	public function getColorPrimary() {
293
		if ($this->themeExist('getColorPrimary')) {
294
			return $this->theme->getColorPrimary();
295
		}
296
		if ($this->themeExist('getMailHeaderColor')) {
297
			return $this->theme->getMailHeaderColor();
298
		}
299
		return $this->defaultColorPrimary;
300
	}
301
302
	/**
303
	 * @return array scss variables to overwrite
304
	 */
305
	public function getScssVariables() {
306
		if ($this->themeExist('getScssVariables')) {
307
			return $this->theme->getScssVariables();
308
		}
309
		return [];
310
	}
311
312
	public function shouldReplaceIcons() {
313
		return false;
314
	}
315
316
	/**
317
	 * Themed logo url
318
	 *
319
	 * @param bool $useSvg Whether to point to the SVG image or a fallback
320
	 * @return string
321
	 */
322
	public function getLogo($useSvg = true) {
323
		if ($this->themeExist('getLogo')) {
324
			return $this->theme->getLogo($useSvg);
325
		}
326
327
		if ($useSvg) {
328
			$logo = \OC::$server->getURLGenerator()->imagePath('core', 'logo/logo.svg');
329
		} else {
330
			$logo = \OC::$server->getURLGenerator()->imagePath('core', 'logo/logo.png');
331
		}
332
		return $logo . '?v=' . hash('sha1', implode('.', \OCP\Util::getVersion()));
333
	}
334
335
	public function getTextColorPrimary() {
336
		if ($this->themeExist('getTextColorPrimary')) {
337
			return $this->theme->getTextColorPrimary();
338
		}
339
		return $this->defaultTextColorPrimary;
340
	}
341
342
	public function getProductName() {
343
		if ($this->themeExist('getProductName')) {
344
			return $this->theme->getProductName();
345
		}
346
		return $this->defaultProductName;
347
	}
348
}
349