Passed
Pull Request — master (#640)
by Vitor
14:24
created

ClickatellCentral   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 22
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 16 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 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
		if ($response->getStatusCode() !== 200 || substr($response->getBody(), 0, 4) !== 'ID: ') {
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type null and resource; however, parameter $string of substr() 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

59
		if ($response->getStatusCode() !== 200 || substr(/** @scrutinizer ignore-type */ $response->getBody(), 0, 4) !== 'ID: ') {
Loading history...
60
			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

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