Passed
Push — master ( 4d1d4d...9d67c2 )
by Roeland
12:27 queued 13s
created

ThemingDefaults::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 27
rs 9.7

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
5
 *
6
 * @author Arthur Schiwon <[email protected]>
7
 * @author Bjoern Schiessle <[email protected]>
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Daniel Kesselberg <[email protected]>
10
 * @author Guillaume COMPAGNON <[email protected]>
11
 * @author Jan-Christoph Borchardt <[email protected]>
12
 * @author Joachim Bauch <[email protected]>
13
 * @author Joas Schilling <[email protected]>
14
 * @author John Molakvoæ (skjnldsv) <[email protected]>
15
 * @author Julius Haertl <[email protected]>
16
 * @author Julius Härtl <[email protected]>
17
 * @author Lukas Reschke <[email protected]>
18
 * @author Michael Weimann <[email protected]>
19
 * @author Morris Jobke <[email protected]>
20
 * @author Patrik Kernstock <[email protected]>
21
 * @author Robin Appelman <[email protected]>
22
 * @author Roeland Jago Douma <[email protected]>
23
 *
24
 * @license GNU AGPL version 3 or any later version
25
 *
26
 * This program is free software: you can redistribute it and/or modify
27
 * it under the terms of the GNU Affero General Public License as
28
 * published by the Free Software Foundation, either version 3 of the
29
 * License, or (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU Affero General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU Affero General Public License
37
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38
 *
39
 */
40
41
namespace OCA\Theming;
42
43
use OCP\App\AppPathNotFoundException;
44
use OCP\App\IAppManager;
45
use OCP\Files\NotFoundException;
46
use OCP\ICacheFactory;
47
use OCP\IConfig;
48
use OCP\IL10N;
49
use OCP\INavigationManager;
50
use OCP\IURLGenerator;
51
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,
101
								IL10N $l,
102
								IURLGenerator $urlGenerator,
103
								ICacheFactory $cacheFactory,
104
								Util $util,
105
								ImageManager $imageManager,
106
								IAppManager $appManager,
107
								INavigationManager $navigationManager
108
	) {
109
		parent::__construct();
110
		$this->config = $config;
111
		$this->l = $l;
112
		$this->imageManager = $imageManager;
113
		$this->urlGenerator = $urlGenerator;
114
		$this->cacheFactory = $cacheFactory;
115
		$this->util = $util;
116
		$this->appManager = $appManager;
117
		$this->navigationManager = $navigationManager;
118
119
		$this->name = parent::getName();
120
		$this->title = parent::getTitle();
121
		$this->entity = parent::getEntity();
122
		$this->url = parent::getBaseUrl();
123
		$this->color = parent::getColorPrimary();
124
		$this->iTunesAppId = parent::getiTunesAppId();
125
		$this->iOSClientUrl = parent::getiOSClientUrl();
126
		$this->AndroidClientUrl = parent::getAndroidClientUrl();
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() {
154
		return (string)$this->config->getAppValue('theming', 'imprintUrl', '');
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);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $default of OCP\IConfig::getAppValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
		$logo = $this->config->getAppValue('theming', 'logoMime', /** @scrutinizer ignore-type */ false);
Loading history...
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() {
276
		return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
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) {
0 ignored issues
show
introduced by
The condition $this->config->getAppVal...'color', null) !== null is always true.
Loading history...
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) {
331
		if ($app === '' || $app === 'files_sharing') {
332
			$app = 'core';
333
		}
334
		$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
335
336
		try {
337
			$customFavicon = $this->imageManager->getImage('favicon');
338
		} catch (NotFoundException $e) {
339
			$customFavicon = null;
340
		}
341
342
		$route = false;
343
		if ($image === 'favicon.ico' && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
344
			$route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
345
		}
346
		if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
347
			$route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
348
		}
349
		if ($image === 'manifest.json') {
350
			try {
351
				$appPath = $this->appManager->getAppPath($app);
352
				if (file_exists($appPath . '/img/manifest.json')) {
353
					return false;
354
				}
355
			} catch (AppPathNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
356
			}
357
			$route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest');
358
		}
359
		if (strpos($image, 'filetypes/') === 0 && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
360
			$route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
361
		}
362
363
		if ($route) {
364
			return $route . '?v=' . $cacheBusterValue;
365
		}
366
367
		return false;
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) {
387
		$this->config->setAppValue('theming', $setting, $value);
388
		$this->increaseCacheBuster();
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);
399
		$this->increaseCacheBuster();
400
401
		$returnValue = '';
402
		switch ($setting) {
403
			case 'name':
404
				$returnValue = $this->getEntity();
405
				break;
406
			case 'url':
407
				$returnValue = $this->getBaseUrl();
408
				break;
409
			case 'slogan':
410
				$returnValue = $this->getSlogan();
411
				break;
412
			case 'color':
413
				$returnValue = $this->getColorPrimary();
414
				break;
415
			case 'logo':
416
			case 'logoheader':
417
			case 'background':
418
			case 'favicon':
419
				$this->imageManager->delete($setting);
420
				break;
421
		}
422
423
		return $returnValue;
424
	}
425
426
	/**
427
	 * Color of text in the header and primary buttons
428
	 *
429
	 * @return string
430
	 */
431
	public function getTextColorPrimary() {
432
		return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
433
	}
434
}
435