Completed
Push — master ( 0cdbd8...fb0474 )
by Maxence
02:54
created

ConfigService::getCloudVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
39
40
/**
41
 * Class ConfigService
42
 *
43
 * @package OCA\FullTextSearch_ElasticSearch\Service
44
 */
45
class ConfigService {
46
47
48
	const ELASTIC_HOST = 'elastic_host';
49
	const ELASTIC_INDEX = 'elastic_index';
50
	const ANALYZER_TOKENIZER = 'analyzer_tokenizer';
51
52
53
	public $defaults = [
54
		self::ELASTIC_HOST       => '',
55
		self::ELASTIC_INDEX      => '',
56
		self::ANALYZER_TOKENIZER => 'standard'
57
	];
58
59
	/** @var IConfig */
60
	private $config;
61
62
	/** @var string */
63
	private $userId;
64
65
	/** @var MiscService */
66
	private $miscService;
67
68
69
	/**
70
	 * ConfigService constructor.
71
	 *
72
	 * @param IConfig $config
73
	 * @param string $userId
74
	 * @param MiscService $miscService
75
	 */
76
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
77
		$this->config = $config;
78
		$this->userId = $userId;
79
		$this->miscService = $miscService;
80
	}
81
82
83
	/**
84
	 * @return array
85
	 */
86
	public function getConfig(): array {
87
		$keys = array_keys($this->defaults);
88
		$data = [];
89
90
		foreach ($keys as $k) {
91
			$data[$k] = $this->getAppValue($k);
92
		}
93
94
		return $data;
95
	}
96
97
98
	/**
99
	 * @param array $save
100
	 */
101
	public function setConfig(array $save) {
102
		$keys = array_keys($this->defaults);
103
104
		foreach ($keys as $k) {
105
			if (array_key_exists($k, $save)) {
106
				$this->setAppValue($k, $save[$k]);
107
			}
108
		}
109
	}
110
111
112
	/**
113
	 * @return array
114
	 * @throws ConfigurationException
115
	 */
116
	public function getElasticHost(): array {
117
118
		$strHost = $this->getAppValue(self::ELASTIC_HOST);
119
		if ($strHost === '') {
120
			throw new ConfigurationException(
121
				'Your ElasticSearchPlatform is not configured properly'
122
			);
123
		}
124
125
		$hosts = explode(',', $strHost);
126
127
		return array_map('trim', $hosts);
128
	}
129
130
131
	/**
132
	 * @return string
133
	 * @throws ConfigurationException
134
	 */
135
	public function getElasticIndex(): string {
136
137
		$index = $this->getAppValue(self::ELASTIC_INDEX);
138
		if ($index === '') {
139
			throw new ConfigurationException(
140
				'Your ElasticSearchPlatform is not configured properly'
141
			);
142
		}
143
144
		return $index;
145
	}
146
147
148
	/**
149
	 * Get a value by key
150
	 *
151
	 * @param string $key
152
	 *
153
	 * @return string
154
	 */
155 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...
156
		$defaultValue = null;
157
		if (array_key_exists($key, $this->defaults)) {
158
			$defaultValue = $this->defaults[$key];
159
		}
160
161
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
162
	}
163
164
	/**
165
	 * Set a value by key
166
	 *
167
	 * @param string $key
168
	 * @param string $value
169
	 */
170
	public function setAppValue(string $key, string $value) {
171
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
172
	}
173
174
	/**
175
	 * remove a key
176
	 *
177
	 * @param string $key
178
	 *
179
	 * @return string
180
	 */
181
	public function deleteAppValue(string $key): string {
182
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
183
	}
184
185
	/**
186
	 * Get a user value by key
187
	 *
188
	 * @param string $key
189
	 *
190
	 * @return string
191
	 */
192 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...
193
		$defaultValue = null;
194
		if (array_key_exists($key, $this->defaults)) {
195
			$defaultValue = $this->defaults[$key];
196
		}
197
198
		return $this->config->getUserValue(
199
			$this->userId, Application::APP_NAME, $key, $defaultValue
200
		);
201
	}
202
203
	/**
204
	 * Set a user value by key
205
	 *
206
	 * @param string $key
207
	 * @param string $value
208
	 *
209
	 * @throws PreConditionNotMetException
210
	 */
211
	public function setUserValue(string $key, string $value) {
212
		$this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
213
	}
214
215
	/**
216
	 * Get a user value by key and user
217
	 *
218
	 * @param string $userId
219
	 * @param string $key
220
	 *
221
	 * @return string
222
	 */
223
	public function getValueForUser(string $userId, string $key): string {
224
		return $this->config->getUserValue($userId, Application::APP_NAME, $key);
225
	}
226
227
	/**
228
	 * Set a user value by key
229
	 *
230
	 * @param string $userId
231
	 * @param string $key
232
	 * @param string $value
233
	 *
234
	 * @throws PreConditionNotMetException
235
	 */
236
	public function setValueForUser($userId, $key, $value) {
237
		$this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
238
	}
239
240
}
241