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

Remove   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 43.48%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 3
b 0
f 0
dl 0
loc 40
ccs 10
cts 23
cp 0.4348
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 20 3
A __construct() 0 14 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\Command;
11
12
use Exception;
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 Remove extends Command {
23
	private array $ids = [];
24
25 1
	public function __construct(
26
		private Factory $gatewayFactory,
27
	) {
28 1
		parent::__construct('twofactorauth:gateway:remove');
29
30 1
		$fqcn = $this->gatewayFactory->getFqcnList();
31 1
		foreach ($fqcn as $fqcn) {
32 1
			$this->ids[] = $fqcn::getProviderId();
33
		}
34
35 1
		$this->addArgument(
36 1
			'gateway',
37 1
			InputArgument::OPTIONAL,
38 1
			'The name of the gateway: ' . implode(', ', $this->ids)
39 1
		);
40
	}
41
42
	#[\Override]
43
	protected function execute(InputInterface $input, OutputInterface $output) {
44
		$gatewayName = strtolower((string)$input->getArgument('gateway'));
45
		if (!in_array($gatewayName, $this->ids, true)) {
46
			$helper = new QuestionHelper();
47
			$choiceQuestion = new ChoiceQuestion('Please choose a provider:', $this->ids);
48
			$gatewayName = $helper->ask($input, $output, $choiceQuestion);
49
		}
50
51
		try {
52
			/** @var AGateway */
53
			$gateway = $this->gatewayFactory->get($gatewayName);
54
		} catch (Exception $e) {
55
			$output->writeln('<error>' . $e->getMessage() . '</error>');
56
			return 1;
57
		}
58
59
		$gateway->remove();
60
		$output->writeln("Removed configuration for gateway $gatewayName");
61
		return 0;
62
	}
63
}
64