Passed
Pull Request — master (#686)
by Vitor
04:21
created

SerwerSMS::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 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 getLogin()
22
 * @method static setLogin(string $login)
23
 * @method string getPassword()
24
 * @method static setPassword(string $password)
25
 * @method string getSender()
26
 * @method static setSender(string $sender)
27
 */
28
class SerwerSMS extends AProvider {
29
	private IClient $client;
30
31
	public function __construct(
32
		IClientService $clientService,
33
	) {
34
		$this->client = $clientService->newClient();
35
	}
36
37
	public function createSettings(): Settings {
38
		return new Settings(
39
			id: 'serwersms',
40
			name: 'SerwerSMS',
41
			fields: [
42
				new FieldDefinition(
43
					field: 'login',
44
					prompt: 'Please enter your SerwerSMS.pl API login:',
45
				),
46
				new FieldDefinition(
47
					field: 'password',
48
					prompt: 'Please enter your SerwerSMS.pl API password:',
49
				),
50
				new FieldDefinition(
51
					field: 'sender',
52
					prompt: 'Please enter your SerwerSMS.pl sender name:',
53
				),
54
			],
55
		);
56
	}
57
58
	#[\Override]
59
	public function send(string $identifier, string $message) {
60
		$login = $this->getLogin();
61
		$password = $this->getPassword();
62
		$sender = $this->getSender();
63
		try {
64
			$response = $this->client->post('https://api2.serwersms.pl/messages/send_sms', [
65
				'headers' => [
66
					'Content-Type' => 'application/json',
67
				],
68
				'json' => [
69
					'username' => $login,
70
					'password' => $password,
71
					'phone' => $identifier,
72
					'text' => $message,
73
					'sender' => $sender,
74
				],
75
			]);
76
77
			$responseData = json_decode((string)$response->getBody(), true);
78
79
			if ($responseData['success'] !== true) {
80
				throw new MessageTransmissionException();
81
			}
82
		} catch (Exception $ex) {
83
			throw new MessageTransmissionException();
84
		}
85
	}
86
}
87