Passed
Push — master ( 2479a1...29894a )
by Vitor
02:30 queued 41s
created

Configure::configureSms()   F

Complexity

Conditions 19
Paths 19

Size

Total Lines 304
Code Lines 213

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
eloc 213
c 6
b 2
f 1
dl 0
loc 304
rs 3.6133
cc 19
nc 19
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ClickSendConfig;
32
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ClockworkSMSConfig;
33
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\EcallSMSConfig;
34
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PlaySMSConfig;
35
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SMSGlobalConfig;
36
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\Sms77IoConfig;
37
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\OvhConfig;
38
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\WebSmsConfig;
39
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SipGateConfig;
40
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\PuzzelSMSConfig;
41
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\HuaweiE3531Config;
42
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SpryngSMSConfig;
43
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ClickatellCentralConfig;
44
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\ClickatellPortalConfig;
45
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\VoipbusterConfig;
46
use OCA\TwoFactorGateway\Service\Gateway\SMS\Provider\SerwerSMSConfig;
47
use OCA\TwoFactorGateway\Service\Gateway\Telegram\Gateway as TelegramGateway;
48
use OCA\TwoFactorGateway\Service\Gateway\Telegram\GatewayConfig as TelegramConfig;
49
use Symfony\Component\Console\Command\Command;
50
use Symfony\Component\Console\Input\InputArgument;
51
use Symfony\Component\Console\Input\InputInterface;
52
use Symfony\Component\Console\Output\OutputInterface;
53
use Symfony\Component\Console\Question\Question;
54
55
class Configure extends Command {
56
57
	/** @var SignalGateway */
58
	private $signalGateway;
59
60
	/** @var SMSGateway */
61
	private $smsGateway;
62
63
	/** @var TelegramGateway */
64
	private $telegramGateway;
65
66
	public function __construct(SignalGateway $signalGateway,
67
								SMSGateway $smsGateway,
68
								TelegramGateway $telegramGateway) {
69
		parent::__construct('twofactorauth:gateway:configure');
70
		$this->signalGateway = $signalGateway;
71
		$this->smsGateway = $smsGateway;
72
		$this->telegramGateway = $telegramGateway;
73
74
		$this->addArgument(
75
			'gateway',
76
			InputArgument::REQUIRED,
77
			'The name of the gateway, e.g. sms, signal, telegram, etc.'
78
		);
79
	}
80
81
	protected function execute(InputInterface $input, OutputInterface $output) {
82
		$gatewayName = $input->getArgument('gateway');
83
84
		/** @var IGateway $gateway */
85
		$gateway = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $gateway is dead and can be removed.
Loading history...
86
		switch ($gatewayName) {
87
			case 'signal':
88
				$this->configureSignal($input, $output);
89
				return 0;
90
			case 'sms':
91
				$this->configureSms($input, $output);
92
				return 0;
93
			case 'telegram':
94
				$this->configureTelegram($input, $output);
95
				return 0;
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
117
		$providerQuestion = new Question('Please choose a SMS provider (sipgate, websms, playsms, clockworksms, puzzelsms, ecallsms, voipms, voipbuster, huawei_e3531, spryng, sms77io, ovh, clickatellcentral, clickatellportal, clicksend, serwersms, smsglobal): ', 'websms');
118
		$provider = $helper->ask($input, $output, $providerQuestion);
119
120
		/** @var SMSConfig $config */
121
		$config = $this->smsGateway->getConfig();
122
		switch ($provider) {
123
			case 'websms':
124
				$config->setProvider($provider);
125
				/** @var WebSmsConfig $providerConfig */
126
				$providerConfig = $config->getProvider()->getConfig();
127
128
				$usernameQuestion = new Question('Please enter your websms.de username: ');
129
				$username = $helper->ask($input, $output, $usernameQuestion);
130
				$passwordQuestion = new Question('Please enter your websms.de password: ');
131
				$password = $helper->ask($input, $output, $passwordQuestion);
132
133
				$providerConfig->setUser($username);
134
				$providerConfig->setPassword($password);
135
				break;
136
137
			case 'sipgate':
138
				$config->setProvider($provider);
139
				/** @var SipGateConfig $providerConfig */
140
				$providerConfig = $config->getProvider()->getConfig();
141
142
				$tokenIdQuestion = new Question('Please enter your sipgate token-id: ');
143
				$tokenId = $helper->ask($input, $output, $tokenIdQuestion);
144
				$accessTokenQuestion = new Question('Please enter your sipgate access token: ');
145
				$accessToken = $helper->ask($input, $output, $accessTokenQuestion);
146
				$webSmsExtensionQuestion = new Question('Please enter your sipgate web-sms extension: ');
147
				$webSmsExtension = $helper->ask($input, $output, $webSmsExtensionQuestion);
148
149
				$providerConfig->setTokenId($tokenId);
150
				$providerConfig->setAccessToken($accessToken);
151
				$providerConfig->setWebSmsExtension($webSmsExtension);
152
				break;
153
154
			case 'playsms':
155
				$config->setProvider($provider);
156
				/** @var PlaySMSConfig $providerConfig */
157
				$providerConfig = $config->getProvider()->getConfig();
158
159
				$urlQuestion = new Question('Please enter your PlaySMS URL: ');
160
				$url = $helper->ask($input, $output, $urlQuestion);
161
				$usernameQuestion = new Question('Please enter your PlaySMS username: ');
162
				$username = $helper->ask($input, $output, $usernameQuestion);
163
				$passwordQuestion = new Question('Please enter your PlaySMS password: ');
164
				$password = $helper->ask($input, $output, $passwordQuestion);
165
166
				$providerConfig->setUrl($url);
167
				$providerConfig->setUser($username);
168
				$providerConfig->setPassword($password);
169
				break;
170
171
			case 'clockworksms':
172
				$config->setProvider($provider);
173
				/** @var ClockworkSMSConfig $providerConfig */
174
				$providerConfig = $config->getProvider()->getConfig();
175
176
				$apitokenQuestion = new Question('Please enter your clockworksms api token: ');
177
				$apitoken = $helper->ask($input, $output, $apitokenQuestion);
178
179
				$providerConfig->setApiToken($apitoken);
180
				break;
181
182
			case 'puzzelsms':
183
				$config->setProvider($provider);
184
185
				/** @var PuzzelSMSConfig $providerConfig */
186
				$providerConfig = $config->getProvider()->getConfig();
187
188
				$urlQuestion = new Question('Please enter your PuzzelSMS URL: ');
189
				$url = $helper->ask($input, $output, $urlQuestion);
190
191
				$usernameQuestion = new Question('Please enter your PuzzelSMS username: ');
192
				$username = $helper->ask($input, $output, $usernameQuestion);
193
194
				$passwordQuestion = new Question('Please enter your PuzzelSMS password: ');
195
				$password = $helper->ask($input, $output, $passwordQuestion);
196
197
				$serviceQuestion = new Question('Please enter your PuzzelSMS service ID: ');
198
				$serviceId = $helper->ask($input, $output, $serviceQuestion);
199
200
				$providerConfig->setUrl($url);
201
				$providerConfig->setUser($username);
202
				$providerConfig->setPassword($password);
203
				$providerConfig->setServiceId($serviceId);
204
				break;
205
206
			case 'ecallsms':
207
				$config->setProvider($provider);
208
				/** @var EcallSMSConfig $providerConfig */
209
				$providerConfig = $config->getProvider()->getConfig();
210
211
				$usernameQuestion = new Question('Please enter your eCall.ch username: ');
212
				$username = $helper->ask($input, $output, $usernameQuestion);
213
				$passwordQuestion = new Question('Please enter your eCall.ch password: ');
214
				$password = $helper->ask($input, $output, $passwordQuestion);
215
				$senderIdQuestion = new Question('Please enter your eCall.ch sender ID: ');
216
				$senderId = $helper->ask($input, $output, $senderIdQuestion);
217
218
				$providerConfig->setUser($username);
219
				$providerConfig->setPassword($password);
220
				$providerConfig->setSenderId($senderId);
221
				break;
222
223
			case 'voipms':
224
				$config->setProvider($provider);
225
226
				/** @var VoipMsConfig $providerConfig */
227
				$providerConfig = $config->getProvider()->getConfig();
228
229
				$usernameQuestion = new Question('Please enter your VoIP.ms API username: ');
230
				$username = $helper->ask($input, $output, $usernameQuestion);
231
232
				$passwordQuestion = new Question('Please enter your VoIP.ms API password: ');
233
				$password = $helper->ask($input, $output, $passwordQuestion);
234
235
				$didQuestion = new Question('Please enter your VoIP.ms DID: ');
236
				$did = $helper->ask($input, $output, $didQuestion);
237
238
				$providerConfig->setUser($username);
239
				$providerConfig->setPassword($password);
240
				$providerConfig->setDid($did);
241
				break;
242
243
			case 'voipbuster':
244
				$config->setProvider($provider);
245
246
				/** @var VoipbusterConfig $providerConfig */
247
				$providerConfig = $config->getProvider()->getConfig();
248
249
				$usernameQuestion = new Question('Please enter your Voipbuster API username: ');
250
				$username = $helper->ask($input, $output, $usernameQuestion);
251
252
				$passwordQuestion = new Question('Please enter your Voipbuster API password: ');
253
				$password = $helper->ask($input, $output, $passwordQuestion);
254
255
				$didQuestion = new Question('Please enter your Voipbuster DID: ');
256
				$did = $helper->ask($input, $output, $didQuestion);
257
258
				$providerConfig->setUser($username);
259
				$providerConfig->setPassword($password);
260
				$providerConfig->setDid($did);
261
				break;
262
263
			case 'huawei_e3531':
264
				$config->setProvider($provider);
265
				/** @var HuaweiE3531Config $providerConfig */
266
				$providerConfig = $config->getProvider()->getConfig();
267
268
				$urlQuestion = new Question('Please enter the base URL of the Huawei E3531 stick: ', 'http://192.168.8.1/api');
269
				$url = $helper->ask($input, $output, $urlQuestion);
270
271
				$providerConfig->setUrl($url);
272
				break;
273
274
			case 'spryng':
275
				$config->setProvider($provider);
276
				/** @var SpryngSMSConfig $providerConfig */
277
				$providerConfig = $config->getProvider()->getConfig();
278
279
				$apitokenQuestion = new Question('Please enter your Spryng api token: ');
280
				$apitoken = $helper->ask($input, $output, $apitokenQuestion);
281
282
				$providerConfig->setApiToken($apitoken);
283
				break;
284
285
			case 'sms77io':
286
				$config->setProvider($provider);
287
				/** @var Sms77IoConfig $providerConfig */
288
				$providerConfig = $config->getProvider()->getConfig();
289
290
				$apiKeyQuestion = new Question('Please enter your sms77.io API key: ');
291
				$apiKey = $helper->ask($input, $output, $apiKeyQuestion);
292
293
				$providerConfig->setApiKey($apiKey);
294
				break;
295
296
			case 'ovh':
297
				$config->setProvider($provider);
298
299
				/** @var OvhConfig $providerConfig */
300
				$providerConfig = $config->getProvider()->getConfig();
301
302
				$endpointQ = new Question('Please enter the endpoint to use (ovh-eu, ovh-us, ovh-ca, soyoustart-eu, soyoustart-ca, kimsufi-eu, kimsufi-ca, runabove-ca): ');
303
				$endpoint = $helper->ask($input, $output, $endpointQ);
304
305
				$appKeyQ = new Question('Please enter your application key: ');
306
				$appKey = $helper->ask($input, $output, $appKeyQ);
307
308
				$appSecretQ = new Question('Please enter your application secret: ');
309
				$appSecret = $helper->ask($input, $output, $appSecretQ);
310
311
				$consumerKeyQ = new Question('Please enter your consumer key: ');
312
				$consumerKey = $helper->ask($input, $output, $consumerKeyQ);
313
314
				$accountQ = new Question('Please enter your account (sms-*****): ');
315
				$account = $helper->ask($input, $output, $accountQ);
316
317
				$senderQ = new Question('Please enter your sender: ');
318
				$sender = $helper->ask($input, $output, $senderQ);
319
320
				$providerConfig->setApplicationKey($appKey);
321
				$providerConfig->setApplicationSecret($appSecret);
322
				$providerConfig->setConsumerKey($consumerKey);
323
				$providerConfig->setEndpoint($endpoint);
324
				$providerConfig->setAccount($account);
325
				$providerConfig->setSender($sender);
326
				break;
327
328
			case 'clickatellcentral':
329
				$config->setProvider($provider);
330
				/** @var ClickatellCentralConfig $providerConfig */
331
				$providerConfig = $config->getProvider()->getConfig();
332
333
				$apiQuestion = new Question('Please enter your central.clickatell.com API-ID: ');
334
				$api = $helper->ask($input, $output, $apiQuestion);
335
				$usernameQuestion = new Question('Please enter your central.clickatell.com username: ');
336
				$username = $helper->ask($input, $output, $usernameQuestion);
337
				$passwordQuestion = new Question('Please enter your central.clickatell.com password: ');
338
				$password = $helper->ask($input, $output, $passwordQuestion);
339
340
				$providerConfig->setApi($api);
341
				$providerConfig->setUser($username);
342
				$providerConfig->setPassword($password);
343
				break;
344
345
			case 'clickatellportal':
346
				$config->setProvider($provider);
347
				/** @var ClickatellPortalConfig $providerConfig */
348
				$providerConfig = $config->getProvider()->getConfig();
349
350
				$apiQuestion = new Question('Please enter your portal.clickatell.com API-Key: ');
351
				$apiKey = $helper->ask($input, $output, $apiQuestion);
352
				$fromQuestion = new Question('Please enter your sender number for two-way messaging. Leave it empty for one-way messaging: ');
353
				$fromNumber = $helper->ask($input, $output, $fromQuestion);
354
355
				$providerConfig->setApiKey($apiKey);
356
357
				if (empty($fromNumber)) {
358
					$providerConfig->deleteFromNumber();
359
				} else {
360
					$providerConfig->setFromNumber($fromNumber);
361
				}
362
				break;
363
364
			case 'clicksend':
365
				$config->setProvider($provider);
366
				/** @var ClickSendConfig $providerConfig */
367
				$providerConfig = $config->getProvider()->getConfig();
368
369
				$usernameQuestion = new Question('Please enter your clicksend.com username: ');
370
				$username = $helper->ask($input, $output, $usernameQuestion);
371
				$apiKeyQuestion = new Question('Please enter your clicksend.com API Key (or, if subuser, the password): ');
372
				$apiKey = $helper->ask($input, $output, $apiKeyQuestion);
373
374
				$providerConfig->setUser($username);
375
				$providerConfig->setApiKey($apiKey);
376
				break;
377
378
			case 'smsglobal':
379
				$config->setProvider($provider);
380
				/** @var SMSGlobalConfig $providerConfig */
381
				$providerConfig = $config->getProvider()->getConfig();
382
383
				$urlproposal = 'https://api.smsglobal.com/http-api.php';
384
				$urlQuestion = new Question('Please enter your SMSGlobal http-api:', $urlproposal);
385
				$url = $helper->ask($input, $output, $urlQuestion);
386
				$usernameQuestion = new Question('Please enter your SMSGlobal username (for http-api):');
387
				$username = $helper->ask($input, $output, $usernameQuestion);
388
				$passwordQuestion = new Question('Please enter your SMSGlobal password: (for http-api):');
389
				$password = $helper->ask($input, $output, $passwordQuestion);
390
391
				$providerConfig->setUrl($url);
392
				$providerConfig->setUser($username);
393
				$providerConfig->setPassword($password);
394
				break;
395
396
			case 'serwersms':
397
				$config->setProvider($provider);
398
				/** @var SerwerSMSConfig $providerConfig */
399
				$providerConfig = $config->getProvider()->getConfig();
400
401
				$loginQuestion = new Question('Please enter your SerwerSMS.pl API login: ');
402
				$login = $helper->ask($input, $output, $loginQuestion);
403
				$passwordQuestion = new Question('Please enter your SerwerSMS.pl API password: ');
404
				$password = $helper->ask($input, $output, $passwordQuestion);
405
				$senderQuestion = new Question('Please enter your SerwerSMS.pl sender name: ');
406
				$sender = $helper->ask($input, $output, $senderQuestion);
407
408
				$providerConfig->setLogin($login);
409
				$providerConfig->setPassword($password);
410
				$providerConfig->setSender($sender);
411
				break;
412
413
			default:
414
				$output->writeln("Invalid provider $provider");
415
				break;
416
		}
417
		return 0;
418
	}
419
420
	private function configureTelegram(InputInterface $input, OutputInterface $output) {
421
		$helper = $this->getHelper('question');
422
		$tokenQuestion = new Question('Please enter your Telegram bot token: ');
423
		$token = $helper->ask($input, $output, $tokenQuestion);
424
		$output->writeln("Using $token.");
425
426
		/** @var TelegramConfig $config */
427
		$config = $this->telegramGateway->getConfig();
428
429
		$config->setBotToken($token);
430
	}
431
}
432