Completed
Push — master ( 5ca5eb...afb5d4 )
by Lukas
16:52
created

OC_Defaults   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 279
rs 8.295
c 0
b 0
f 0
wmc 42
lcom 1
cbo 7

20 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 30 3
A themeExist() 0 6 3
A getBaseUrl() 0 7 2
A getSyncClientUrl() 0 7 2
A getiOSClientUrl() 0 7 2
A getiTunesAppId() 0 7 2
A getAndroidClientUrl() 0 7 2
A getDocBaseUrl() 0 7 2
A getTitle() 0 7 2
A getName() 0 7 2
A getHTMLName() 0 7 2
A getEntity() 0 7 2
A getSlogan() 0 7 2
A getLogoClaim() 0 7 2
A getShortFooter() 0 11 2
A getLongFooter() 0 9 2
A buildDocLinkToKey() 0 6 2
A getColorPrimary() 0 10 3
A shouldReplaceIcons() 0 3 1
A getLogo() 0 7 2

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Jan-Christoph Borchardt <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Pascal de Bruijn <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Robin McCorkell <[email protected]>
13
 * @author scolebrook <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Volkan Gezer <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
class OC_Defaults {
33
34
	private $theme;
35
	private $l;
36
37
	private $defaultEntity;
38
	private $defaultName;
39
	private $defaultTitle;
40
	private $defaultBaseUrl;
41
	private $defaultSyncClientUrl;
42
	private $defaultiOSClientUrl;
43
	private $defaultiTunesAppId;
44
	private $defaultAndroidClientUrl;
45
	private $defaultDocBaseUrl;
46
	private $defaultDocVersion;
47
	private $defaultSlogan;
48
	private $defaultLogoClaim;
49
	private $defaultColorPrimary;
50
	private $defaultLogoUrl;
51
	private $defaultCacheBuster;
0 ignored issues
show
Unused Code introduced by
The property $defaultCacheBuster is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
53
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
		$this->l = \OC::$server->getL10N('lib');
55
56
		$this->defaultEntity = 'Nextcloud'; /* e.g. company name, used for footers and copyright notices */
57
		$this->defaultName = 'Nextcloud'; /* short name, used when referring to the software */
58
		$this->defaultTitle = 'Nextcloud'; /* can be a longer name, for titles */
59
		$this->defaultBaseUrl = 'https://nextcloud.com';
60
		$this->defaultSyncClientUrl = 'https://nextcloud.com/install/#install-clients';
61
		$this->defaultiOSClientUrl = 'https://itunes.apple.com/us/app/nextcloud/id1125420102?mt=8';
62
		$this->defaultiTunesAppId = '1125420102';
63
		$this->defaultAndroidClientUrl = 'https://play.google.com/store/apps/details?id=com.nextcloud.client';
64
		$this->defaultDocBaseUrl = 'https://docs.nextcloud.com';
65
		$this->defaultDocVersion = '11'; // used to generate doc links
66
		$this->defaultSlogan = $this->l->t('a safe home for all your data');
67
		$this->defaultLogoClaim = '';
68
		$this->defaultColorPrimary = '#0082c9';
69
		$this->defaultLogoUrl = \OC::$server->getURLGenerator()->imagePath('core','logo.svg');
70
		$this->defaultLogoUrl .=  '?v=' . hash('sha1', implode('.', \OCP\Util::getVersion()));
71
72
		$themePath = OC::$SERVERROOT . '/themes/' . OC_Util::getTheme() . '/defaults.php';
73
		if (file_exists($themePath)) {
74
			// prevent defaults.php from printing output
75
			ob_start();
76
			require_once $themePath;
77
			ob_end_clean();
78
			if (class_exists('OC_Theme')) {
79
				$this->theme = new OC_Theme();
80
			}
81
		}
82
	}
83
84
	/**
85
	 * @param string $method
86
	 */
87
	private function themeExist($method) {
88
		if (isset($this->theme) && method_exists($this->theme, $method)) {
89
			return true;
90
		}
91
		return false;
92
	}
93
94
	/**
95
	 * Returns the base URL
96
	 * @return string URL
97
	 */
98
	public function getBaseUrl() {
99
		if ($this->themeExist('getBaseUrl')) {
100
			return $this->theme->getBaseUrl();
101
		} else {
102
			return $this->defaultBaseUrl;
103
		}
104
	}
105
106
	/**
107
	 * Returns the URL where the sync clients are listed
108
	 * @return string URL
109
	 */
110
	public function getSyncClientUrl() {
111
		if ($this->themeExist('getSyncClientUrl')) {
112
			return $this->theme->getSyncClientUrl();
113
		} else {
114
			return $this->defaultSyncClientUrl;
115
		}
116
	}
117
118
	/**
119
	 * Returns the URL to the App Store for the iOS Client
120
	 * @return string URL
121
	 */
122
	public function getiOSClientUrl() {
123
		if ($this->themeExist('getiOSClientUrl')) {
124
			return $this->theme->getiOSClientUrl();
125
		} else {
126
			return $this->defaultiOSClientUrl;
127
		}
128
	}
129
130
	/**
131
	 * Returns the AppId for the App Store for the iOS Client
132
	 * @return string AppId
133
	 */
