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

Gateway::isComplete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
ccs 5
cts 7
cp 0.7143
rs 10
cc 3
nc 3
nop 1
crap 3.2098
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\Telegram;
11
12
use OCA\TwoFactorGateway\AppInfo\Application;
13
use OCA\TwoFactorGateway\Exception\ConfigurationException;
14
use OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\AProvider;
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
23
class Gateway extends AGateway {
24
25 1
	public function __construct(
26
		public IAppConfig $appConfig,
27
		private Factory $telegramProviderFactory,
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->telegramProviderFactory->getFqcnList();
40
		$names = [];
41
		$providers = [];
42
		foreach ($namespaces as $ns) {
43
			$provider = $this->telegramProviderFactory->get($ns);
44
			$providers[] = $provider;
45
			$names[] = $provider->getSettings()->name;
46
		}
47
48
		$helper = new QuestionHelper();
49
		$choiceQuestion = new ChoiceQuestion('Please choose a Telegram provider:', $names);
50
		$name = $helper->ask($input, $output, $choiceQuestion);
51
		$selectedIndex = array_search($name, $names);
52
53
		$providers[$selectedIndex]->cliConfigure($input, $output);
54
		return 0;
55
	}
56
57 1
	#[\Override]
58
	public function createSettings(): Settings {
59
		try {
60 1
			$settings = $this->getProvider()->getSettings();
61 1
		} catch (ConfigurationException) {
62 1
			$settings = new Settings(
63 1
				name: 'Telegram',
64 1
			);
65
		}
66 1
		return $settings;
67
	}
68
69 1
	#[\Override]
70
	public function isComplete(?Settings $settings = null): bool {
71 1
		if ($settings === null) {
72
			try {
73 1
				$provider = $this->getProvider();
74 1
			} catch (ConfigurationException) {
75 1
				return false;
76
			}
77
			$settings = $provider->getSettings();
78
		}
79
		return parent::isComplete($settings);
80
	}
81
82
	#[\Override]
83
	public function remove(?Settings $settings = null): void {
84
		if (!is_object($settings)) {
85
			$settings = $this->getSettings();
86
		}
87
		parent::remove($settings);
88
	}
89
90 2
	public function getProvider(string $providerName = ''): AProvider {
91 2
		if ($providerName) {
92
			$this->setProvider($providerName);
93
		}
94 2
		$providerName = $this->appConfig->getValueString(Application::APP_ID, 'telegram_provider_name');
95 2
		if ($providerName === '') {
96 2
			throw new ConfigurationException();
97
		}
98
99
		return $this->telegramProviderFactory->get($providerName);
100
	}
101
102
	public function setProvider(string $provider): void {
103
		$this->appConfig->setValueString(Application::APP_ID, 'telegram_provider_name', $provider);
104
	}
105
}
106