Passed
Pull Request — master (#640)
by Vitor
03:36
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
		$ids = [];
29
		$fqcn = $this->gatewayFactory->getFqcnList();
30
		foreach ($fqcn as $fqcn) {
31
			$ids[] = $fqcn::getProviderId();
32
		}
33
34
		$this->addArgument(
35
			'uid',
36
			InputArgument::REQUIRED,
37
			'The user id'
38
		);
39
		$this->addArgument(
40
			'gateway',
41
			InputArgument::REQUIRED,
42
			'The name of the gateway: ' . implode(', ', $ids)
43
		);
44
		$this->addArgument(
45
			'identifier',
46
			InputArgument::REQUIRED,
47
			'The identifier of the recipient , e.g. phone number, user id, etc.'
48
		);
49
	}
50
51
	#[\Override]
52
	protected function execute(InputInterface $input, OutputInterface $output) {
53
		$uid = $input->getArgument('uid');
54
		$user = $this->userManager->get($uid);
55
		if (is_null($user)) {
56
			$output->writeln('<error>Invalid UID</error>');
57
			return 1;
58
		}
59
		$gatewayName = $input->getArgument('gateway');
60
		$identifier = $input->getArgument('identifier');
61
62
		try {
63
			$gateway = $this->gatewayFactory->get($gatewayName);
64
		} catch (InvalidProviderException $e) {
65
			$output->writeln("<error>{$e->getMessage()}</error>");
66
			return 1;
67
		}
68
69
		$gateway->send($user, $identifier, 'Test', ['code' => '123456']);
70
		return 0;
71
	}
72
}
73