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

Remove   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
dl 0
loc 35
ccs 21
cts 21
cp 1
rs 10
c 3
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A execute() 0 13 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