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

ClickSend::createSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
ccs 12
cts 12
cp 1
rs 9.9332
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 Henning Bopp <[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 getUser()
22
 * @method static setUser(string $user)
23
 *
24
 * @method string getApikey()
25
 * @method static setApikey(string $apikey)
26
 */
27
class ClickSend 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: 'clicksend',
39 1
			name: 'ClickSend',
40 1
			fields: [
41 1
				new FieldDefinition(
42 1
					field: 'user',
43 1
					prompt: 'Please enter your clicksend.com username:',
44 1
				),
45 1
				new FieldDefinition(
46 1
					field: 'apikey',
47 1
					prompt: 'Please enter your clicksend.com api key (or subuser password):',
48 1
				),
49 1
			]
50 1
		);
51
	}
52
53
	#[\Override]
54
	public function send(string $identifier, string $message) {
55
		$apiKey = $this->getApiKey();
56
		$username = $this->getUser();
57
		try {
58
			$this->client->get('https://api-mapper.clicksend.com/http/v2/send.php', [
59
				'query' => [
60
					'method' => 'http',
61
					'username' => $username,
62
					'key' => $apiKey,
63
					'to' => $identifier,
64
					'message' => $message,
65
					'senderid' => 'nextcloud'
66
				],
67
			]);
68
		} catch (Exception $ex) {
69
			throw new MessageTransmissionException();
70
		}
71
	}
72
}
73