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

Remove::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 20
ccs 0
cts 13
cp 0
rs 9.7998
c 2
b 0
f 0
cc 3
nc 4
nop 2
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\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