Completed
Push — master ( a04533...2c451c )
by Vitor
19s queued 14s
created

GatewayConfig::getOrFail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Rainer Dohmen <[email protected]>
7
 * Nextcloud - Two-factor Gateway for XMPP
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
24
namespace OCA\TwoFactorGateway\Service\Gateway\XMPP;
25
26
use OCA\TwoFactorGateway\AppInfo\Application;
27
use OCA\TwoFactorGateway\Exception\ConfigurationException;
28
use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
29
use OCP\IConfig;
30
31
class GatewayConfig implements IGatewayConfig {
32
	private const expected = [
33
		'xmpp_sender',
34
		'xmpp_password',
35
		'xmpp_server',
36
		'xmpp_username',
37
		'xmpp_method',
38
	];
39
40
	/** @var IConfig */
41
	private $config;
42
43
	public function __construct(IConfig $config) {
44
		$this->config = $config;
45
	}
46
47
	private function getOrFail(string $key): string {
48
		$val = $this->config->getAppValue(Application::APP_ID, $key);
49
		if ($val === '') {
50
			throw new ConfigurationException();
51
		}
52
		return $val;
53
	}
54
55
	public function getSender(): string {
56
		return $this->getOrFail('xmpp_sender');
57
	}
58
59
	public function setSender(string $sender) {
60
		$this->config->setAppValue(Application::APP_ID, 'xmpp_sender', $sender);
61
	}
62
63
	public function getPassword(): string {
64
		return $this->getOrFail('xmpp_password');
65
	}
66
67
	public function setPassword(string $password) {
68
		$this->config->setAppValue(Application::APP_ID, 'xmpp_password', $password);
69
	}
70
71
	public function getServer(): string {
72
		return $this->getOrFail('xmpp_server');
73
	}
74
75
	public function setServer(string $server) {
76
		$this->config->setAppValue(Application::APP_ID, 'xmpp_server', $server);
77
	}
78
79
	public function getUsername(): string {
80
		return $this->getOrFail('xmpp_username');
81
	}
82
83
	public function setUsername(string $username) {
84
		$this->config->setAppValue(Application::APP_ID, 'xmpp_username', $username);
85
	}
86
	public function getMethod(): string {
87
		return $this->getOrFail('xmpp_method');
88
	}
89
90
	public function setMethod(string $method) {
91
		$this->config->setAppValue(Application::APP_ID, 'xmpp_method', $method);
92
	}
93
94
	public function isComplete(): bool {
95
		$set = $this->config->getAppKeys(Application::APP_ID);
96
		return count(array_intersect($set, self::expected)) === count(self::expected);
97
	}
98
99
	public function remove() {
100
		foreach (self::expected as $key) {
101
			$this->config->deleteAppValue(Application::APP_ID, $key);
102
		}
103
	}
104
}
105