Passed
Pull Request — master (#686)
by Vitor
04:21
created

Gateway::isComplete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\SMS;
11
12
use OCA\TwoFactorGateway\AppInfo\Application;
13
use OCA\TwoFactorGateway\Exception\ConfigurationException;
14
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\IProvider;
15
use OCA\TwoFactorGateway\Provider\Gateway\AGateway;
16
use OCA\TwoFactorGateway\Provider\Settings;
17
use OCP\IAppConfig;
18
use Symfony\Component\Console\Helper\QuestionHelper;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Question\ChoiceQuestion;
22
use Symfony\Component\Console\Question\Question;
23
24
class Gateway extends AGateway {
25
	public function __construct(
26
		public IAppConfig $appConfig,
27
		private Factory $smsProviderFactory,
28
	) {
29
		parent::__construct($appConfig);
30
	}
31
32
	#[\Override]
33
	public function send(string $identifier, string $message, array $extra = []): void {
34
		$this->getProvider()->send($identifier, $message);
35
	}
36
37
	#[\Override]
38
	final public function cliConfigure(InputInterface $input, OutputInterface $output): int {
39
		$namespaces = $this->smsProviderFactory->getFqcnList();
40
		$names = [];
41
		foreach ($namespaces as $ns) {
42
			$provider = $this->smsProviderFactory->get($ns);
43
			$names[] = $provider->getSettings()->name;
44
		}
45
46
		$helper = new QuestionHelper();
47
		$choiceQuestion = new ChoiceQuestion('Please choose a SMS provider:', $names);
48
		$name = $helper->ask($input, $output, $choiceQuestion);
49
		$selectedIndex = array_search($name, $names);
50
		$schema = $names[$selectedIndex];
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
51
52
		$provider = $this->getProvider($namespaces[$selectedIndex]::getProviderId());
53
54
		foreach ($provider->fields as $field) {
0 ignored issues
show
Bug introduced by
Accessing fields on the interface OCA\TwoFactorGateway\Pro...\SMS\Provider\IProvider suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
			$id = $field->field;
56
			$prompt = $field->prompt . ' ';
57
			$defaultVal = $field->default ?? null;
58
			$optional = (bool)($field->optional ?? false);
59
60
			$answer = (string)$helper->ask($input, $output, new Question($prompt, $defaultVal));
61
62
			if ($optional && $answer === '') {
63
				$method = 'delete' . $this->toCamel($id);
64
				$provider->{$method}();
65
				continue;
66
			}
67
68
			$method = 'set' . $this->toCamel($id);
69
			$provider->{$method}($answer);
70
		}
71
		return 0;
72
	}
73
74
	public function createSettings(): Settings {
75
		try {
76
			$settings = $this->getProvider()->getSettings();
77
		} catch (ConfigurationException) {
78
			$settings = new Settings(
79
				name: 'SMS',
80
			);
81
		}
82
		return $settings;
83
	}
84
85
	#[\Override]
86
	public function isComplete(?Settings $settings = null): bool {
87
		if ($settings === null) {
88
			try {
89
				$provider = $this->getProvider();
90
			} catch (ConfigurationException) {
91
				return false;
92
			}
93
			$settings = $provider->getSettings();
94
		}
95
		return parent::isComplete($settings);
96
	}
97
98
	#[\Override]
99
	public function remove(?Settings $settings = null): void {
100
		if (!is_object($settings)) {
101
			$settings = $this->getSettings();
102
		}
103
		parent::remove($settings);
104
	}
105
106
	public function getProvider(string $providerName = ''): IProvider {
107
		if ($providerName) {
108
			$this->setProvider($providerName);
109
		}
110
		$providerName = $this->appConfig->getValueString(Application::APP_ID, 'sms_provider_name');
111
		if ($providerName === '') {
112
			throw new ConfigurationException();
113
		}
114
115
		return $this->smsProviderFactory->get($providerName);
116
	}
117
118
	public function setProvider(string $provider): void {
119
		$this->appConfig->setValueString(Application::APP_ID, 'sms_provider_name', $provider);
120
	}
121
}
122