Passed
Push — master ( 72545f...5076a0 )
by Morris
14:01 queued 11s
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
		// short cut to avoid setting up the filesystem just to check if the logo is there
229
		//
230
		// explanation: if an SVG is requested and the app config value for logoMime is set then the logo is there.
231
		// otherwise we need to check it and maybe also generate a PNG from the SVG (that's done in getImage() which
232
		// needs to be called then)
233
		if ($useSvg === true && $logo !== false) {
234
			$logoExists = true;
235
		} else {
236
			try {
237
				$this->imageManager->getImage('logo', $useSvg);
238
				$logoExists = true;
239
			} catch (\Exception $e) {
240
				$logoExists = false;
241
			}
242
		}
243
244
		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
245
246
		if (!$logo || !$logoExists) {
247
			if ($useSvg) {
248
				$logo = $this->urlGenerator->imagePath('core', 'logo/logo.svg');
249
			} else {
250
				$logo = $this->urlGenerator->imagePath('core', 'logo/logo.png');
251
			}
252
			return $logo . '?v=' . $cacheBusterCounter;
253
		}
254
255
		return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
256
	}
257
258
	/**
259
	 * Themed background image url
260
	 *
261
	 * @return string
262
	 */
263
	public function getBackground(): string {
264
		return $this->imageManager->getImageUrl('background');
265
	}
266
267
	/**
268
	 * @return string
269
	 */
270
	public function getiTunesAppId() {
271
		return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
272
	}
273
274
	/**
275
	 * @return string
276
	 */
277
	public function getiOSClientUrl() {
278
		return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
279
	}
280
281
	/**
282
	 * @return string
283
	 */
284
	public function getAndroidClientUrl() {
285
		return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
286
	}
287
288
289
	/**
290
	 * @return array scss variables to overwrite
291
	 */
292
	public function getScssVariables() {
293
		$cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
294
		if ($value = $cache->get('getScssVariables')) {
295
			return $value;
296
		}
297
298
		$variables = [
299
			'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'",
300
			'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
301
			'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'",
302
			'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'",
303
			'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'"
304
		];
305
306
		$variables['image-logo'] = "url('".$this->imageManager->getImageUrl('logo')."')";
307
		$variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')";
308
		$variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')";
309
		$variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
310
		$variables['image-login-plain'] = 'false';
311
312
		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...
313
			$variables['color-primary'] = $this->getColorPrimary();
314
			$variables['color-primary-text'] = $this->getTextColorPrimary();
315
			$variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
316
		}
317
318
		if ($this->config->getAppValue('theming', 'backgroundMime', null) === 'backgroundColor') {
319
			$variables['image-login-plain'] = 'true';
320
		}
321
322
		$variables['has-legal-links'] = 'false';
323
		if ($this->getImprintUrl() !== '' || $this->getPrivacyUrl() !== '') {
324
			$variables['has-legal-links'] = 'true';
325
		}
326
327
		$cache->set('getScssVariables', $variables);
328
		return $variables;
329
	}
330
331
	/**
332
	 * Check if the image should be replaced by the theming app
333
	 * and return the new image location then
334
	 *
335
	 * @param string $app name of the app
336
	 * @param string $image filename of the image
337
	 * @return bool|string false if image should not replaced, otherwise the location of the image
338
	 */
339
	public function replaceImagePath($app, $image) {
340
		if ($app === '' || $app === 'files_sharing') {
341
			$app = 'core';
342
		}
343
		$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
344
345
		try {
346
			$customFavicon = $this->imageManager->getImage('favicon');
347
		} catch (NotFoundException $e) {
348
			$customFavicon = null;
349
		}
350
351
		$route = false;
352
		if ($image === 'favicon.ico' && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
353
			$route = $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]);
354
		}
355
		if (($image === 'favicon-touch.png' || $image === 'favicon-fb.png') && ($customFavicon !== null || $this->imageManager->shouldReplaceIcons())) {
356
			$route = $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]);
357
		}
358
		if ($image === 'manifest.json') {
359
			try {
360
				$appPath = $this->appManager->getAppPath($app);
361
				if (file_exists($appPath . '/img/manifest.json')) {
362
					return false;
363
				}
364
			} catch (AppPathNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
365
			}
366
			$route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest');
367
		}
368
		if (strpos($image, 'filetypes/') === 0 && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) {
369
			$route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]);
370
		}
371
372
		if ($route) {
373
			return $route . '?v=' . $cacheBusterValue;
374
		}
375
376
		return false;
377
	}
378
379
	/**
380
	 * Increases the cache buster key
381
	 */
382
	private function increaseCacheBuster() {
383
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
384
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey + 1);
385
		$this->cacheFactory->createDistributed('theming-')->clear();
386
		$this->cacheFactory->createDistributed('imagePath')->clear();
387
	}
388
389
	/**
390
	 * Update setting in the database
391
	 *
392
	 * @param string $setting
393
	 * @param string $value
394
	 */
395
	public function set($setting, $value) {
396
		$this->config->setAppValue('theming', $setting, $value);
397
		$this->increaseCacheBuster();
398
	}
399
400
	/**
401
	 * Revert settings to the default value
402
	 *
403
	 * @param string $setting setting which should be reverted
404
	 * @return string default value
405
	 */
406
	public function undo($setting) {
407
		$this->config->deleteAppValue('theming', $setting);
408
		$this->increaseCacheBuster();
409
410
		$returnValue = '';
411
		switch ($setting) {
412
			case 'name':
413
				$returnValue = $this->getEntity();
414
				break;
415
			case 'url':
416
				$returnValue = $this->getBaseUrl();
417
				break;
418
			case 'slogan':
419
				$returnValue = $this->getSlogan();
420
				break;
421
			case 'color':
422
				$returnValue = $this->getColorPrimary();
423
				break;
424
			case 'logo':
425
			case 'logoheader':
426
			case 'background':
427
			case 'favicon':
428
				$this->imageManager->delete($setting);
429
				break;
430
		}
431
432
		return $returnValue;
433
	}
434
435
	/**
436
	 * Color of text in the header and primary buttons
437
	 *
438
	 * @return string
439
	 */
440
	public function getTextColorPrimary() {
441
		return $this->util->invertTextColor($this->getColorPrimary()) ? '#000000' : '#ffffff';
442
	}
443
}
444