Passed
Pull Request — master (#686)
by Vitor
05:00 queued 15s
created

ClickatellCentral::send()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christian Schrötter <[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 getApi()
20
 * @method static setApi(string $api)
21
 * @method string getUser()
22
 * @method static setUser(string $user)
23
 * @method string getPassword()
24
 * @method static setPassword(string $password)
25
 */
26
class ClickatellCentral extends AProvider {
27
	public const SCHEMA = [
28
		'id' => 'clickatell_central',
29
		'name' => 'Clickatell Central',
30
		'fields' => [
31
			['field' => 'api',      'prompt' => 'Please enter your central.clickatell.com API-ID:'],
32
			['field' => 'user',     'prompt' => 'Please enter your central.clickatell.com username:'],
33
			['field' => 'password', 'prompt' => 'Please enter your central.clickatell.com password:'],
34
		],
35
	];
36
37
	private IClient $client;
38
39
	public function __construct(
40
		IClientService $clientService,
41
	) {
42
		$this->client = $clientService->newClient();
43
	}
44
45
	#[\Override]
46
	public function send(string $identifier, string $message) {
47
		try {
48
			$response = $this->client->get(vsprintf('https://api.clickatell.com/http/sendmsg?user=%s&password=%s&api_id=%u&to=%s&text=%s', [
49
				urlencode($this->getUser()),
50
				urlencode($this->getPassword()),
51
				$this->getApi(),
52
				urlencode($identifier),
53
				urlencode($message),
54
			]));
55
		} catch (Exception $ex) {
56
			throw new MessageTransmissionException();
57
		}
58
59
		$body = (string)$response->getBody();
60
		if ($response->getStatusCode() !== 200 || substr($body, 0, 4) !== 'ID: ') {
61
			throw new MessageTransmissionException($body);
62
		}
63
	}
64
}
65