Completed
Pull Request — stable9 (#244)
by Joas
23:06 queued 11:08
created

Template::getMailHeaderColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 * @copyright Copyright (c) 2016 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
use OCP\IConfig;
26
use OCP\IL10N;
27
use OCP\IURLGenerator;
28
29
/**
30
 * Class Template
31
 *
32
 * Handle all the values which can be modified by this app
33
 *
34
 * @package OCA\Theming
35
 */
36
class Template extends \OC_Defaults {
37
	/** @var IConfig */
38
	private $config;
39
	/** @var  IL10N */
40
	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...
41
	/** @var IURLGenerator */
42
	private $urlGenerator;
43
	/** @var string */
44
	private $name;
45
	/** @var string */
46
	private $url;
47
	/** @var string */
48
	private $slogan;
49
	/** @var string */
50
	private $color;
51
52
	/**
53
	 * Template constructor.
54
	 *
55
	 * @param IConfig $config
56
	 * @param IL10N $l
57
	 * @param IURLGenerator $urlGenerator
58
	 * @param \OC_Defaults $defaults
59
	 */
60
	public function __construct(IConfig $config,
61
								IL10N $l,
62
								IURLGenerator $urlGenerator,
63
								\OC_Defaults $defaults
64
	) {
65
		parent::__construct();
66
		$this->config = $config;
67
		$this->l = $l;
68
		$this->urlGenerator = $urlGenerator;
69
70
		$this->name = $defaults->getName();
71
		$this->url = $defaults->getBaseUrl();
72
		$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...
73
		$this->color = $defaults->getMailHeaderColor();
74
	}
75
76
	public function getName() {
77
		return $this->config->getAppValue('theming', 'name', $this->name);
78
	}
79
80
	public function getEntity() {
81
		return $this->config->getAppValue('theming', 'name', $this->name);
82
	}
83
	
84
	public function getBaseUrl() {
85
		return $this->config->getAppValue('theming', 'url', $this->url);
86
	}
87
88
	public function getSlogan() {
89
		return $this->config->getAppValue('theming', 'slogan', $this->slogan);
90
	}
91
92
	/**
93
	 * Color that is used for the header as well as for mail headers
94
	 *
95
	 * @return string
96
	 */
97
	public function getMailHeaderColor() {
98
		return $this->config->getAppValue('theming', 'color', $this->color);
99
	}
100
101
	/**
102
	 * Increases the cache buster key
103
	 */
104
	private function increaseCacheBuster() {
105
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
106
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
107
	}
108
109
	/**
110
	 * Update setting in the database
111
	 *
112
	 * @param string $setting
113
	 * @param string $value
114
	 */
115
	public function set($setting, $value) {
116
		$this->config->setAppValue('theming', $setting, $value);
117
		$this->increaseCacheBuster();
118
	}
119
120
	/**
121
	 * Revert settings to the default value
122
	 *
123
	 * @param string $setting setting which should be reverted
124
	 * @return string default value
125
	 */
126
	public function undo($setting) {
127
		$this->config->deleteAppValue('theming', $setting);
128
		$this->increaseCacheBuster();
129
130
		switch ($setting) {
131
			case 'name':
132
				$returnValue = $this->getEntity();
133
				break;
134
			case 'url':
135
				$returnValue = $this->getBaseUrl();
136
				break;
137
			case 'slogan':
138
				$returnValue = $this->getSlogan();
139
				break;
140
			case 'color':
141
				$returnValue = $this->getMailHeaderColor();
142
				break;
143
			default:
144
				$returnValue = '';
145
				break;
146
		}
147
148
		return $returnValue;
149
	}
150
}
151