Completed
Push — master ( 458fe9...b685f3 )
by Maxence
01:45
created

ConfigService::isCloudVersionAtLeast()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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