Completed
Pull Request — master (#92)
by Christoph
03:15
created

GatewayConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 31
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOrFail() 0 6 2
A getBotToken() 0 2 1
A __construct() 0 2 1
A isComplete() 0 6 1
A setBotToken() 0 2 1
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
	/** @var IConfig */
36
	private $config;
37
38
	public function __construct(IConfig $config) {
39
		$this->config = $config;
40
	}
41
42
	private function getOrFail(string $key): string {
43
		$val = $this->config->getAppValue(Application::APP_NAME, $key, null);
44
		if (is_null($val)) {
0 ignored issues
show
introduced by
The condition is_null($val) is always false.
Loading history...
45
			throw new ConfigurationException();
46
		}
47
		return $val;
48
	}
49
50
	public function getBotToken(): string {
51
		return $this->getOrFail('telegram_bot_token');
52
	}
53
54
	public function setBotToken(string $token) {
55
		$this->config->setAppValue(Application::APP_NAME, 'telegram_bot_token', $token);
56
	}
57
58
	public function isComplete(): bool {
59
		$set = $this->config->getAppKeys(Application::APP_NAME);
60
		$expected = [
61
			'telegram_bot_token',
62
		];
63
		return count(array_intersect($set, $expected)) === count($expected);
64
	}
65
66
67
}