Passed
Pull Request — master (#640)
by Vitor
03:39
created

SMSGlobal::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 23
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2021 Pascal Clémot <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\Drivers;
11
12
use Exception;
13
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
14
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\AProvider;
15
use OCP\Http\Client\IClient;
16
use OCP\Http\Client\IClientService;
17
18
/**
19
 * @method string getUrl()
20
 * @method static setUrl(string $url)
21
 * @method string getUser()
22
 * @method static setUser(string $user)
23
 * @method string getPassword()
24
 * @method static setPassword(string $password)
25
 */
26
class SMSGlobal extends AProvider {
27
	public const SCHEMA = [
28
		'name' => 'SMSGlobal',
29
		'fields' => [
30
			['field' => 'url',      'prompt' => 'Please enter your SMSGlobal http-api:', 'default' => 'https://api.smsglobal.com/http-api.php'],
31
			['field' => 'user',     'prompt' => 'Please enter your SMSGlobal username (for http-api):'],
32
			['field' => 'password', 'prompt' => 'Please enter your SMSGlobal password (for http-api):'],
33
		],
34
	];
35
	private IClient $client;
36
37
	public function __construct(
38
		IClientService $clientService,
39
	) {
40
		$this->client = $clientService->newClient();
41
	}
42
43
	#[\Override]
44
	public function send(string $identifier, string $message) {
45
		$to = str_replace('+', '', $identifier);
46
47
		try {
48
			$this->client->get(
49
				$this->getUrl(),
50
				[
51
					'query' => [
52
						'action' => 'sendsms',
53
						'user' => $this->getUser(),
54
						'password' => $this->getPassword(),
55
						'origin' => 'nextcloud',
56
						'from' => 'nextcloud',
57
						'to' => $to,
58
						'text' => $message,
59
						'clientcharset' => 'UTF-8',
60
						'detectcharset' => 1
61
					],
62
				]
63
			);
64
		} catch (Exception $ex) {
65
			throw new MessageTransmissionException();
66
		}
67
	}
68
}
69