Completed
Push — master ( 63adda...00bb92 )
by Lukas
09:13
created

ThemingDefaults::getMailHeaderColor()   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
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
23
namespace OCA\Theming;
24
25
26
use OCP\IConfig;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\Files\IRootFolder;
30
31
class ThemingDefaults extends \OC_Defaults {
32
33
	/** @var IConfig */
34
	private $config;
35
	/** @var IL10N */
36
	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...
37
	/** @var IURLGenerator */
38
	private $urlGenerator;
39
	/** @var IRootFolder */
40
	private $rootFolder;
41
	/** @var string */
42
	private $name;
43
	/** @var string */
44
	private $url;
45
	/** @var string */
46
	private $slogan;
47
	/** @var string */
48
	private $color;
49
50
	/**
51
	 * ThemingDefaults constructor.
52
	 *
53
	 * @param IConfig $config
54
	 * @param IL10N $l
55
	 * @param IURLGenerator $urlGenerator
56
	 * @param \OC_Defaults $defaults
57
	 * @param IRootFolder $rootFolder
58
	 */
59
	public function __construct(IConfig $config,
60
								IL10N $l,
61
								IURLGenerator $urlGenerator,
62
								\OC_Defaults $defaults,
63
								IRootFolder $rootFolder
64
	) {
65
		parent::__construct();
66
		$this->config = $config;
67
		$this->l = $l;
68
		$this->urlGenerator = $urlGenerator;
69
		$this->rootFolder = $rootFolder;
70
71
		$this->name = $defaults->getName();
72
		$this->url = $defaults->getBaseUrl();
73
		$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...
74
		$this->color = $defaults->getMailHeaderColor();
75
	}
76
77
	public function getName() {
78
		return $this->config->getAppValue('theming', 'name', $this->name);
79
	}
80
81
	public function getHTMLName() {
82
		return $this->config->getAppValue('theming', 'name', $this->name);
83
	}
84
85
	public function getTitle() {
86
		return $this->config->getAppValue('theming', 'name', $this->name);
87
	}
88
89
	public function getEntity() {
90
		return $this->config->getAppValue('theming', 'name', $this->name);
91
	}
92
93
	public function getBaseUrl() {
94
		return $this->config->getAppValue('theming', 'url', $this->url);
95
	}
96
97
	public function getSlogan() {
98
		return $this->config->getAppValue('theming', 'slogan', $this->slogan);
99
	}
100
101
	public function getShortFooter() {
102
		$slogan = $this->getSlogan();
103
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
104
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
105
			($slogan !== '' ? ' – ' . $slogan : '');
106
107
		return $footer;
108
	}
109
110
	/**
111
	 * Color that is used for the header as well as for mail headers
112
	 *
113
	 * @return string
114
	 */
115
	public function getMailHeaderColor() {
116
		return $this->config->getAppValue('theming', 'color', $this->color);
117
	}
118
119
	/**
120
	 * Themed logo url
121
	 *
122
	 * @return string
123
	 */
124 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...
125
		$logo = $this->config->getAppValue('theming', 'logoMime');
126
		if(!$logo || !$this->rootFolder->nodeExists('/themedinstancelogo')) {
127
			return $this->urlGenerator->imagePath('core','logo.svg');
128
		} else {
129
			return $this->urlGenerator->linkToRoute('theming.Theming.getLogo');
130
		}
131
	}
132
133
	/**
134
	 * Themed background image url
135
	 *
136
	 * @return string
137
	 */
138 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...
139
		$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime');
140
		if(!$backgroundLogo || !$this->rootFolder->nodeExists('/themedbackgroundlogo')) {
141
			return $this->urlGenerator->imagePath('core','background.jpg');
142
		} else {
143
			return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
144
		}
145
	}
146
147
	/**
148
	 * Increases the cache buster key
149
	 */
150
	private function increaseCacheBuster() {
151
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
152
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
153
	}
154
155
	/**
156
	 * Update setting in the database
157
	 *
158
	 * @param string $setting
159
	 * @param string $value
160
	 */
161
	public function set($setting, $value) {
162
		$this->config->setAppValue('theming', $setting, $value);
163
		$this->increaseCacheBuster();
164
	}
165
166
	/**
167
	 * Revert settings to the default value
168
	 *
169
	 * @param string $setting setting which should be reverted
170
	 * @return string default value
171
	 */
172
	public function undo($setting) {
173
		$this->config->deleteAppValue('theming', $setting);
174
		$this->increaseCacheBuster();
175
176
		switch ($setting) {
177
			case 'name':
178
				$returnValue = $this->getEntity();
179
				break;
180
			case 'url':
181
				$returnValue = $this->getBaseUrl();
182
				break;
183
			case 'slogan':
184
				$returnValue = $this->getSlogan();
185
				break;
186
			case 'color':
187
				$returnValue = $this->getMailHeaderColor();
188
				break;
189
			default:
190
				$returnValue = '';
191
				break;
192
		}
193
194
		return $returnValue;
195
	}
196
197
}
198