Completed
Push — master ( 8024bc...f8f0d2 )
by Vitor
14s
created

ClickatellCentral::createSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

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