Completed
Push — master ( bf3f6e...f59a0d )
by Maxence
02:56 queued 01:15
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
 * Bookmarks_FullTextSearch - Indexing bookmarks
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\Bookmarks_FullTextSearch\Service;
32
33
34
use OCA\Bookmarks_FullTextSearch\AppInfo\Application;
35
use OCP\IConfig;
36
37
38
class ConfigService {
39
40
	const BOOKMARKS_TTL = 'bookmarks_ttl';
41
42
	public $defaults = [
43
		self::BOOKMARKS_TTL => '5'
44
	];
45
46
47
	/** @var IConfig */
48
	private $config;
49
50
	/** @var string */
51
	private $userId;
52
53
	/** @var MiscService */
54
	private $miscService;
55
56
57
	/**
58
	 * ConfigService constructor.
59
	 *
60
	 * @param IConfig $config
61
	 * @param string $userId
62
	 * @param MiscService $miscService
63
	 */
64
	public function __construct(IConfig $config, $userId, MiscService $miscService) {
65
		$this->config = $config;
66
		$this->userId = $userId;
67
		$this->miscService = $miscService;
68
	}
69
70
71
	/**
72
	 * @return array
73
	 */
74
	public function getConfig(): array {
75
		$keys = array_keys($this->defaults);
76
		$data = [];
77
78
		foreach ($keys as $k) {
79
			$data[$k] = $this->getAppValue($k);
80
		}
81
82
		return $data;
83
	}
84
85
86
	/**
87
	 * @param array $save
88
	 */
89
	public function setConfig(array $save) {
90
		$keys = array_keys($this->defaults);
91
92
		foreach ($keys as $k) {
93
			if (array_key_exists($k, $save)) {
94
				$this->setAppValue($k, $save[$k]);
95
			}
96
		}
97
	}
98
99
100
	/**
101
	 * Get a value by key
102
	 *
103
	 * @param string $key
104
	 *
105
	 * @return string
106
	 */
107
	public function getAppValue(string $key): string {
108
		$defaultValue = null;
109
		if (array_key_exists($key, $this->defaults)) {
110
			$defaultValue = $this->defaults[$key];
111
		}
112
113
		return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue);
114
	}
115
116
117
	/**
118
	 * Set a value by key
119
	 *
120
	 * @param string $key
121
	 * @param string $value
122
	 */
123
	public function setAppValue(string $key, string $value) {
124
		$this->config->setAppValue(Application::APP_NAME, $key, $value);
125
	}
126
127
128
	/**
129
	 * remove a key
130
	 *
131
	 * @param string $key
132
	 */
133
	public function deleteAppValue(string $key) {
134
		$this->config->deleteAppValue(Application::APP_NAME, $key);
135
	}
136
137
138
	/**
139
	 * return if option is enabled.
140
	 *
141
	 * @param $key
142
	 *
143
	 * @return bool
144
	 */
145
	public function optionIsSelected($key): bool {
146
		return ($this->getAppValue($key) === '1');
147
	}
148
149
}
150
151