Passed
Pull Request — master (#505)
by Vitor
08:00
created

Configure::configureSms()   D

Complexity

Conditions 17
Paths 17

Size

Total Lines 287
Code Lines 200

Duplication

Lines 0
Ratio 0 %

Importance

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