Passed
Pull Request — master (#640)
by Vitor
03:36
created

Configure::configureXMPP()   C

Complexity

Conditions 13
Paths 216

Size

Total Lines 56
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 56
rs 5.5833
cc 13
nc 216
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	private array $ids = [];
24
25
	public function __construct(
26
		private Factory $gatewayFactory,
27
	) {
28
		parent::__construct('twofactorauth:gateway:configure');
29
30
		$fqcn = $this->gatewayFactory->getFqcnList();
31
		foreach ($fqcn as $fqcn) {
32
			$this->ids[] = $fqcn::getProviderId();
33
		}
34
35
		$this->addArgument(
36
			'gateway',
37
			InputArgument::OPTIONAL,
38
			'The name of the gateway: ' . implode(', ', $this->ids)
39
		);
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
			return $gateway->cliConfigure($input, $output);
55
		} catch (InvalidProviderException $e) {
56
			$output->writeln("<error>Invalid gateway $gatewayName</error>");
57
			return 1;
58
		}
59
	}
60
}
61