SMSApi   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 36
dl 0
loc 55
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 39 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Marcin Kot <[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 getToken()
20
 * @method static setToken(string $token)
21
 * @method string getSender()
22
 * @method static setSender(string $sender)
23
 */
24
class SMSApi extends AProvider {
25
	public const SCHEMA = [
26
		'name' => 'SMSAPI',
27
		'fields' => [
28
			['field' => 'token', 'prompt' => 'Please enter your SMSApi.com API token:'],
29
			['field' => 'sender','prompt' => 'Please enter your SMSApi.com sender name:'],
30
		],
31
	];
32
	private IClient $client;
33
34
	public function __construct(
35
		IClientService $clientService,
36
	) {
37
		$this->client = $clientService->newClient();
38
	}
39
40
	#[\Override]
41
	public function send(string $identifier, string $message) {
42
		$sender = $this->getSender();
43
		$token = $this->getToken();
44
		$url = 'https://api.smsapi.com/sms.do';
45
46
		$params = [
47
			'to' => $identifier,         //destination number
48
			'from' => $sender,             //sendername made in https://ssl.smsapi.com/sms_settings/sendernames
49
			'message' => $message,    		//message content
50
			'format' => 'json',           	//get response in json format
51
		];
52
53
		try {
54
			static $content;
55
56
			$c = curl_init();
57
			curl_setopt($c, CURLOPT_URL, $url);
58
			curl_setopt($c, CURLOPT_POST, true);
59
			curl_setopt($c, CURLOPT_POSTFIELDS, $params);
60
			curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
61
			curl_setopt($c, CURLOPT_HTTPHEADER, [
62
				"Authorization: Bearer $token"
63
			]);
64
65
			$content = curl_exec($c);
66
			if ($content === false) {
67
				throw new MessageTransmissionException();
68
			}
69
			$http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
0 ignored issues
show
Unused Code introduced by
The assignment to $http_status is dead and can be removed.
Loading history...
70
71
			curl_close($c);
72
			$responseData = json_decode($content, true);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
			$responseData = json_decode(/** @scrutinizer ignore-type */ $content, true);
Loading history...
73
74
			if ($responseData['count'] !== 1) {
75
				throw new MessageTransmissionException();
76
			}
77
		} catch (Exception $ex) {
78
			throw new MessageTransmissionException();
79
		}
80
	}
81
}
82