Completed
Push — master ( 6bfb4d...93218d )
by Christoph
19s queued 14s
created

GatewayConfig::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 * @author André Fondse <[email protected]>
8
 *
9
 * Nextcloud - Two-factor Gateway for Telegram
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
26
namespace OCA\TwoFactorGateway\Service\Gateway\Telegram;
27
28
use OCA\TwoFactorGateway\AppInfo\Application;
29
use OCA\TwoFactorGateway\Exception\ConfigurationException;
30
use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
31
use OCP\IConfig;
32
33
class GatewayConfig implements IGatewayConfig {
34
35
	const expected = [
36
		'telegram_bot_token',
37
	];
38
39
	/** @var IConfig */
40
	private $config;
41
42
	public function __construct(IConfig $config) {
43
		$this->config = $config;
44
	}
45
46
	private function getOrFail(string $key): string {
47
		$val = $this->config->getAppValue(Application::APP_NAME, $key);
48
		if ($val === '') {
49
			throw new ConfigurationException();
50
		}
51
		return $val;
52
	}
53
54
	public function getBotToken(): string {
55
		return $this->getOrFail('telegram_bot_token');
56
	}
57
58
	public function setBotToken(string $token) {
59
		$this->config->setAppValue(Application::APP_NAME, 'telegram_bot_token', $token);
60
	}
61
62
	public function isComplete(): bool {
63
		$set = $this->config->getAppKeys(Application::APP_NAME);
64
		return count(array_intersect($set, self::expected)) === count(self::expected);
65
	}
66
67
	public function remove() {
68
		foreach(self::expected as $key) {
69
			$this->config->deleteAppValue(Application::APP_NAME, $key);
70
		}
71
	}
72
}
73