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

VoipMsConfig   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
eloc 20
c 1
b 0
f 1
dl 0
loc 55
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 2 1
A getOrFail() 0 6 2
A setPassword() 0 2 1
A setDid() 0 2 1
A isComplete() 0 3 1
A __construct() 0 2 1
A remove() 0 3 2
A getDid() 0 2 1
A getPassword() 0 2 1
A setUser() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Francois Blackburn <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway for Telegram
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 function array_intersect;
27
use OCA\TwoFactorGateway\AppInfo\Application;
28
use OCA\TwoFactorGateway\Exception\ConfigurationException;
29
use OCP\IConfig;
30
31
class VoipMsConfig implements IProviderConfig {
32
33
	const expected = [
34
		'voipms_api_username',
35
		'voipms_api_password',
36
		'voipms_did',
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, null);
48
		if (is_null($val)) {
0 ignored issues
show
introduced by
The condition is_null($val) is always false.
Loading history...
49
			throw new ConfigurationException();
50
		}
51
		return $val;
52
	}
53
54
	public function getUser(): string {
55
		return $this->getOrFail('voipms_api_username');
56
	}
57
58
	public function setUser(string $user) {
59
		$this->config->setAppValue(Application::APP_NAME, 'voipms_api_username', $user);
60
	}
61
62
	public function getPassword(): string {
63
		return $this->getOrFail('voipms_api_password');
64
	}
65
66
	public function setPassword(string $password) {
67
		$this->config->setAppValue(Application::APP_NAME, 'voipms_api_password', $password);
68
	}
69
70
	public function getDid(): string {
71
		return $this->getOrFail('voipms_did');
72
	}
73
74
	public function setDid(string $did) {
75
		$this->config->setAppValue(Application::APP_NAME, 'voipms_did', $did);
76
	}
77
78
	public function isComplete(): bool {
79
		$set = $this->config->getAppKeys(Application::APP_NAME);
80
		return count(array_intersect($set, self::expected)) === count(self::expected);
81
	}
82
	
83
	public function remove() {
84
		foreach(self::expected as $key) {
85
			$this->config->deleteAppValue(Application::APP_NAME, $key);
86
		}
87
	}
88
}
89