SMSApi   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 36.96%

Importance

Changes 0
Metric Value
wmc 6
eloc 41
dl 0
loc 65
ccs 17
cts 46
cp 0.3696
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 39 4
A __construct() 0 4 1
A createSettings() 0 12 1
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 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 getToken()
22
 * @method static setToken(string $token)
23
 * @method string getSender()
24
 * @method static setSender(string $sender)
25
 */
26
class SMSApi extends AProvider {
27
	private IClient $client;
28
29 1
	public function __construct(
30
		IClientService $clientService,
31
	) {
32 1
		$this->client = $clientService->newClient();
33
	}
34
35 1
	public function createSettings(): Settings {
36 1
		return new Settings(
37 1
			id: 'smsapi',
38 1
			name: 'SMSAPI',
39 1
			fields: [
40 1
				new FieldDefinition(
41 1
					field: 'token',
42 1
					prompt: 'Please enter your SMSApi.com API token:',
43 1
				),
44 1
				new FieldDefinition(
45 1
					field: 'sender',
46 1
					prompt: 'Please enter your SMSApi.com sender name:',
47 1
				),
48 1
			]
49 1
		);
50
	}
51
52
	#[\Override]
53
	public function send(string $identifier, string $message) {
54
		$sender = $this->getSender();
55
		$token = $this->getToken();
56
		$url = 'https://api.smsapi.com/sms.do';
57
58
		$params = [
59
			'to' => $identifier,         //destination number
60
			'from' => $sender,             //sendername made in https://ssl.smsapi.com/sms_settings/sendernames
61
			'message' => $message,    		//message content
62
			'format' => 'json',           	//get response in json format
63
		];
64
65
		try {
66
			static $content;
67
68
			$c = curl_init();
69
			curl_setopt($c, CURLOPT_URL, $url);
70
			curl_setopt($c, CURLOPT_POST, true);
71
			curl_setopt($c, CURLOPT_POSTFIELDS, $params);
72
			curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
73
			curl_setopt($c, CURLOPT_HTTPHEADER, [
74
				"Authorization: Bearer $token"
75
			]);
76
77
			$content = curl_exec($c);
78
			if ($content === false) {
79
				throw new MessageTransmissionException();
80
			}
81
			$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...
82
83
			curl_close($c);
84
			$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

84
			$responseData = json_decode(/** @scrutinizer ignore-type */ $content, true);
Loading history...
85
86
			if ($responseData['count'] !== 1) {
87
				throw new MessageTransmissionException();
88
			}
89
		} catch (Exception $ex) {
90
			throw new MessageTransmissionException();
91
		}
92
	}
93
}
94