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

Gateway   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 18
eloc 56
c 1
b 1
f 0
dl 0
loc 98
ccs 19
cts 57
cp 0.3333
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 6 2
A setProvider() 0 2 1
A send() 0 3 1
A __construct() 0 5 1
A cliConfigure() 0 36 5
A getProvider() 0 10 3
A isComplete() 0 11 3
A createSettings() 0 10 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 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
		$providers = [];
42
		foreach ($namespaces as $ns) {
43
			$provider = $this->smsProviderFactory->get($ns);
44
			$providers[] = $this->smsProviderFactory->get($ns);
45
			$names[] = $provider->getSettings()->name;
46
		}
47
48
		$helper = new QuestionHelper();
49
		$choiceQuestion = new ChoiceQuestion('Please choose a SMS provider:', $names);
50
		$name = $helper->ask($input, $output, $choiceQuestion);
51
		$selectedIndex = array_search($name, $names);
52
53
		$provider = $providers[$selectedIndex];
54
55
		foreach ($provider->getSettings()->fields as $field) {
56
			$id = $field->field;
57
			$prompt = $field->prompt . ' ';
58
			$defaultVal = $field->default ?? null;
59
			$optional = (bool)($field->optional ?? false);
60
61
			$answer = (string)$helper->ask($input, $output, new Question($prompt, $defaultVal));
62
63
			if ($optional && $answer === '') {
64
				$method = 'delete' . $this->toCamel($id);
65
				$provider->{$method}();
66
				continue;
67
			}
68
69
			$method = 'set' . $this->toCamel($id);
70
			$provider->{$method}($answer);
71
		}
72
		return 0;
73
	}
74
75 1
	#[\Override]
76
	public function createSettings(): Settings {
77
		try {
78 1
			$settings = $this->getProvider()->getSettings();
79 1
		} catch (ConfigurationException) {
80 1
			$settings = new Settings(
81 1
				name: 'SMS',
82 1
			);
83
		}
84 1
		return $settings;
85
	}
86
87 1
	#[\Override]
88
	public function isComplete(?Settings $settings = null): bool {
89 1
		if ($settings === null) {
90
			try {
91 1
				$provider = $this->getProvider();
92 1
			} catch (ConfigurationException) {
93 1
				return false;
94
			}
95
			$settings = $provider->getSettings();
96
		}
97
		return parent::isComplete($settings);
98
	}
99
100
	#[\Override]
101
	public function remove(?Settings $settings = null): void {
102
		if (!is_object($settings)) {
103
			$settings = $this->getSettings();
104
		}
105
		parent::remove($settings);
106
	}
107
108 1
	public function getProvider(string $providerName = ''): IProvider {
109 1
		if ($providerName) {
110
			$this->setProvider($providerName);
111
		}
112 1
		$providerName = $this->appConfig->getValueString(Application::APP_ID, 'sms_provider_name');
113 1
		if ($providerName === '') {
114 1
			throw new ConfigurationException();
115
		}
116
117
		return $this->smsProviderFactory->get($providerName);
118
	}
119
120
	public function setProvider(string $provider): void {
121
		$this->appConfig->setValueString(Application::APP_ID, 'sms_provider_name', $provider);
122
	}
123
}
124