Passed
Pull Request — master (#640)
by Vitor
03:39
created

ClickatellPortal   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 23
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 18 4
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 getApikey()
20
 * @method static setApikey(string $apikey)
21
 *
22
 * @method string getFrom()
23
 * @method static setFrom(string $from)
24
 */
25
class ClickatellPortal extends AProvider {
26
	public const SCHEMA = [
27
		'id' => 'clickatell_portal',
28
		'name' => 'Clickatell Portal',
29
		'fields' => [
30
			['field' => 'apikey', 'prompt' => 'Please enter your portal.clickatell.com API-Key:'],
31
			['field' => 'from',   'prompt' => 'Please enter your sender number for two-way messaging (empty = one-way): ', 'optional' => true],
32
		],
33
	];
34
	private IClient $client;
35
36
	public function __construct(
37
		IClientService $clientService,
38
	) {
39
		$this->client = $clientService->newClient();
40
	}
41
42
	#[\Override]
43
	public function send(string $identifier, string $message) {
44
		try {
45
			$from = $this->getFrom();
46
			$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...
47
			$response = $this->client->get(vsprintf('https://platform.clickatell.com/messages/http/send?apiKey=%s&to=%s&content=%s%s', [
48
				urlencode($this->getApikey()),
49
				urlencode($identifier),
50
				urlencode($message),
51
				$from,
52
			]));
53
		} catch (Exception $ex) {
54
			throw new MessageTransmissionException();
55
		}
56
57
		$body = (string) $response->getBody();
58
		if ($response->getStatusCode() !== 202) {
59
			throw new MessageTransmissionException($body);
60
		}
61
	}
62
}
63