Passed
Pull Request — master (#640)
by Vitor
14:24
created

Gateway::cliConfigure()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 34
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 34
rs 9.2248
c 0
b 0
f 0
cc 5
nc 6
nop 2
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 OCP\IAppConfig;
17
use OCP\IUser;
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 const SCHEMA = [
26
		'name' => 'SMS',
27
	];
28
29
	public function __construct(
30
		public IAppConfig $appConfig,
31
		private Factory $providerFactory,
32
	) {
33
		parent::__construct($appConfig);
34
	}
35
36
	#[\Override]
37
	public function send(IUser $user, string $identifier, string $message, array $extra = []): void {
38
		$this->getProvider()->send($identifier, $message);
39
	}
40
41
	#[\Override]
42
	final public function cliConfigure(InputInterface $input, OutputInterface $output): int {
43
		$namespaces = $this->providerFactory->getFqcnList();
44
		foreach ($namespaces as $ns) {
45
			$schemas[] = $ns::SCHEMA;
46
		}
47
		$names = array_column($schemas, 'name');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $schemas seems to be defined by a foreach iteration on line 44. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
48
49
		$helper = new QuestionHelper();
50
		$choiceQuestion = new ChoiceQuestion('Please choose a SMS provider:', $names);
51
		$name = $helper->ask($input, $output, $choiceQuestion);
52
		$selectedIndex = array_search($name, $names);
53
		$schema = $schemas[$selectedIndex];
54
55
		$provider = $this->getProvider($namespaces[$selectedIndex]::getProviderId());
56
57
		foreach ($schema['fields'] as $field) {
58
			$id = $field['field'];
59
			$prompt = $field['prompt'] . ' ';
60
			$defaultVal = $field['default'] ?? null;
61
			$optional = (bool)($field['optional'] ?? false);
62
63
			$answer = (string)$helper->ask($input, $output, new Question($prompt, $defaultVal));
64
65
			if ($optional && $answer === '') {
66
				$method = 'delete' . $this->toCamel($id);
67
				$provider->{$method}();
68
				continue;
69
			}
70
71
			$method = 'set' . $this->toCamel($id);
72
			$provider->{$method}($answer);
73
		}
74
		return 0;
75
	}
76
77
	#[\Override]
78
	public function isComplete(array $schema = []): bool {
79
		if (empty($schema)) {
80
			try {
81
				$provider = $this->getProvider();
82
			} catch (ConfigurationException) {
83
				return false;
84
			}
85
			$schema = $provider::SCHEMA;
86
		}
87
		return parent::isComplete($schema);
88
	}
89
90
	#[\Override]
91
	public function remove(array $schema = []): void {
92
		if (empty($schema)) {
93
			$schema = static::SCHEMA;
94
		}
95
		parent::remove($schema);
96
	}
97
98
	public function getProvider(string $providerName = ''): IProvider {
99
		if ($providerName) {
100
			$this->setProvider($providerName);
101
		}
102
		$providerName = $this->appConfig->getValueString(Application::APP_ID, 'sms_provider_name');
103
		if ($providerName === '') {
104
			throw new ConfigurationException();
105
		}
106
107
		return $this->providerFactory->get($providerName);
108
	}
109
110
	public function setProvider(string $provider): void {
111
		$this->appConfig->setValueString(Application::APP_ID, 'sms_provider_name', $provider);
112
	}
113
}
114