Completed
Push — master ( df0d10...b9c55a )
by Maxence
02:20
created

ConfigService::compareIndexOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/**
3
 * Files_FullTextSearch - Index the content of your files
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\Files_FullTextSearch\Service;
28
29
use OCA\Files_FullTextSearch\AppInfo\Application;
30
use OCA\Files_FullTextSearch\Model\FilesDocument;
31
use OCA\FullTextSearch\Model\Index;
32
use OCP\IConfig;
33
use OCP\PreConditionNotMetException;
34
use OCP\Util;
35
36
class ConfigService {
37
38
	const FILES_LOCAL = 'files_local';
39
	const FILES_EXTERNAL = 'files_external';
40
	const FILES_GROUP_FOLDERS = 'files_group_folders';
41
	const FILES_ENCRYPTED = 'files_encrypted';
42
	const FILES_FEDERATED = 'files_federated';
43
	const FILES_SIZE = 'files_size';
44
	const FILES_OFFICE = 'files_office';
45
	const FILES_PDF = 'files_pdf';
46
	const FILES_IMAGE = 'files_image';
47
	const FILES_AUDIO = 'files_audio';
48
49
	public $defaults = [
50
		self::FILES_LOCAL         => '1',
51
		self::FILES_EXTERNAL      => '0',
52
		self::FILES_GROUP_FOLDERS => '0',
53
		self::FILES_ENCRYPTED     => '0',
54
		self::FILES_FEDERATED     => '0',
55
		self::FILES_SIZE          => '20',
56
		self::FILES_PDF           => '1',
57
		self::FILES_OFFICE        => '1',
58
		self::FILES_IMAGE         => '0',
59
		self::FILES_AUDIO         => '0'
60
	];
61
62
63
	/** @var IConfig */
64
	private $config;
65
66
	/** @var string */
67
	private $userId;
68
69
	/** @var MiscService */
70
	private $miscService;
71
72
	/**
73
	 * ConfigService constructor.
74
	 *
75
	 * @param IConfig $config
76
	 * @param string $userId
77
	 * @param MiscService $miscService
78
	 */
79
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
80
		$this->config = $config;
81
		$this->userId = $userId;
82
		$this->miscService = $miscService;
83
	}
84
85
86
	/**
87
	 * @return array
88
	 */
89
	public function getConfig() {
90
		$keys = array_keys($this->defaults);
91
		$data = [];
92
93
		foreach ($keys as $k) {
94
			$data[$k] = $this->getAppValue($k);
95
		}
96
97
		return $data;
98
	}
99
100
101
	/**
102
	 * @param array $save
103
	 */
104
	public function setConfig($save) {
105
		$keys = array_keys($this->defaults);
106
107
		foreach ($keys as $k) {
108
			if (array_key_exists($k, $save)) {
109
				$this->setAppValue($k, $save[$k]);
110
			}
111
		}
112
	}
113
114
115
	/**
116
	 * Get a value by key
117
	 *
118
	 * @param string $key
119
	 *
120
	 * @return string
121
	 */
122 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...
123
		$defaultValue = null;
124
		if (array_key_exists($key, $this->defaults)) {
125
			$defaultValue = $this->defaults[$key];
126
		}
127
128
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
129
	}
130
131
	/**
132
	 * Set a value by key
133
	 *
134
	 * @param string $key
135
	 * @param string $value
136
	 *
137
	 * @return void
138
	 */
139
	public function setAppValue($key, $value) {
140
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
141
	}
142
143
	/**
144
	 * remove a key
145
	 *
146
	 * @param string $key
147
	 *
148
	 * @return string
149
	 */
150
	public function deleteAppValue($key) {
151
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
152
	}
153
154
155
	/**
156
	 * return if option is enabled.
157
	 *
158
	 * @param $key
159
	 *
160
	 * @return bool
161
	 */
162
	public function optionIsSelected($key) {
163
		return ($this->getAppValue($key) === '1');
164
	}
165
166
167
	/**
168
	 * Get a user value by key
169
	 *
170
	 * @param string $key
171
	 *
172
	 * @return string
173
	 */
174 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...
175
		$defaultValue = null;
176
		if (array_key_exists($key, $this->defaults)) {
177
			$defaultValue = $this->defaults[$key];
178
		}
179
180
		return $this->config->getUserValue(
181
			$this->userId, Application::APP_NAME, $key, $defaultValue
182
		);
183
	}
184
185
	/**
186
	 * Set a user value by key
187
	 *
188
	 * @param string $key
189
	 * @param string $value
190
	 *
191
	 * @return string
192
	 * @throws PreConditionNotMetException
193
	 */
194
	public function setUserValue($key, $value) {
195
		return $this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value);
196
	}
197
198
	/**
199
	 * Get a user value by key and user
200
	 *
201
	 * @param string $userId
202
	 * @param string $key
203
	 *
204
	 * @return string
205
	 */
206
	public function getValueForUser($userId, $key) {
207
		return $this->config->getUserValue($userId, Application::APP_NAME, $key);
208
	}
209
210
	/**
211
	 * Set a user value by key
212
	 *
213
	 * @param string $userId
214
	 * @param string $key
215
	 * @param string $value
216
	 *
217
	 * @return string
218
	 * @throws PreConditionNotMetException
219
	 */
220
	public function setValueForUser($userId, $key, $value) {
221
		return $this->config->setUserValue($userId, Application::APP_NAME, $key, $value);
222
	}
223
224
225
	/**
226
	 * @param string $key
227
	 *
228
	 * @return mixed
229
	 */
230
	public function getSystemValue($key) {
231
		return $this->config->getSystemValue($key);
232
	}
233
234
235
	/**
236
	 * @param FilesDocument $document
237
	 * @param string $option
238
	 */
239
	public function setDocumentIndexOption(FilesDocument $document, $option) {
240
		$document->getIndex()
241
				 ->setOption($option, $this->getAppValue($option));
242
	}
243
244
245
	/**
246
	 * @param Index $index
247
	 *
248
	 * @return bool
249
	 */
250
	public function compareIndexOptions(Index $index) {
251
		$options = $index->getOptions();
252
		$ak = array_keys($options);
253
		foreach ($ak as $k) {
254
			if ($options[$k] !== $this->getAppValue($k)) {
255
				return false;
256
			}
257
		}
258
259
		return true;
260
	}
261
262
	/**
263
	 * return the cloud version.
264
	 * if $complete is true, return a string x.y.z
265
	 *
266
	 * @param boolean $complete
267
	 *
268
	 * @return string|integer
269
	 */
270
	public function getCloudVersion($complete = false) {
271
		$ver = Util::getVersion();
272
273
		if ($complete) {
274
			return implode('.', $ver);
275
		}
276
277
		return $ver[0];
278
	}
279
}
280