Completed
Push — master ( 95c290...6dc686 )
by Christoph
14s
created

Configure::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Command;
25
26
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
27
use OCA\TwoFactorGateway\Service\Gateway\Signal\Gateway as SignalGateway;
28
use OCA\TwoFactorGateway\Service\Gateway\Signal\GatewayConfig as SignalConfig;
29
use OCA\TwoFactorGateway\Service\Gateway\SMS\Gateway as SMSGateway;
30
use OCA\TwoFactorGateway\Service\Gateway\SMS\GatewayConfig as SMSConfig;
31
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlaySMSConfig;
32
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig;
33
use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
34
use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig;
35
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
use Symfony\Component\Console\Question\Question;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Question\Question was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
41
class Configure extends Command {
42
43
	/** @var SignalGateway */
44
	private $signalGateway;
45
46
	/** @var SMSGateway */
47
	private $smsGateway;
48
49
	/** @var TelegramGateway */
50
	private $telegramGateway;
51
52
	public function __construct(SignalGateway $signalGateway,
53
								SMSGateway $smsGateway,
54
								TelegramGateway $telegramGateway) {
55
		parent::__construct('twofactorauth:gateway:configure');
56
		$this->signalGateway = $signalGateway;
57
		$this->smsGateway = $smsGateway;
58
		$this->telegramGateway = $telegramGateway;
59
60
		$this->addArgument(
61
			'gateway',
62
			InputArgument::REQUIRED,
63
			'The identifier (e.g. phone number) of the recipient'
64
		);
65
	}
66
67
	protected function execute(InputInterface $input, OutputInterface $output) {
68
		$gatewayName = $input->getArgument('gateway');
69
70
		/** @var IGateway $gateway */
71
		$gateway = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $gateway is dead and can be removed.
Loading history...
72
		switch ($gatewayName) {
73
			case 'signal':
74
				$this->configureSignal($input, $output);
75
				break;
76
			case 'sms':
77
				$this->configureSms($input, $output);
78
				break;
79
			case 'telegram':
80
				$this->configureTelegram($input, $output);
81
				break;
82
			default:
83
				$output->writeln("<error>Invalid gateway $gatewayName</error>");
84
				return;
85
		}
86
	}
87
88
	private function configureSignal(InputInterface $input, OutputInterface $output) {
89
		$helper = $this->getHelper('question');
90
		$urlQuestion = new Question('Please enter the URL of the Signal gateway (leave blank to use default): ', 'http://localhost:5000');
91
		$url = $helper->ask($input, $output, $urlQuestion);
92
		$output->writeln("Using $url.");
93
94
		/** @var SignalConfig $config */
95
		$config = $this->signalGateway->getConfig();
96
97
		$config->setUrl($url);
98
	}
99
100
	private function configureSms(InputInterface $input, OutputInterface $output) {
101
		$helper = $this->getHelper('question');
102
		$providerQuestion = new Question('Please choose a SMS provider (websms, playsms): ', 'websms');
103
		$provider = $helper->ask($input, $output, $providerQuestion);
104
105
		/** @var SMSConfig $config */
106
		$config = $this->smsGateway->getConfig();
107
		switch ($provider) {
108
			case 'websms':
109
				$config->setProvider($provider);
110
				/** @var WebSmsConfig $providerConfig */
111
				$providerConfig = $config->getProvider()->getConfig();
112
113
				$usernameQuestion = new Question('Please enter your websms.de username: ');
114
				$username = $helper->ask($input, $output, $usernameQuestion);
115
				$passwordQuestion = new Question('Please enter your websms.de password: ');
116
				$password = $helper->ask($input, $output, $passwordQuestion);
117
118
				$providerConfig->setUser($username);
119
				$providerConfig->setPassword($password);
120
121
				break;
122
			case 'playsms':
123
				$config->setProvider($provider);
124
				/** @var PlaySMSConfig $providerConfig */
125
				$providerConfig = $config->getProvider()->getConfig();
126
127
				$urlQuestion = new Question('Please enter your PlaySMS URL: ');
128
				$url = $helper->ask($input, $output, $urlQuestion);
129
				$usernameQuestion = new Question('Please enter your PlaySMS username: ');
130
				$username = $helper->ask($input, $output, $usernameQuestion);
131
				$passwordQuestion = new Question('Please enter your PlaySMS password: ');
132
				$password = $helper->ask($input, $output, $passwordQuestion);
133
134
				$providerConfig->setUrl($url);
135
				$providerConfig->setUser($username);
136
				$providerConfig->setPassword($password);
137
138
				break;
139
			default:
140
				$output->writeln("Invalid provider $provider");
141
				break;
142
		}
143
144
	}
145
146
	private function configureTelegram(InputInterface $input, OutputInterface $output) {
147
		$helper = $this->getHelper('question');
148
		$tokenQuestion = new Question('Please enter your Telegram bot token: ');
149
		$token = $helper->ask($input, $output, $tokenQuestion);
150
		$output->writeln("Using $token.");
151
152
		/** @var TelegramConfig $config */
153
		$config = $this->telegramGateway->getConfig();
154
155
		$config->setBotToken($token);
156
	}
157
158
}