Passed
Push — master ( 68f8dc...c1b78a )
by Maxence
02:11
created

ConfigService::getDefaultValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
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 APP_TEST = 'test_admin';
36
	const APP_TEST_PERSONAL = 'test_personal';
37
38
	private $defaults = [
39
		self::APP_TEST          => '1',
40
		self::APP_TEST_PERSONAL => '0'
41
	];
42
43
	/** @var IConfig */
44
	private $config;
45
46
	/** @var string */
47
	private $userId;
48
49
	/** @var MiscService */
50
	private $miscService;
51
52
53
	/**
54
	 * ConfigService constructor.
55
	 *
56
	 * @param IConfig $config
57
	 * @param string $userId
58
	 * @param MiscService $miscService
59
	 */
60
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
61
		$this->config = $config;
62
		$this->userId = $userId;
63
		$this->miscService = $miscService;
64
	}
65
66
67
	/**
68
	 * Get a value by key
69
	 *
70
	 * @param string $key
71
	 *
72
	 * @return string
73
	 */
74
	public function getAppValue($key) {
75
		$defaultValue = $this->getDefaultValue($key);
76
77
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
78
	}
79
80
81
	/**
82
	 * Set a value by key
83
	 *
84
	 * @param string $key
85
	 * @param string $value
86
	 *
87
	 * @return void
88
	 */
89
	public function setAppValue($key, $value) {
90
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
91
	}
92
93
94
	/**
95
	 * remove a key
96
	 *
97
	 * @param string $key
98
	 *
99
	 * @return string
100
	 */
101
	public function deleteAppValue($key) {
102
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
103
	}
104
105
106
	/**
107
	 * Get a user value by key
108
	 *
109
	 * @param string $key
110
	 *
111
	 * @return string
112
	 */
113
	public function getUserValue($key) {
114
		$defaultValue = $this->getDefaultValue($key);
115
116
		return $this->config->getUserValue(
117
			$this->userId, Application::APP_NAME, $key, $defaultValue
118
		);
119
	}
120
121
122
	/**
123
	 * Set a user value by key
124
	 *
125
	 * @param string $key
126
	 * @param string $value
127
	 *
128
	 * @return string
129
	 */
130
	public function setUserValue($key, $value) {
131
		return $this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
132
	}
133
134
135
	/**
136
	 * Get a user value by key and user
137
	 *
138
	 * @param string $userId
139
	 * @param string $key
140
	 *
141
	 * @return string
142
	 */
143
	public function getValueForUser($userId, $key) {
144
		return $this->config->getUserValue($userId, Application::APP_NAME, $key);
145
	}
146
147
148
	/**
149
	 * Set a user value by key
150
	 *
151
	 * @param string $userId
152
	 * @param string $key
153
	 * @param string $value
154
	 *
155
	 * @return string
156
	 */
157
	public function setValueForUser($userId, $key, $value) {
158
		return $this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
159
	}
160
161
162
	/**
163
	 * @param string $key
164
	 *
165
	 * @return string
166
	 */
167
	private function getDefaultValue($key) {
168
		if (array_key_exists($key, $this->defaults)) {
169
			return (string) $this->defaults[$key];
170
		}
171
172
		return '';
173
	}
174
175
176
	/**
177
	 * return the cloud version.
178
	 * if $complete is true, return a string x.y.z
179
	 *
180
	 * @param boolean $complete
181
	 *
182
	 * @return string|integer
183
	 */
184
	public function getCloudVersion($complete = false) {
185
		$ver = Util::getVersion();
186
187
		if ($complete) {
188
			return implode('.', $ver);
189
		}
190
191
		return $ver[0];
192
	}
193
}
194