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

Gateway::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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