Completed
Push — master ( 4c6036...8838ed )
by Morris
11:46
created

ThemingDefaults::getSlogan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Theming;
24
25
26
use OCP\Files\IAppData;
27
use OCP\ICacheFactory;
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\Util;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\Theming\Util.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
32
33
class ThemingDefaults extends \OC_Defaults {
34
35
	/** @var IConfig */
36
	private $config;
37
	/** @var IL10N */
38
	private $l;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
39
	/** @var IURLGenerator */
40
	private $urlGenerator;
41
	/** @var IAppData */
42
	private $appData;
43
	/** @var ICacheFactory */
44
	private $cacheFactory;
45
	/** @var string */
46
	private $name;
47
	/** @var string */
48
	private $url;
49
	/** @var string */
50
	private $slogan;
51
	/** @var string */
52
	private $color;
53
54
	/**
55
	 * ThemingDefaults constructor.
56
	 *
57
	 * @param IConfig $config
58
	 * @param IL10N $l
59
	 * @param IURLGenerator $urlGenerator
60
	 * @param \OC_Defaults $defaults
61
	 * @param IAppData $appData
62
	 * @param ICacheFactory $cacheFactory
63
	 */
64
	public function __construct(IConfig $config,
65
								IL10N $l,
66
								IURLGenerator $urlGenerator,
67
								\OC_Defaults $defaults,
68
								IAppData $appData,
69
								ICacheFactory $cacheFactory
70
	) {
71
		parent::__construct();
72
		$this->config = $config;
73
		$this->l = $l;
74
		$this->urlGenerator = $urlGenerator;
75
		$this->appData = $appData;
76
		$this->cacheFactory = $cacheFactory;
77
78
		$this->name = $defaults->getName();
79
		$this->url = $defaults->getBaseUrl();
80
		$this->slogan = $defaults->getSlogan();
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaults->getSlogan() can also be of type object<OC_L10N_String>. However, the property $slogan is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
		$this->color = $defaults->getColorPrimary();
82
	}
83
84
	public function getName() {
85
		return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
86
	}
87
88
	public function getHTMLName() {
89
		return $this->config->getAppValue('theming', 'name', $this->name);
90
	}
91
92
	public function getTitle() {
93
		return $this->getName();
94
	}
95
96
	public function getEntity() {
97
		return $this->getName();
98
	}
99
100
	public function getBaseUrl() {
101
		return $this->config->getAppValue('theming', 'url', $this->url);
102
	}
103
104
	public function getSlogan() {
105
		return Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', $this->slogan));
0 ignored issues
show
Bug Compatibility introduced by
The expression \OCP\Util::sanitizeHTML(...ogan', $this->slogan)); of type array|string adds the type array to the return on line 105 which is incompatible with the return type of the parent method OC_Defaults::getSlogan of type string|OC_L10N_String.
Loading history...
106
	}
107
108
	public function getShortFooter() {
109
		$slogan = $this->getSlogan();
110
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
111
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
112
			($slogan !== '' ? ' – ' . $slogan : '');
113
114
		return $footer;
115
	}
116
117
	/**
118
	 * Color that is used for the header as well as for mail headers
119
	 *
120
	 * @return string
121
	 */
122
	public function getColorPrimary() {
123
		return $this->config->getAppValue('theming', 'color', $this->color);
124
	}
125
126
	/**
127
	 * Themed logo url
128
	 *
129
	 * @return string
130
	 */
131 View Code Duplication
	public function getLogo() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
		$logo = $this->config->getAppValue('theming', 'logoMime');
133
134
		$logoExists = true;
135
		try {
136
			$this->appData->getFolder('images')->getFile('logo');
137
		} catch (\Exception $e) {
138
			$logoExists = false;
139
		}
140
141
		if(!$logo || !$logoExists) {
142
			return $this->urlGenerator->imagePath('core','logo.svg');
143
		}
144
145
		return $this->urlGenerator->linkToRoute('theming.Theming.getLogo');
146
	}
147
148
	/**
149
	 * Themed background image url
150
	 *
151
	 * @return string
152
	 */
153 View Code Duplication
	public function getBackground() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
		$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime');
155
156
		$backgroundExists = true;
157
		try {
158
			$this->appData->getFolder('images')->getFile('background');
159
		} catch (\Exception $e) {
160
			$backgroundExists = false;
161
		}
162
163
		if(!$backgroundLogo || !$backgroundExists) {
164
			return $this->urlGenerator->imagePath('core','background.jpg');
165
		}
166
167
		return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
168
	}
169
170
	/**
171
	 * Check if Imagemagick is enabled and if SVG is supported
172
	 * otherwise we can't render custom icons
173
	 *
174
	 * @return bool
175
	 */
176
	public function shouldReplaceIcons() {
177
		$cache = $this->cacheFactory->create('theming');
178
		if($value = $cache->get('shouldReplaceIcons')) {
179
			return (bool)$value;
180
		}
181
		$value = false;
182
		if(extension_loaded('imagick')) {
183
			$checkImagick = new \Imagick();
184
			if (count($checkImagick->queryFormats('SVG')) >= 1) {
185
				$value = true;
186
			}
187
			$checkImagick->clear();
188
		}
189
		$cache->set('shouldReplaceIcons', $value);
190
		return $value;
191
	}
192
193
	/**
194
	 * Gets the current cache buster count
195
	 *
196
	 * @return string
197
	 */
198
	public function getCacheBusterCounter() {
199
		return $this->config->getAppValue('theming', 'cachebuster', '0');
200
	}
201
202
	/**
203
	 * Increases the cache buster key
204
	 */
205
	private function increaseCacheBuster() {
206
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
207
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
208
	}
209
210
	/**
211
	 * Update setting in the database
212
	 *
213
	 * @param string $setting
214
	 * @param string $value
215
	 */
216
	public function set($setting, $value) {
217
		$this->config->setAppValue('theming', $setting, $value);
218
		$this->increaseCacheBuster();
219
	}
220
221
	/**
222
	 * Revert settings to the default value
223
	 *
224
	 * @param string $setting setting which should be reverted
225
	 * @return string default value
226
	 */
227
	public function undo($setting) {
228
		$this->config->deleteAppValue('theming', $setting);
229
		$this->increaseCacheBuster();
230
231
		switch ($setting) {
232
			case 'name':
233
				$returnValue = $this->getEntity();
234
				break;
235
			case 'url':
236
				$returnValue = $this->getBaseUrl();
237
				break;
238
			case 'slogan':
239
				$returnValue = $this->getSlogan();
240
				break;
241
			case 'color':
242
				$returnValue = $this->getColorPrimary();
243
				break;
244
			default:
245
				$returnValue = '';
246
				break;
247
		}
248
249
		return $returnValue;
250
	}
251
252
}
253