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

Gateway::getProvider()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 4
nop 1
crap 12
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
		$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
	public function createSettings(): Settings {
58
		try {
59
			$settings = $this->getProvider()->getSettings();
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on OCA\TwoFactorGateway\Pro...gram\Provider\IProvider. Since it exists in all sub-types, consider adding an abstract or default implementation to OCA\TwoFactorGateway\Pro...gram\Provider\IProvider. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
			$settings = $this->getProvider()->/** @scrutinizer ignore-call */ getSettings();
Loading history...
60
		} catch (ConfigurationException) {
61
			$settings = new Settings(
62
				name: 'Telegram',
63
			);
64
		}
65
		return $settings;
66
	}
67
68
	#[\Override]
69
	public function isComplete(?Settings $settings = null): bool {
70
		if ($settings === null) {
71
			try {
72
				$provider = $this->getProvider();
73
			} catch (ConfigurationException) {
74
				return false;
75
			}
76
			$settings = $provider->getSettings();
77
		}
78
		return parent::isComplete($settings);
79
	}
80
81
	#[\Override]
82
	public function remove(?Settings $settings = null): void {
83
		if (!is_object($settings)) {
84
			$settings = $this->getSettings();
85
		}
86
		parent::remove($settings);
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