Sms77Io::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 André Matthies <[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 getApiKey()
22
 * @method static setApiKey(string $apiKey)
23
 */
24
class Sms77Io extends AProvider {
25
	private IClient $client;
26
27 1
	public function __construct(
28
		IClientService $clientService,
29
	) {
30 1
		$this->client = $clientService->newClient();
31
	}
32
33 1
	public function createSettings(): Settings {
34 1
		return new Settings(
35 1
			id: 'sms77io',
36 1
			name: 'sms77.io',
37 1
			fields: [
38 1
				new FieldDefinition(
39 1
					field: 'api_key',
40 1
					prompt: 'Please enter your sms77.io API key:',
41 1
				),
42 1
			]
43 1
		);
44
	}
45
46
	#[\Override]
47
	public function send(string $identifier, string $message) {
48
		$apiKey = $this->getApiKey();
49
		try {
50
			$this->client->get('https://gateway.sms77.io/api/sms', [
51
				'query' => [
52
					'p' => $apiKey,
53
					'to' => $identifier,
54
					'text' => $message,
55
					'sendWith' => 'nextcloud'
56
				],
57
			]);
58
		} catch (Exception $ex) {
59
			throw new MessageTransmissionException();
60
		}
61
	}
62
}
63