Completed
Push — master ( 8024bc...f8f0d2 )
by Vitor
14s
created

Remove::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.9332
c 2
b 0
f 0
cc 2
nc 2
nop 2
crap 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 OCA\TwoFactorGateway\Provider\Gateway\AGateway;
13
use OCA\TwoFactorGateway\Provider\Gateway\Factory;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\QuestionHelper;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ChoiceQuestion;
20
21
class Remove extends Command {
22
	/** @var AGateway[] */
23
	private array $gateways = [];
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
			$gateway = $this->gatewayFactory->get($fqcn);
33 1
			$this->gateways[$gateway->getSettings()->id] = $gateway;
34
		}
35
36 1
		$this->addArgument(
37 1
			'gateway',
38 1
			InputArgument::OPTIONAL,
39 1
			'The name of the gateway: ' . implode(', ', array_keys($this->gateways))
40 1
		);
41
	}
42
43 4
	#[\Override]
44
	protected function execute(InputInterface $input, OutputInterface $output) {
45 4
		$gatewayName = strtolower((string)$input->getArgument('gateway'));
46 4
		if (!array_key_exists($gatewayName, $this->gateways)) {
47 4
			$helper = new QuestionHelper();
48 4
			$choiceQuestion = new ChoiceQuestion('Please choose a provider:', array_keys($this->gateways));
49 4
			$selected = $helper->ask($input, $output, $choiceQuestion);
50 3
			$gateway = $this->gateways[$selected];
51
		}
52
53 3
		$gateway->remove();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $gateway does not seem to be defined for all execution paths leading up to this point.
Loading history...
54 3
		$output->writeln("Removed configuration for gateway $gatewayName");
55 3
		return 0;
56
	}
57
}
58