134
	public function getiTunesAppId() {
135
		if ($this->themeExist('getiTunesAppId')) {
136
			return $this->theme->getiTunesAppId();
137
		} else {
138
			return $this->defaultiTunesAppId;
139
		}
140
	}
141
142
	/**
143
	 * Returns the URL to Google Play for the Android Client
144
	 * @return string URL
145
	 */
146
	public function getAndroidClientUrl() {
147
		if ($this->themeExist('getAndroidClientUrl')) {
148
			return $this->theme->getAndroidClientUrl();
149
		} else {
150
			return $this->defaultAndroidClientUrl;
151
		}
152
	}
153
154
	/**
155
	 * Returns the documentation URL
156
	 * @return string URL
157
	 */
158
	public function getDocBaseUrl() {
159
		if ($this->themeExist('getDocBaseUrl')) {
160
			return $this->theme->getDocBaseUrl();
161
		} else {
162
			return $this->defaultDocBaseUrl;
163
		}
164
	}
165
166
	/**
167
	 * Returns the title
168
	 * @return string title
169
	 */
170
	public function getTitle() {
171
		if ($this->themeExist('getTitle')) {
172
			return $this->theme->getTitle();
173
		} else {
174
			return $this->defaultTitle;
175
		}
176
	}
177
178
	/**
179
	 * Returns the short name of the software
180
	 * @return string title
181
	 */
182
	public function getName() {
183
		if ($this->themeExist('getName')) {
184
			return $this->theme->getName();
185
		} else {
186
			return $this->defaultName;
187
		}
188
	}
189
190
	/**
191
	 * Returns the short name of the software containing HTML strings
192
	 * @return string title
193
	 */
194
	public function getHTMLName() {
195
		if ($this->themeExist('getHTMLName')) {
196
			return $this->theme->getHTMLName();
197
		} else {
198
			return $this->defaultName;
199
		}
200
	}
201
202
	/**
203
	 * Returns entity (e.g. company name) - used for footer, copyright
204
	 * @return string entity name
205
	 */
206
	public function getEntity() {
207
		if ($this->themeExist('getEntity')) {
208
			return $this->theme->getEntity();
209
		} else {
210
			return $this->defaultEntity;
211
		}
212
	}
213
214
	/**
215
	 * Returns slogan
216
	 * @return string slogan
217
	 */
218
	public function getSlogan() {
219
		if ($this->themeExist('getSlogan')) {
220
			return $this->theme->getSlogan();
221
		} else {
222
			return $this->defaultSlogan;
223
		}
224
	}
225
226
	/**
227
	 * Returns logo claim
228
	 * @return string logo claim
229
	 */
230
	public function getLogoClaim() {
231
		if ($this->themeExist('getLogoClaim')) {
232
			return $this->theme->getLogoClaim();
233
		} else {
234
			return $this->defaultLogoClaim;
235
		}
236
	}
237
238
	/**
239
	 * Returns short version of the footer
240
	 * @return string short footer
241
	 */
242
	public function getShortFooter() {
243
		if ($this->themeExist('getShortFooter')) {
244
			$footer = $this->theme->getShortFooter();
245
		} else {
246
			$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
247
				' rel="noreferrer">' .$this->getEntity() . '</a>'.
248
				' – ' . $this->getSlogan();
249
		}
250
251
		return $footer;
252
	}
253
254
	/**
255
	 * Returns long version of the footer
256
	 * @return string long footer
257
	 */
258
	public function getLongFooter() {
259
		if ($this->themeExist('getLongFooter')) {
260
			$footer = $this->theme->getLongFooter();
261
		} else {
262
			$footer = $this->getShortFooter();
263
		}
264
265
		return $footer;
266
	}
267
268
	/**
269
	 * @param string $key
270
	 * @return string URL to doc with key
271
	 */
272
	public function buildDocLinkToKey($key) {
273
		if ($this->themeExist('buildDocLinkToKey')) {
274
			return $this->theme->buildDocLinkToKey($key);
275
		}
276
		return $this->getDocBaseUrl() . '/server/' . $this->defaultDocVersion . '/go.php?to=' . $key;
277
	}
278
279
	/**
280
	 * Returns primary color
281
	 * @return string
282
	 */
283
	public function getColorPrimary() {
284
285
		if ($this->themeExist('getColorPrimary')) {
286
			return $this->theme->getColorPrimary();
287
		}
288
		if ($this->themeExist('getMailHeaderColor')) {
289
			return $this->theme->getMailHeaderColor();
0 ignored issues
show
Bug introduced by
The method getMailHeaderColor() does not seem to exist on object<OC_Theme>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
290
		}
291
		return $this->defaultColorPrimary;
292
	}
293
294
	public function shouldReplaceIcons() {
295
		return false;
296
	}
297
298
	/**
299
	 * Themed logo url
300
	 *
301
	 * @return string
302
	 */
303
	public function getLogo() {
304
		if ($this->themeExist('getLogo')) {
305
			return $this->theme->getLogo();
0 ignored issues
show
Bug introduced by
The method getLogo() does not exist on OC_Theme. Did you maybe mean getLogoClaim()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
306
		}
307
308
		return $this->defaultLogoUrl;
309
	}
310
}
311