Completed
Push — master ( da3b44...325c46 )
by Maxence
02:02
created

ConfigService::setUserValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
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 2018
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\FullTextSearch\Service;
28
29
use OCA\FullTextSearch\AppInfo\Application;
30
use OCA\FullTextSearch\Exceptions\ProviderOptionsDoesNotExistException;
31
use OCP\IConfig;
32
use OCP\PreConditionNotMetException;
33
use OCP\Util;
34
35
class ConfigService {
36
37
	const APP_NAVIGATION = 'app_navigation';
38
	const SEARCH_PLATFORM = 'search_platform';
39
	const PROVIDER_INDEXED = 'provider_indexed';
40
	const CRON_LAST_ERR_RESET = 'cron_err_reset';
41
42
	/** @var array */
43
	public $defaults = [
44
		self::SEARCH_PLATFORM     => '',
45
		self::APP_NAVIGATION      => '0',
46
		self::PROVIDER_INDEXED    => '',
47
		self::CRON_LAST_ERR_RESET => '0'
48
	];
49
50
	/** @var IConfig */
51
	private $config;
52
53
	/** @var string */
54
	private $userId;
55
56
	/** @var MiscService */
57
	private $miscService;
58
59
60
	/**
61
	 * ConfigService constructor.
62
	 *
63
	 * @param IConfig $config
64
	 * @param string $userId
65
	 * @param MiscService $miscService
66
	 */
67
	public function __construct(
68
		IConfig $config, $userId, MiscService $miscService
69
	) {
70
		$this->config = $config;
71
		$this->userId = $userId;
72
		$this->miscService = $miscService;
73
	}
74
75
76
	/**
77
	 * @return array
78
	 */
79
	public function getConfig() {
80
		$keys = array_keys($this->defaults);
81
		$data = [];
82
83
		foreach ($keys as $k) {
84
			$data[$k] = $this->getAppValue($k);
85
		}
86
87
		return $data;
88
	}
89
90
91
	/**
92
	 * @param array $save
93
	 */
94
	public function setConfig($save) {
95
		$keys = array_keys($this->defaults);
96
97
		foreach ($keys as $k) {
98
			if (array_key_exists($k, $save)) {
99
				$this->setAppValue($k, $save[$k]);
100
			}
101
		}
102
	}
103
104
105
	/**
106
	 * Get a version of an app
107
	 *
108
	 * @param string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
109
	 *
110
	 * @return string
111
	 */
112
	public function getAppVersion($appId) {
113
		return $this->config->getAppValue($appId, 'installed_version', '');
114
	}
115
116
117
	/**
118
	 * Get a value by key
119
	 *
120
	 * @param string $key
121
	 *
122
	 * @return string
123
	 */
124 View Code Duplication
	public function getAppValue($key) {
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...
125
		$defaultValue = null;
126
		if (array_key_exists($key, $this->defaults)) {
127
			$defaultValue = $this->defaults[$key];
128
		}
129
130
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
131
	}
132
133
	/**
134
	 * Set a value by key
135
	 *
136
	 * @param string $key
137
	 * @param string $value
138
	 *
139
	 * @return void
140
	 */
141
	public function setAppValue($key, $value) {
142
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
143
	}
144
145
	/**
146
	 * remove a key
147
	 *
148
	 * @param string $key
149
	 *
150
	 * @return string
151
	 */
152
	public function deleteAppValue($key) {
153
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
154
	}
155
156
	/**
157
	 * Get a user value by key
158
	 *
159
	 * @param string $key
160
	 *
161
	 * @return string
162
	 */
163 View Code Duplication
	public function getUserValue($key) {
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...
164
		$defaultValue = null;
165
		if (array_key_exists($key, $this->defaults)) {
166
			$defaultValue = $this->defaults[$key];
167
		}
168
169
		return $this->config->getUserValue(
170
			$this->userId, Application::APP_NAME, $key, $defaultValue
171
		);
172
	}
173
174
	/**
175
	 * Set a user value by key
176
	 *
177
	 * @param string $key
178
	 * @param string $value
179
	 *
180
	 * @return string
181
	 * @throws PreConditionNotMetException
182
	 */
183
	public function setUserValue($key, $value) {
184
		return $this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
185
	}
186
187
	/**
188
	 * Get a user value by key and user
189
	 *
190
	 * @param string $userId
191
	 * @param string $key
192
	 *
193
	 * @return string
194
	 */
195
	public function getValueForUser($userId, $key) {
196
		return $this->config->getUserValue($userId, Application::APP_NAME, $key);
197
	}
198
199
	/**
200
	 * Set a user value by key
201
	 *
202
	 * @param string $userId
203
	 * @param string $key
204
	 * @param string $value
205
	 *
206
	 * @return string
207
	 * @throws PreConditionNotMetException
208
	 */
209
	public function setValueForUser($userId, $key, $value) {
210
		return $this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
211
	}
212
213
214
	/**
215
	 * @param string $providerId
216
	 * @param string $options
217
	 * @param string $value
218
	 */
219
	public function setProviderOptions($providerId, $options, $value) {
220
		$arr = json_decode($this->getAppValue($options), true);
221
		if ($arr === null) {
222
			$arr = [];
223
		}
224
225
		$arr[$providerId] = $value;
226
227
		$this->setAppValue($options, json_encode($arr));
228
	}
229
230
231
	/**
232
	 * @param string $options
233
	 */
234
	public function resetProviderOptions($options) {
235
		$this->setAppValue($options, '');
236
	}
237
238
239
	/**
240
	 * @param string $providerId
241
	 * @param string $options
242
	 *
243
	 * @return string
244
	 * @throws ProviderOptionsDoesNotExistException
245
	 */
246
	public function getProviderOptions($providerId, $options) {
247
		$arr = json_decode($this->getAppValue($options), true);
248
		if ($arr === null) {
249
			$arr = [];
250
		}
251
252
		if (!key_exists($providerId, $arr)) {
253
			throw new ProviderOptionsDoesNotExistException();
254
		}
255
256
		return $arr[$providerId];
257
	}
258
259
260
	/**
261
	 * return the cloud version.
262
	 * if $complete is true, return a string x.y.z
263
	 *
264
	 * @param boolean $complete
265
	 *
266
	 * @return string|integer
267
	 */
268
	public function getCloudVersion($complete = false) {
269
		$ver = Util::getVersion();
270
271
		if ($complete) {
272
			return implode('.', $ver);
273
		}
274
275
		return $ver[0];
276
	}
277
}
278