Passed
Pull Request — master (#640)
by Vitor
14:24
created

Test::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 15
dl 0
loc 20
rs 9.7666
c 4
b 1
f 1
cc 3
nc 3
nop 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\Exception\InvalidProviderException;
13
use OCA\TwoFactorGateway\Provider\Gateway\Factory;
14
use OCP\IUserManager;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class Test extends Command {
21
22
	public function __construct(
23
		private IUserManager $userManager,
24
		private Factory $gatewayFactory,
25
	) {
26
		parent::__construct('twofactorauth:gateway:test');
27
28
		$fqcn = $this->gatewayFactory->getFqcnList();
29
		foreach ($fqcn as $fqcn) {
30
			$ids[] = $fqcn::getProviderId();
31
		}
32
33
		$this->addArgument(
34
			'uid',
35
			InputArgument::REQUIRED,
36
			'The user id'
37
		);
38
		$this->addArgument(
39
			'gateway',
40
			InputArgument::REQUIRED,
41
			'The name of the gateway: ' . implode(', ', $ids)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ids seems to be defined by a foreach iteration on line 29. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
42
		);
43
		$this->addArgument(
44
			'identifier',
45
			InputArgument::REQUIRED,
46
			'The identifier of the recipient , e.g. phone number, user id, etc.'
47
		);
48
	}
49
50
	#[\Override]
51
	protected function execute(InputInterface $input, OutputInterface $output) {
52
		$uid = $input->getArgument('uid');
53
		$user = $this->userManager->get($uid);
54
		if (is_null($user)) {
55
			$output->writeln('<error>Invalid UID</error>');
56
			return 1;
57
		}
58
		$gatewayName = $input->getArgument('gateway');
59
		$identifier = $input->getArgument('identifier');
60
61
		try {
62
			$gateway = $this->gatewayFactory->get($gatewayName);
63
		} catch (InvalidProviderException $e) {
64
			$output->writeln("<error>{$e->getMessage()}</error>");
65
			return 1;
66
		}
67
68
		$gateway->send($user, $identifier, 'Test', ['code' => '123456']);
69
		return 0;
70
	}
71
}
72