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

ClickSend   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 22
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 17 2
A __construct() 0 4 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 OCP\Http\Client\IClient;
16
use OCP\Http\Client\IClientService;
17
18
/**
19
 * @method string getUser()
20
 * @method static setUser(string $user)
21
 *
22
 * @method string getApikey()
23
 * @method static setApikey(string $apikey)
24
 */
25
class ClickSend extends AProvider {
26
	public const SCHEMA = [
27
		'name' => 'ClickSend',
28
		'fields' => [
29
			['field' => 'user', 'prompot' => 'Please enter your clicksend.com username:'],
30
			['field' => 'apikey', 'prompot' => 'Please enter your clicksend.com api key (or subuser password):'],
31
		],
32
	];
33
	private IClient $client;
34
35
	public function __construct(
36
		IClientService $clientService,
37
	) {
38
		$this->client = $clientService->newClient();
39
	}
40
41
	#[\Override]
42
	public function send(string $identifier, string $message) {
43
		$apiKey = $this->getApiKey();
44
		$username = $this->getUser();
45
		try {
46
			$this->client->get('https://api-mapper.clicksend.com/http/v2/send.php', [
47
				'query' => [
48
					'method' => 'http',
49
					'username' => $username,
50
					'key' => $apiKey,
51
					'to' => $identifier,
52
					'message' => $message,
53
					'senderid' => 'nextcloud'
54
				],
55
			]);
56
		} catch (Exception $ex) {
57
			throw new MessageTransmissionException();
58
		}
59
	}
60
}
61