SMSApi::send()   A
last analyzed

Complexity

Conditions 3
Paths 13

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 26
c 4
b 2
f 0
dl 0
loc 36
rs 9.504
cc 3
nc 13
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Marcin Kot <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway for SMSApi.com
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider;
25
26
use Exception;
27
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
28
use OCP\Http\Client\IClient;
29
use OCP\Http\Client\IClientService;
30
31
class SMSApi implements IProvider {
32
	public const PROVIDER_ID = 'smsapi.com';
33
34
	/** @var IClient */
35
	private $client;
36
37
	/** @var SMSApiConfig */
38
	private $config;
39
40
	public function __construct(IClientService $clientService,
41
		SMSApiConfig $config) {
42
		$this->client = $clientService->newClient();
43
		$this->config = $config;
44
	}
45
46
	/**
47
	 * @param string $identifier
48
	 * @param string $message
49
	 *
50
	 * @throws SmsTransmissionException
51
	 */
52
	public function send(string $identifier, string $message) {
53
		$config = $this->getConfig();
54
		$sender = $config->getSender();
55
		$token = $config->getToken();
56
		$url = 'https://api.smsapi.com/sms.do';
57
58
		$params = array(
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, array(
74
				"Authorization: Bearer $token"
75
			));
76
77
			$content = curl_exec($c);
78
			$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...
79
80
			curl_close($c);
81
			$responseData = json_decode($content->getBody(), true);
82
83
			if ($responseData['count'] !== 1) {
84
				throw new SmsTransmissionException();
85
			}
86
		} catch (Exception $ex) {
87
			throw new SmsTransmissionException();
88
		}
89
	}
90
91
	/**
92
	 * @return SMSApiConfig
93
	 */
94
	public function getConfig(): IProviderConfig {
95
		return $this->config;
96
	}
97
}
98