Completed
Pull Request — master (#770)
by Julius
08:36
created

ThemingDefaults::getLogo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 2
b 0
f 1
nc 2
nop 0
dl 8
loc 8
rs 9.4285
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
27
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\Files\IRootFolder;
32
33
34
class ThemingDefaults extends \OC_Defaults {
35
36
	/** @var IConfig */
37
	private $config;
38
	/** @var IL10N */
39
	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...
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
	/** @var string */
43
	private $name;
44
	/** @var string */
45
	private $url;
46
	/** @var string */
47
	private $slogan;
48
	/** @var string */
49
	private $color;
50
	/** @var IRootFolder */
51
	private $rootFolder;
52
53
	/**
54
	 * ThemingDefaults constructor.
55
	 *
56
	 * @param IConfig $config
57
	 * @param IL10N $l
58
	 * @param IURLGenerator $urlGenerator
59
	 * @param \OC_Defaults $defaults
60
	 * @param IRootFolder $rootFolder
61
	 */
62
	public function __construct(IConfig $config,
63
								IL10N $l,
64
								IURLGenerator $urlGenerator,
65
								\OC_Defaults $defaults,
66
								IRootFolder $rootFolder
67
	) {
68
		parent::__construct();
69
		$this->config = $config;
70
		$this->l = $l;
71
		$this->urlGenerator = $urlGenerator;
72
		$this->rootFolder = $rootFolder;
73
74
		$this->name = $defaults->getName();
75
		$this->url = $defaults->getBaseUrl();
76
		$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...
77
		$this->color = $defaults->getMailHeaderColor();
78
	}
79
80
	public function getName() {
81
		return $this->config->getAppValue('theming', 'name', $this->name);
82
	}
83
84
	public function getHTMLName() {
85
		return $this->config->getAppValue('theming', 'name', $this->name);
86
	}
87
88
	public function getTitle() {
89
		return $this->config->getAppValue('theming', 'name', $this->name);
90
	}
91
92
	public function getEntity() {
93
		return $this->config->getAppValue('theming', 'name', $this->name);
94
	}
95
96
	public function getBaseUrl() {
97
		return $this->config->getAppValue('theming', 'url', $this->url);
98
	}
99
100
	public function getSlogan() {
101
		return $this->config->getAppValue('theming', 'slogan', $this->slogan);
102
	}
103
104
	public function getShortFooter() {
105
		$slogan = $this->getSlogan();
106
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
107
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
108
			($slogan !== '' ? ' – ' . $slogan : '');
109
110
		return $footer;
111
	}
112
113
	/**
114
	 * Color that is used for the header as well as for mail headers
115
	 *
116
	 * @return string
117
	 */
118
	public function getMailHeaderColor() {
119
		return $this->config->getAppValue('theming', 'color', $this->color);
120
	}
121
122
	/**
123
	 * Themed logo url
124
	 *
125
	 * @return string
126
	 */
127 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...
128
		$logo = $this->config->getAppValue('theming', 'logoMime');
129
		if(!$logo || !$this->rootFolder->nodeExists('themedinstancelogo')) {
130
			return $this->urlGenerator->imagePath('core','logo.svg');
131
		} else {
132
			return $this->urlGenerator->linkToRoute('theming.Theming.getLogo');
133
		}
134
	}
135
	/**
136
	 * Themed background image url
137
	 *
138
	 * @return string
139
	 */
140 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...
141
		$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime');
142
		if(!$backgroundLogo || !$this->rootFolder->nodeExists('themedbackgroundlogo')) {
143
			return $this->urlGenerator->imagePath('core','background.jpg');
144
		} else {
145
			return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
146
		}
147
	}
148
	/**
149
	 * Increases the cache buster key
150
	 */
151
	private function increaseCacheBuster() {
152
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
153
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
154
	}
155
156
	/**
157
	 * Update setting in the database
158
	 *
159
	 * @param string $setting
160
	 * @param string $value
161
	 */
162
	public function set($setting, $value) {
163
		$this->config->setAppValue('theming', $setting, $value);
164
		$this->increaseCacheBuster();
165
	}
166
167
	/**
168
	 * Revert settings to the default value
169
	 *
170
	 * @param string $setting setting which should be reverted
171
	 * @return string default value
172
	 */
173
	public function undo($setting) {
174
		$this->config->deleteAppValue('theming', $setting);
175
		$this->increaseCacheBuster();
176
177
		switch ($setting) {
178
			case 'name':
179
				$returnValue = $this->getEntity();
180
				break;
181
			case 'url':
182
				$returnValue = $this->getBaseUrl();
183
				break;
184
			case 'slogan':
185
				$returnValue = $this->getSlogan();
186
				break;
187
			case 'color':
188
				$returnValue = $this->getMailHeaderColor();
189
				break;
190
			default:
191
				$returnValue = '';
192
				break;
193
		}
194
195
		return $returnValue;
196
	}
197
198
}
199