Completed
Push — master ( 261493...1ee18f )
by Vitor
19s queued 13s
created

SipGateConfig::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 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Claus-Justus Heine <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway for SipGate
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider;
25
26
use OCA\TwoFactorGateway\AppInfo\Application;
27
use OCA\TwoFactorGateway\Exception\ConfigurationException;
28
use OCP\IConfig;
29
30
class SipGateConfig implements IProviderConfig {
31
	private const expected = [
32
		'sipgate_token_id',
33
		'sipgate_access_token',
34
                'sipgate_web_sms_extension',
35
	];
36
37
	/** @var IConfig */
38
	private $config;
39
40
	public function __construct(IConfig $config) {
41
		$this->config = $config;
42
	}
43
44
	private function getOrFail(string $key): string {
45
		$val = $this->config->getAppValue(Application::APP_ID, $key, null);
46
		if (is_null($val)) {
0 ignored issues
show
introduced by
The condition is_null($val) is always false.
Loading history...
47
			throw new ConfigurationException();
48
		}
49
		return $val;
50
	}
51
52
	public function getTokenId(): string {
53
		return $this->getOrFail('sipgate_token_id');
54
	}
55
56
	public function setTokenId(string $tokenId) {
57
		$this->config->setAppValue(Application::APP_ID, 'sipgate_token_id', $tokenId);
58
	}
59
60
	public function getAccessToken(): string {
61
		return $this->getOrFail('sipgate_access_token');
62
	}
63
64
	public function setAccessToken(string $accessToken) {
65
		$this->config->setAppValue(Application::APP_ID, 'sipgate_access_token', $accessToken);
66
	}
67
68
	public function getWebSmsExtension(): string {
69
		return $this->getOrFail('sipgate_web_sms_extension');
70
	}
71
72
	public function setWebSmsExtension(string $webSmsExtension) {
73
		$this->config->setAppValue(Application::APP_ID, 'sipgate_web_sms_extension', $webSmsExtension);
74
	}
75
	public function isComplete(): bool {
76
		$set = $this->config->getAppKeys(Application::APP_ID);
77
		return count(array_intersect($set, self::expected)) === count(self::expected);
78
	}
79
80
	public function remove() {
81
		foreach (self::expected as $key) {
82
			$this->config->deleteAppValue(Application::APP_ID, $key);
83
		}
84
	}
85
}
86