SerwerSMS   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 5
eloc 35
dl 0
loc 56
ccs 21
cts 42
cp 0.5
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 26 3
A createSettings() 0 16 1
A __construct() 0 4 1
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 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: 'serwersms',
40 1
			name: 'SerwerSMS',
41 1
			fields: [
42 1
				new FieldDefinition(
43 1
					field: 'login',
44 1
					prompt: 'Please enter your SerwerSMS.pl API login:',
45 1
				),
46 1
				new FieldDefinition(
47 1
					field: 'password',
48 1
					prompt: 'Please enter your SerwerSMS.pl API password:',
49 1
				),
50 1
				new FieldDefinition(
51 1
					field: 'sender',
52 1
					prompt: 'Please enter your SerwerSMS.pl sender name:',
53 1
				),
54 1
			],
55 1
		);
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