ClickatellPortal::send()   A
last analyzed

Complexity

Conditions 4
Paths 7

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.7998
c 0
b 0
f 0
cc 4
nc 7
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 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 getApikey()
22
 * @method static setApikey(string $apikey)
23
 *
24
 * @method string getFrom()
25
 * @method static setFrom(string $from)
26
 */
27
class ClickatellPortal extends AProvider {
28
	private IClient $client;
29
30 1
	public function __construct(
31
		IClientService $clientService,
32
	) {
33 1
		$this->client = $clientService->newClient();
34
	}
35
36 1
	public function createSettings(): Settings {
37 1
		return new Settings(
38 1
			id: 'clickatell_portal',
39 1
			name: 'Clickatell Portal',
40 1
			fields: [
41 1
				new FieldDefinition(
42 1
					field: 'apikey',
43 1
					prompt: 'Please enter your portal.clickatell.com API-Key:',
44 1
				),
45 1
				new FieldDefinition(
46 1
					field: 'from',
47 1
					prompt: 'Please enter your sender number for two-way messaging (empty = one-way): ',
48 1
					optional: true,
49 1
				),
50 1
			],
51 1
		);
52
	}
53
54
	#[\Override]
55
	public function send(string $identifier, string $message) {
56
		try {
57
			$from = $this->getFrom();
58
			$from = !is_null($from) ? sprintf('&from=%s', urlencode($from)) : '';
0 ignored issues
show
introduced by
The condition is_null($from) is always false.
Loading history...
59
			$response = $this->client->get(vsprintf('https://platform.clickatell.com/messages/http/send?apiKey=%s&to=%s&content=%s%s', [
60
				urlencode($this->getApikey()),
61
				urlencode($identifier),
62
				urlencode($message),
63
				$from,
64
			]));
65
		} catch (Exception $ex) {
66
			throw new MessageTransmissionException();
67
		}
68
69
		$body = (string)$response->getBody();
70
		if ($response->getStatusCode() !== 202) {
71
			throw new MessageTransmissionException($body);
72
		}
73
	}
74
}
75