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

SerwerSMS::send()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 26
rs 9.6333
c 0
b 0
f 0
cc 3
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Paweł Kuffel <[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 getLogin()
20
 * @method static setLogin(string $login)
21
 * @method string getPassword()
22
 * @method static setPassword(string $password)
23
 * @method string getSender()
24
 * @method static setSender(string $sender)
25
 */
26
class SerwerSMS extends AProvider {
27
	public const SCHEMA = [
28
		'name' => 'SerwerSMS',
29
		'fields' => [
30
			['field' => 'login',    'prompt' => 'Please enter your SerwerSMS.pl API login:'],
31
			['field' => 'password', 'prompt' => 'Please enter your SerwerSMS.pl API password:'],
32
			['field' => 'sender',   'prompt' => 'Please enter your SerwerSMS.pl sender name:'],
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
		$login = $this->getLogin();
46
		$password = $this->getPassword();
47
		$sender = $this->getSender();
48
		try {
49
			$response = $this->client->post('https://api2.serwersms.pl/messages/send_sms', [
50
				'headers' => [
51
					'Content-Type' => 'application/json',
52
				],
53
				'json' => [
54
					'username' => $login,
55
					'password' => $password,
56
					'phone' => $identifier,
57
					'text' => $message,
58
					'sender' => $sender,
59
				],
60
			]);
61
62
			$responseData = json_decode((string)$response->getBody(), true);
63
64
			if ($responseData['success'] !== true) {
65
				throw new MessageTransmissionException();
66
			}
67
		} catch (Exception $ex) {
68
			throw new MessageTransmissionException();
69
		}
70
	}
71
}
72