Test::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 10
c 4
b 1
f 1
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.9332
cc 2
nc 2
nop 2
crap 6
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
			'gateway',
36
			InputArgument::REQUIRED,
37
			'The name of the gateway: ' . implode(', ', $ids)
38
		);
39
		$this->addArgument(
40
			'identifier',
41
			InputArgument::REQUIRED,
42
			'The identifier of the recipient , e.g. phone number, user id, etc.'
43
		);
44
	}
45
46
	#[\Override]
47
	protected function execute(InputInterface $input, OutputInterface $output) {
48
		$gatewayName = $input->getArgument('gateway');
49
		$identifier = $input->getArgument('identifier');
50
51
		try {
52
			$gateway = $this->gatewayFactory->get($gatewayName);
53
		} catch (InvalidProviderException $e) {
54
			$output->writeln("<error>{$e->getMessage()}</error>");
55
			return 1;
56
		}
57
58
		$gateway->send($identifier, 'Test', ['code' => '123456']);
59
		return 0;
60
	}
61
}
62