Passed
Pull Request — master (#245)
by
unknown
03:21
created

Configure::configureEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\ClockworkSMSConfig;
32
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\EcallSMSConfig;
33
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlaySMSConfig;
34
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig;
35
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PuzzelSMSConfig;
36
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\HuaweiE3531Config;
37
use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
38
use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig;
39
use OCA\TwoFactorGateway\Service\Gateway\Email\Gateway as EmailGateway;
40
use OCA\TwoFactorGateway\Service\Gateway\Email\GatewayConfig as EmailConfig;
41
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...
42
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...
43
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...
44
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...
45
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...
46
47
class Configure extends Command {
48
49
	/** @var SignalGateway */
50
	private $signalGateway;
51
52
	/** @var SMSGateway */
53
	private $smsGateway;
54
55
	/** @var TelegramGateway */
56
	private $telegramGateway;
57
58
	/** @var EmailGateway */
59
	private $emailGateway;
60
61
	public function __construct(SignalGateway $signalGateway,
62
								SMSGateway $smsGateway,
63
								TelegramGateway $telegramGateway,
64
                                EmailGateway $emailGateway) {
65
		parent::__construct('twofactorauth:gateway:configure');
66
		$this->signalGateway = $signalGateway;
67
		$this->smsGateway = $smsGateway;
68
		$this->telegramGateway = $telegramGateway;
69
        $this->emailGateway = $emailGateway;
70
71
		$this->addArgument(
72
			'gateway',
73
			InputArgument::REQUIRED,
74
			'The name of the gateway, e.g. sms, signal, telegram, email, etc.'
75
		);
76
	}
77
78
	protected function execute(InputInterface $input, OutputInterface $output) {
79
		$gatewayName = $input->getArgument('gateway');
80
81
		/** @var IGateway $gateway */
82
		$gateway = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $gateway is dead and can be removed.
Loading history...
83
		switch ($gatewayName) {
84
			case 'signal':
85
				$this->configureSignal($input, $output);
86
				break;
87
			case 'sms':
88
				$this->configureSms($input, $output);
89
				break;
90
			case 'telegram':
91
				$this->configureTelegram($input, $output);
92
				break;
93
			case 'email':
94
				$this->configureEmail($input, $output);
95
				break;
96
			default:
97
				$output->writeln("<error>Invalid gateway $gatewayName</error>");
98
				return;
99
		}
100
	}
101
102
	private function configureSignal(InputInterface $input, OutputInterface $output) {
103
		$helper = $this->getHelper('question');
104
		$urlQuestion = new Question('Please enter the URL of the Signal gateway (leave blank to use default): ', 'http://localhost:5000');
105
		$url = $helper->ask($input, $output, $urlQuestion);
106
		$output->writeln("Using $url.");
107
108
		/** @var SignalConfig $config */
109
		$config = $this->signalGateway->getConfig();
110
111
		$config->setUrl($url);
112
	}
113
114
	private function configureSms(InputInterface $input, OutputInterface $output) {
115
		$helper = $this->getHelper('question');
116
		$providerQuestion = new Question('Please choose a SMS provider (websms, playsms, clockworksms, puzzelsms, ecallsms, voipms, huawei_e3531): ', 'websms');
117
		$provider = $helper->ask($input, $output, $providerQuestion);
118
119
		/** @var SMSConfig $config */
120
		$config = $this->smsGateway->getConfig();
121
		switch ($provider) {
122
			case 'websms':
123
				$config->setProvider($provider);
124
				/** @var WebSmsConfig $providerConfig */
125
				$providerConfig = $config->getProvider()->getConfig();
126
127
				$usernameQuestion = new Question('Please enter your websms.de username: ');
128
				$username = $helper->ask($input, $output, $usernameQuestion);
129
				$passwordQuestion = new Question('Please enter your websms.de password: ');
130
				$password = $helper->ask($input, $output, $passwordQuestion);
131
132
				$providerConfig->setUser($username);
133
				$providerConfig->setPassword($password);
134
135
				break;
136
			case 'playsms':
137
				$config->setProvider($provider);
138
				/** @var PlaySMSConfig $providerConfig */
139
				$providerConfig = $config->getProvider()->getConfig();
140
141
				$urlQuestion = new Question('Please enter your PlaySMS URL: ');
142
				$url = $helper->ask($input, $output, $urlQuestion);
143
				$usernameQuestion = new Question('Please enter your PlaySMS username: ');
144
				$username = $helper->ask($input, $output, $usernameQuestion);
145
				$passwordQuestion = new Question('Please enter your PlaySMS password: ');
146
				$password = $helper->ask($input, $output, $passwordQuestion);
147
148
				$providerConfig->setUrl($url);
149
				$providerConfig->setUser($username);
150
				$providerConfig->setPassword($password);
151
152
				break;
153
			case 'clockworksms':
154
				$config->setProvider($provider);
155
				/** @var ClockworkSMSConfig $providerConfig */
156
				$providerConfig = $config->getProvider()->getConfig();
157
158
				$apitokenQuestion = new Question('Please enter your clockworksms api token: ');
159
				$apitoken = $helper->ask($input, $output, $apitokenQuestion);
160
161
				$providerConfig->setApiToken($apitoken);
162
163
				break;
164
			case 'puzzelsms':
165
				$config->setProvider($provider);
166
167
				/** @var PuzzelSMSConfig $providerConfig */
168
				$providerConfig = $config->getProvider()->getConfig();
169
170
				$urlQuestion = new Question('Please enter your PuzzelSMS URL: ');
171
				$url = $helper->ask($input, $output, $urlQuestion);
172
173
				$usernameQuestion = new Question('Please enter your PuzzelSMS username: ');
174
				$username = $helper->ask($input, $output, $usernameQuestion);
175
176
				$passwordQuestion = new Question('Please enter your PuzzelSMS password: ');
177
				$password = $helper->ask($input, $output, $passwordQuestion);
178
179
				$serviceQuestion = new Question('Please enter your PuzzelSMS service ID: ');
180
				$serviceId = $helper->ask($input, $output, $serviceQuestion);
181
182
				$providerConfig->setUrl($url);
183
				$providerConfig->setUser($username);
184
				$providerConfig->setPassword($password);
185
				$providerConfig->setServiceId($serviceId);
186
				break;
187
			case 'ecallsms':
188
				$config->setProvider($provider);
189
				/** @var EcallSMSConfig $providerConfig */
190
				$providerConfig = $config->getProvider()->getConfig();
191
192
				$usernameQuestion = new Question('Please enter your eCall.ch username: ');
193
				$username = $helper->ask($input, $output, $usernameQuestion);
194
				$passwordQuestion = new Question('Please enter your eCall.ch password: ');
195
				$password = $helper->ask($input, $output, $passwordQuestion);
196
				$senderIdQuestion = new Question('Please enter your eCall.ch sender ID: ');
197
				$senderId = $helper->ask($input, $output, $senderIdQuestion);
198
199
				$providerConfig->setUser($username);
200
				$providerConfig->setPassword($password);
201
				$providerConfig->setSenderId($senderId);
202
				break;
203
204
			case 'voipms':
205
				$config->setProvider($provider);
206
207
				/** @var VoipMsConfig $providerConfig */
208
				$providerConfig = $config->getProvider()->getConfig();
209
210
				$usernameQuestion = new Question('Please enter your VoIP.ms API username: ');
211
				$username = $helper->ask($input, $output, $usernameQuestion);
212
213
				$passwordQuestion = new Question('Please enter your VoIP.ms API password: ');
214
				$password = $helper->ask($input, $output, $passwordQuestion);
215
216
				$didQuestion = new Question('Please enter your VoIP.ms DID: ');
217
				$did = $helper->ask($input, $output, $didQuestion);
218
219
				$providerConfig->setUser($username);
220
				$providerConfig->setPassword($password);
221
				$providerConfig->setDid($did);
222
				break;
223
224
			case 'huawei_e3531':
225
				$config->setProvider($provider);
226
				/** @var HuaweiE3531Config $providerConfig */
227
				$providerConfig = $config->getProvider()->getConfig();
228
229
				$urlQuestion = new Question('Please enter the base URL of the Huawei E3531 stick: ', 'http://192.168.8.1/api');
230
				$url = $helper->ask($input, $output, $urlQuestion);
231
232
				$providerConfig->setUrl($url);
233
				break;
234
235
			default:
236
				$output->writeln("Invalid provider $provider");
237
				break;
238
		}
239
240
	}
241
242
	private function configureTelegram(InputInterface $input, OutputInterface $output) {
243
		$helper = $this->getHelper('question');
244
		$tokenQuestion = new Question('Please enter your Telegram bot token: ');
245
		$token = $helper->ask($input, $output, $tokenQuestion);
246
		$output->writeln("Using $token.");
247
248
		/** @var TelegramConfig $config */
249
		$config = $this->telegramGateway->getConfig();
250
251
		$config->setBotToken($token);
252
	}
253
254
	private function configureEmail(InputInterface $input, OutputInterface $output) {
255
	}
256
}
257