Configure   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 8
Bugs 3 Features 0
Metric Value
wmc 5
eloc 25
c 8
b 3
f 0
dl 0
loc 39
ccs 19
cts 23
cp 0.8261
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A execute() 0 17 3
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\Command;
11
12
use OCA\TwoFactorGateway\Exception\InvalidProviderException;
13
use OCA\TwoFactorGateway\Provider\Gateway\AGateway;
14
use OCA\TwoFactorGateway\Provider\Gateway\Factory;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Helper\QuestionHelper;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Question\ChoiceQuestion;
21
22
class Configure extends Command {
23
	/** @var AGateway[] */
24
	private array $gateways = [];
25
26 1
	public function __construct(
27
		private Factory $gatewayFactory,
28
	) {
29 1
		parent::__construct('twofactorauth:gateway:configure');
30
31 1
		$fqcnList = $this->gatewayFactory->getFqcnList();
32 1
		foreach ($fqcnList as $fqcn) {
33 1
			$gateway = $this->gatewayFactory->get($fqcn);
34 1
			$this->gateways[$gateway->getProviderId()] = $gateway;
35
		}
36
37 1
		$this->addArgument(
38 1
			'gateway',
39 1
			InputArgument::OPTIONAL,
40 1
			'The name of the gateway: ' . implode(', ', array_keys($this->gateways))
41 1
		);
42
	}
43
44 1
	#[\Override]
45
	protected function execute(InputInterface $input, OutputInterface $output): int {
46 1
		$gatewayName = strtolower((string)$input->getArgument('gateway'));
47 1
		if (!array_key_exists($gatewayName, $this->gateways)) {
48 1
			$helper = new QuestionHelper();
49 1
			$choiceQuestion = new ChoiceQuestion('Please choose a provider:', array_keys($this->gateways));
50 1
			$selectedIndex = $helper->ask($input, $output, $choiceQuestion);
51 1
			$gateway = $this->gateways[$selectedIndex];
52
		} else {
53
			$gateway = $this->gateways[$gatewayName];
54
		}
55
56
		try {
57 1
			return $gateway->cliConfigure($input, $output);
58
		} catch (InvalidProviderException $e) {
59
			$output->writeln("<error>Invalid gateway $gatewayName</error>");
60
			return 1;
61
		}
62
	}
63
}
64