Passed
Pull Request — master (#505)
by Vitor
08:00
created

SerwerSMS::send()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 19
c 2
b 0
f 2
dl 0
loc 26
rs 9.6333
cc 3
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Paweł Kuffel <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway for SerwerSMS.pl
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 SerwerSMS implements IProvider {
32
	public const PROVIDER_ID = 'serwersms';
33
34
	/** @var IClient */
35
	private $client;
36
37
	/** @var SerwerSMSConfig */
38
	private $config;
39
40
	public function __construct(IClientService $clientService,
41
							SerwerSMSConfig $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
		$login = $config->getLogin();
55
		$password = $config->getPassword();
56
		$sender = $config->getSender();
57
		try {
58
			$response = $this->client->post('https://api2.serwersms.pl/messages/send_sms', [
59
				'headers' => [
60
					'Content-Type' => 'application/json',
61
				],
62
				'json' => [
63
					'username' => $login,
64
					'password' => $password,
65
					'phone' => $identifier,
66
					'text' => $message,
67
					'sender' => $sender,
68
				],
69
			]);
70
71
			$responseData = json_decode($response->getBody(), true);
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type resource; 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

71
			$responseData = json_decode(/** @scrutinizer ignore-type */ $response->getBody(), true);
Loading history...
72
73
			if ($responseData['success'] !== true) {
74
				throw new SmsTransmissionException();
75
			}
76
		} catch (Exception $ex) {
77
			throw new SmsTransmissionException();
78
		}
79
	}
80
81
	/**
82
	 * @return SerwerSMSConfig
83
	 */
84
	public function getConfig(): IProviderConfig {
85
		return $this->config;
86
	}
87
}
88