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

Gateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 1
	public function __construct(
26
		public IAppConfig $appConfig,
27
		private Factory $smsProviderFactory,
28
	) {
29 1
		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 1
	#[\Override]
75
	public function createSettings(): Settings {
76
		try {
77 1
			$settings = $this->getProvider()->getSettings();
78 1
		} catch (ConfigurationException) {
79 1
			$settings = new Settings(
80 1
				name: 'SMS',
81 1
			);
82
		}
83 1
		return $settings;
84
	}
85
86 1
	#[\Override]
87
	public function isComplete(?Settings $settings = null): bool {
88 1
		if ($settings === null) {
89
			try {
90 1
				$provider = $this->getProvider();
91 1
			} catch (ConfigurationException) {
92 1
				return false;
93
			}
94
			$settings = $provider->getSettings();
95
		}
96
		return parent::isComplete($settings);
97
	}
98
99
	#[\Override]
100
	public function remove(?Settings $settings = null): void {
101
		if (!is_object($settings)) {
102
			$settings = $this->getSettings();
103
		}
104
		parent::remove($settings);
105
	}
106
107 1
	public function getProvider(string $providerName = ''): IProvider {
108 1
		if ($providerName) {
109
			$this->setProvider($providerName);
110
		}
111 1
		$providerName = $this->appConfig->getValueString(Application::APP_ID, 'sms_provider_name');
112 1
		if ($providerName === '') {
113 1
			throw new ConfigurationException();
114
		}
115
116
		return $this->smsProviderFactory->get($providerName);
117
	}
118
119
	public function setProvider(string $provider): void {
120
		$this->appConfig->setValueString(Application::APP_ID, 'sms_provider_name', $provider);
121
	}
122
}
123