Completed
Push — master ( 76b7f4...b4b2b8 )
by Maxence
02:02
created

ConfigService::getValueForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Bookmarks_FullTextSearch - Indexing bookmarks
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\Bookmarks_FullTextSearch\Service;
28
29
use OCA\Bookmarks_FullTextSearch\AppInfo\Application;
30
use OCP\IConfig;
31
use OCP\PreConditionNotMetException;
32
use OCP\Util;
33
34
class ConfigService {
35
36
	const BOOKMARKS_TTL = 'bookmarks_ttl';
37
38
	private $defaults = [
39
		self::BOOKMARKS_TTL => '5'
40
	];
41
42
43
	/** @var IConfig */
44
	private $config;
45
46
	/** @var string */
47
	private $userId;
48
49
	/** @var MiscService */
50
	private $miscService;
51
52
	/**
53
	 * ConfigService constructor.
54
	 *
55
	 * @param IConfig $config
56
	 * @param string $userId
57
	 * @param MiscService $miscService
58
	 */
59
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
60
		$this->config = $config;
61
		$this->userId = $userId;
62
		$this->miscService = $miscService;
63
	}
64
65
66
	/**
67
	 * @return array
68
	 */
69
	public function getConfig() {
70
		$keys = array_keys($this->defaults);
71
		$data = [];
72
73
		foreach ($keys as $k) {
74
			$data[$k] = $this->getAppValue($k);
75
		}
76
77
		return $data;
78
	}
79
80
81
	/**
82
	 * @param array $save
83
	 */
84
	public function setConfig($save) {
85
		$keys = array_keys($this->defaults);
86
87
		foreach ($keys as $k) {
88
			if (array_key_exists($k, $save)) {
89
				$this->setAppValue($k, $save[$k]);
90
			}
91
		}
92
	}
93
94
95
	/**
96
	 * Get a value by key
97
	 *
98
	 * @param string $key
99
	 *
100
	 * @return string
101
	 */
102
	public function getAppValue($key) {
103
		$defaultValue = null;
104
		if (array_key_exists($key, $this->defaults)) {
105
			$defaultValue = $this->defaults[$key];
106
		}
107
108
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
109
	}
110
111
	/**
112
	 * Set a value by key
113
	 *
114
	 * @param string $key
115
	 * @param string $value
116
	 *
117
	 * @return void
118
	 */
119
	public function setAppValue($key, $value) {
120
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
121
	}
122
123
	/**
124
	 * remove a key
125
	 *
126
	 * @param string $key
127
	 *
128
	 * @return string
129
	 */
130
	public function deleteAppValue($key) {
131
		return $this->config->deleteAppValue(Application::APP_NAME, $key);
132
	}
133
134
135
	/**
136
	 * return if option is enabled.
137
	 *
138
	 * @param $key
139
	 *
140
	 * @return bool
141
	 */
142
	public function optionIsSelected($key) {
143
		return ($this->getAppValue($key) === '1');
144
	}
145
146
147
	/**
148
	 * return the cloud version.
149
	 * if $complete is true, return a string x.y.z
150
	 *
151
	 * @param boolean $complete
152
	 *
153
	 * @return string|integer
154
	 */
155
	public function getCloudVersion($complete = false) {
156
		$ver = Util::getVersion();
157
158
		if ($complete) {
159
			return implode('.', $ver);
160
		}
161
162
		return $ver[0];
163
	}
164
}
165