Completed
Push — master ( 261493...1ee18f )
by Vitor
19s queued 13s
created

Configure::configureSms()   D

Complexity

Conditions 15
Paths 15

Size

Total Lines 251
Code Lines 173

Duplication

Lines 0
Ratio 0 %

Importance

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