Passed
Pull Request — master (#640)
by Vitor
13:21 queued 09:43
created

ClickatellPortal   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 22
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 17 4
A __construct() 0 4 1
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
		if ($response->getStatusCode() !== 202) {
58
			throw new MessageTransmissionException($response->getBody());
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type resource; however, parameter $message of OCA\TwoFactorGateway\Exc...xception::__construct() 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

58
			throw new MessageTransmissionException(/** @scrutinizer ignore-type */ $response->getBody());
Loading history...
59
		}
60
	}
61
}
62