Passed
Push — master ( 42c369...11b018 )
by Maxence
02:16
created

ConfigService::getAppValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use OCA\CMSPico\AppInfo\Application;
30
use OCP\IConfig;
31
use OCP\Util;
32
33
class ConfigService {
34
35
	const CUSTOM_TEMPLATES = 'custom_templates';
36
37
	const CUSTOM_THEMES = 'custom_themes';
38
39
	private $defaults = [
40
		self::CUSTOM_THEMES => '',
41
		self::CUSTOM_TEMPLATES => ''
42
	];
43
44
	/** @var IConfig */
45
	private $config;
46
47
	/** @var string */
48
	private $userId;
49
50
	/** @var MiscService */
51
	private $miscService;
52
53
54
	/**
55
	 * ConfigService constructor.
56
	 *
57
	 * @param IConfig $config
58
	 * @param string $userId
59
	 * @param MiscService $miscService
60
	 */
61
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
62
		$this->config = $config;
63
		$this->userId = $userId;
64
		$this->miscService = $miscService;
65
	}
66
67
68
	/**
69
	 * Get a value by key
70
	 *
71
	 * @param string $key
72
	 *
73
	 * @return string
74
	 */
75
	public function getAppValue($key) {
76
		$defaultValue = $this->getDefaultValue($key);
77
78
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
79
	}
80
81
82
	/**
83
	 * Set a value by key
84
	 *
85
	 * @param string $key
86
	 * @param string $value
87
	 *
88
	 * @return void
89
	 */
90
	public function setAppValue($key, $value) {
91
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
92
	}
93
94
95
	/**
96
	 * remove a key
97
	 *
98
	 * @param string $key
99
	 *
100
	 * @return string
101
	 */
102
	public function deleteAppValue($key) {
103
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
104
	}
105
106
107
	/**
108
	 * Get a user value by key
109
	 *
110
	 * @param string $key
111
	 *
112
	 * @return string
113
	 */
114
	public function getUserValue($key) {
115
		$defaultValue = $this->getDefaultValue($key);
116
117
		return $this->config->getUserValue(
118
			$this->userId, Application::APP_NAME, $key, $defaultValue
119
		);
120
	}
121
122
123
	/**
124
	 * Set a user value by key
125
	 *
126
	 * @param string $key
127
	 * @param string $value
128
	 *
129
	 * @return string
130
	 */
131
	public function setUserValue($key, $value) {
132
		return $this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
133
	}
134
135
136
	/**
137
	 * Get a user value by key and user
138
	 *
139
	 * @param string $userId
140
	 * @param string $key
141
	 *
142
	 * @return string
143
	 */
144
	public function getValueForUser($userId, $key) {
145
		return $this->config->getUserValue($userId, Application::APP_NAME, $key);
146
	}
147
148
149
	/**
150
	 * Set a user value by key
151
	 *
152
	 * @param string $userId
153
	 * @param string $key
154
	 * @param string $value
155
	 *
156
	 * @return string
157
	 */
158
	public function setValueForUser($userId, $key, $value) {
159
		return $this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
160
	}
161
162
163
	/**
164
	 * @param string $key
165
	 *
166
	 * @return string
167
	 */
168
	private function getDefaultValue($key) {
169
		if (array_key_exists($key, $this->defaults)) {
170
			return (string)$this->defaults[$key];
171
		}
172
173
		return '';
174
	}
175
176
177
	public function getSystemValue($key, $default) {
178
		return $this->config->getSystemValue($key, $default);
179
	}
180
181
	/**
182
	 * return the cloud version.
183
	 * if $complete is true, return a string x.y.z
184
	 *
185
	 * @param boolean $complete
186
	 *
187
	 * @return string|integer
188
	 */
189 View Code Duplication
	public function getCloudVersion($complete = false) {
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...
190
		$ver = Util::getVersion();
191
192
		if ($complete) {
193
			return implode('.', $ver);
194
		}
195
196
		return $ver[0];
197
	}
198
}
199