Completed
Push — master ( 8024bc...f8f0d2 )
by Vitor
14s
created

SMSGlobal::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 OCA\TwoFactorGateway\Provider\FieldDefinition;
16
use OCA\TwoFactorGateway\Provider\Settings;
17
use OCP\Http\Client\IClient;
18
use OCP\Http\Client\IClientService;
19
20
/**
21
 * @method string getUrl()
22
 * @method static setUrl(string $url)
23
 * @method string getUser()
24
 * @method static setUser(string $user)
25
 * @method string getPassword()
26
 * @method static setPassword(string $password)
27
 */
28
class SMSGlobal extends AProvider {
29
	private IClient $client;
30
31 1
	public function __construct(
32
		IClientService $clientService,
33
	) {
34 1
		$this->client = $clientService->newClient();
35
	}
36
37 1
	public function createSettings(): Settings {
38 1
		return new Settings(
39 1
			id: 'smsglobal',
40 1
			name: 'SMSGlobal',
41 1
			fields: [
42 1
				new FieldDefinition(
43 1
					field: 'url',
44 1
					prompt: 'Please enter your SMSGlobal http-api:',
45 1
					default: 'https://api.smsglobal.com/http-api.php',
46 1
				),
47 1
				new FieldDefinition(
48 1
					field: 'user',
49 1
					prompt: 'Please enter your SMSGlobal username (for http-api):',
50 1
				),
51 1
				new FieldDefinition(
52 1
					field: 'password',
53 1
					prompt: 'Please enter your SMSGlobal password (for http-api):',
54 1
				),
55 1
			]
56 1
		);
57
	}
58
59
	#[\Override]
60
	public function send(string $identifier, string $message) {
61
		$to = str_replace('+', '', $identifier);
62
63
		try {
64
			$this->client->get(
65
				$this->getUrl(),
66
				[
67
					'query' => [
68
						'action' => 'sendsms',
69
						'user' => $this->getUser(),
70
						'password' => $this->getPassword(),
71
						'origin' => 'nextcloud',
72
						'from' => 'nextcloud',
73
						'to' => $to,
74
						'text' => $message,
75
						'clientcharset' => 'UTF-8',
76
						'detectcharset' => 1
77
					],
78
				]
79
			);
80
		} catch (Exception $ex) {
81
			throw new MessageTransmissionException();
82
		}
83
	}
84
}
85