Completed
Push — master ( 98ba9d...c4a6d6 )
by Maxence
02:00 queued 18s
created

ConfigService::setValueForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * FullTextSearch_ElasticSearch - Use Elasticsearch to index the content of your nextcloud
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\FullTextSearch_ElasticSearch\Service;
32
33
34
use OCA\FullTextSearch_ElasticSearch\AppInfo\Application;
35
use OCA\FullTextSearch_ElasticSearch\Exceptions\ConfigurationException;
36
use OCP\IConfig;
37
use OCP\PreConditionNotMetException;
38
use OCP\Util;
39
40
41
/**
42
 * Class ConfigService
43
 *
44
 * @package OCA\FullTextSearch_ElasticSearch\Service
45
 */
46
class ConfigService {
47
48
49
	const FIELDS_LIMIT = 'fields_limit';
50
	const ELASTIC_HOST = 'elastic_host';
51
	const ELASTIC_INDEX = 'elastic_index';
52
	const ELASTIC_VER_BELOW66 = 'es_ver_below66';
53
	const ANALYZER_TOKENIZER = 'analyzer_tokenizer';
54
55
56
	public $defaults = [
57
		self::ELASTIC_HOST        => '',
58
		self::ELASTIC_INDEX       => '',
59
		self::FIELDS_LIMIT        => '10000',
60
		self::ELASTIC_VER_BELOW66 => '0',
61
		self::ANALYZER_TOKENIZER  => 'standard'
62
	];
63
64
	/** @var IConfig */
65
	private $config;
66
67
	/** @var string */
68
	private $userId;
69
70
	/** @var MiscService */
71
	private $miscService;
72
73
74
	/**
75
	 * ConfigService constructor.
76
	 *
77
	 * @param IConfig $config
78
	 * @param string $userId
79
	 * @param MiscService $miscService
80
	 */
81
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
82
		$this->config = $config;
83
		$this->userId = $userId;
84
		$this->miscService = $miscService;
85
	}
86
87
88
	/**
89
	 * @return array
90
	 */
91
	public function getConfig(): array {
92
		$keys = array_keys($this->defaults);
93
		$data = [];
94
95
		foreach ($keys as $k) {
96
			$data[$k] = $this->getAppValue($k);
97
		}
98
99
		return $data;
100
	}
101
102
103
	/**
104
	 * @param array $save
105
	 */
106
	public function setConfig(array $save) {
107
		$keys = array_keys($this->defaults);
108
109
		foreach ($keys as $k) {
110
			if (array_key_exists($k, $save)) {
111
				$this->setAppValue($k, $save[$k]);
112
			}
113
		}
114
	}
115
116
117
	/**
118
	 * @return array
119
	 * @throws ConfigurationException
120
	 */
121
	public function getElasticHost(): array {
122
123
		$strHost = $this->getAppValue(self::ELASTIC_HOST);
124
		if ($strHost === '') {
125
			throw new ConfigurationException(
126
				'Your ElasticSearchPlatform is not configured properly'
127
			);
128
		}
129
130
		$hosts = explode(',', $strHost);
131
132
		return array_map('trim', $hosts);
133
	}
134
135
136
	/**
137
	 * @return string
138
	 * @throws ConfigurationException
139
	 */
140
	public function getElasticIndex(): string {
141
142
		$index = $this->getAppValue(self::ELASTIC_INDEX);
143
		if ($index === '') {
144
			throw new ConfigurationException(
145
				'Your ElasticSearchPlatform is not configured properly'
146
			);
147
		}
148
149
		return $index;
150
	}
151
152
153
	/**
154
	 * Get a value by key
155
	 *
156
	 * @param string $key
157
	 *
158
	 * @return string
159
	 */
160 View Code Duplication
	public function getAppValue(string $key): string {
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...
161
		$defaultValue = null;
162
		if (array_key_exists($key, $this->defaults)) {
163
			$defaultValue = $this->defaults[$key];
164
		}
165
166
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
167
	}
168
169
	/**
170
	 * Set a value by key
171
	 *
172
	 * @param string $key
173
	 * @param string $value
174
	 */
175
	public function setAppValue(string $key, string $value) {
176
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
177
	}
178
179
	/**
180
	 * remove a key
181
	 *
182
	 * @param string $key
183
	 *
184
	 * @return string
185
	 */
186
	public function deleteAppValue(string $key): string {
187
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
188
	}
189
190
	/**
191
	 * Get a user value by key
192
	 *
193
	 * @param string $key
194
	 *
195
	 * @return string
196
	 */
197 View Code Duplication
	public function getUserValue(string $key): string {
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...
198
		$defaultValue = null;
199
		if (array_key_exists($key, $this->defaults)) {
200
			$defaultValue = $this->defaults[$key];
201
		}
202
203
		return $this->config->getUserValue(
204
			$this->userId, Application::APP_NAME, $key, $defaultValue
205
		);
206
	}
207
208
	/**
209
	 * Set a user value by key
210
	 *
211
	 * @param string $key
212
	 * @param string $value
213
	 *
214
	 * @throws PreConditionNotMetException
215
	 */
216
	public function setUserValue(string $key, string $value) {
217
		$this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
218
	}
219
220
	/**
221
	 * Get a user value by key and user
222
	 *
223
	 * @param string $userId
224
	 * @param string $key
225
	 *
226
	 * @return string
227
	 */
228
	public function getValueForUser(string $userId, string $key): string {
229
		return $this->config->getUserValue($userId, Application::APP_NAME, $key);
230
	}
231
232
	/**
233
	 * Set a user value by key
234
	 *
235
	 * @param string $userId
236
	 * @param string $key
237
	 * @param string $value
238
	 *
239
	 * @throws PreConditionNotMetException
240
	 */
241
	public function setValueForUser($userId, $key, $value) {
242
		$this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
243
	}
244
245
246
	/**
247
	 * @return int
248
	 */
249
	public function getNcVersion(): int {
250
		$ver = Util::getVersion();
251
252
		return $ver[0];
253
	}
254
255
}
256
257