Passed
Push — master ( 2479a1...29894a )
by Vitor
02:30 queued 41s
created

ClickatellPortalConfig::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 Christian Schrötter <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author André Fondse <[email protected]>
9
 *
10
 * Nextcloud - Two-factor Gateway for Telegram
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider;
27
28
use OCA\TwoFactorGateway\AppInfo\Application;
29
use OCA\TwoFactorGateway\Exception\ConfigurationException;
30
use OCP\IConfig;
31
32
class ClickatellPortalConfig implements IProviderConfig {
33
	public const expected = [
34
		'clickatell_portal_apikey',
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_NAME, $key, null);
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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 getApiKey(): string {
53
		return $this->getOrFail('clickatell_portal_apikey');
54
	}
55
56
	public function setApiKey(string $apiKey) {
57
		$this->config->setAppValue(Application::APP_NAME, 'clickatell_portal_apikey', $apiKey);
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
	}
59
60
	public function getFromNumber() /* ?string */
61
	{
62
		return $this->config->getAppValue(Application::APP_NAME, 'clickatell_portal_from', null);
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
	}
64
65
	public function setFromNumber(string $fromNumber) {
66
		$this->config->setAppValue(Application::APP_NAME, 'clickatell_portal_from', $fromNumber);
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
	}
68
69
	public function deleteFromNumber() {
70
		$this->config->deleteAppValue(Application::APP_NAME, 'clickatell_portal_from');
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
	}
72
73
	public function isComplete(): bool {
74
		$set = $this->config->getAppKeys(Application::APP_NAME);
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
		return count(array_intersect($set, self::expected)) === count(self::expected);
76
	}
77
78
	public function remove() {
79
		foreach (self::expected as $key) {
80
			$this->config->deleteAppValue(Application::APP_NAME, $key);
0 ignored issues
show
Bug introduced by
The constant OCA\TwoFactorGateway\AppInfo\Application::APP_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
81
		}
82
	}
83
}
84