Completed
Push — stable10 ( 737591...a2942c )
by Lukas
09:58 queued 09:42
created

ThemingDefaults::undo()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 24
rs 8.5125
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
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 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
	 */
58
	public function __construct(IConfig $config,
59
								IL10N $l,
60
								IURLGenerator $urlGenerator,
61
								\OC_Defaults $defaults
62
	) {
63
		parent::__construct();
64
		$this->config = $config;
65
		$this->l = $l;
66
		$this->urlGenerator = $urlGenerator;
67
68
		$this->name = $defaults->getName();
69
		$this->url = $defaults->getBaseUrl();
70
		$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...
71
		$this->color = $defaults->getMailHeaderColor();
72
	}
73
74
	public function getName() {
75
		return $this->config->getAppValue('theming', 'name', $this->name);
76
	}
77
78
	public function getHTMLName() {
79
		return $this->config->getAppValue('theming', 'name', $this->name);
80
	}
81
82
	public function getTitle() {
83
		return $this->config->getAppValue('theming', 'name', $this->name);
84
	}
85
86
	public function getEntity() {
87
		return $this->config->getAppValue('theming', 'name', $this->name);
88
	}
89
90
	public function getBaseUrl() {
91
		return $this->config->getAppValue('theming', 'url', $this->url);
92
	}
93
94
	public function getSlogan() {
95
		return $this->config->getAppValue('theming', 'slogan', $this->slogan);
96
	}
97
98
	public function getShortFooter() {
99
		$slogan = $this->getSlogan();
100
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
101
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
102
			($slogan !== '' ? ' – ' . $slogan : '');
103
104
		return $footer;
105
	}
106
107
	/**
108
	 * Color that is used for the header as well as for mail headers
109
	 *
110
	 * @return string
111
	 */
112
	public function getMailHeaderColor() {
113
		return $this->config->getAppValue('theming', 'color', $this->color);
114
	}
115
116
	/**
117
	 * Increases the cache buster key
118
	 */
119
	private function increaseCacheBuster() {
120
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
121
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
122
	}
123
124
	/**
125
	 * Update setting in the database
126
	 *
127
	 * @param string $setting
128
	 * @param string $value
129
	 */
130
	public function set($setting, $value) {
131
		$this->config->setAppValue('theming', $setting, $value);
132
		$this->increaseCacheBuster();
133
	}
134
135
	/**
136
	 * Revert settings to the default value
137
	 *
138
	 * @param string $setting setting which should be reverted
139
	 * @return string default value
140
	 */
141
	public function undo($setting) {
142
		$this->config->deleteAppValue('theming', $setting);
143
		$this->increaseCacheBuster();
144
145
		switch ($setting) {
146
			case 'name':
147
				$returnValue = $this->getEntity();
148
				break;
149
			case 'url':
150
				$returnValue = $this->getBaseUrl();
151
				break;
152
			case 'slogan':
153
				$returnValue = $this->getSlogan();
154
				break;
155
			case 'color':
156
				$returnValue = $this->getMailHeaderColor();
157
				break;
158
			default:
159
				$returnValue = '';
160
				break;
161
		}
162
163
		return $returnValue;
164
	}
165
166
}
167