SpryngSMS   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 39.38%

Importance

Changes 0
Metric Value
wmc 4
eloc 26
dl 0
loc 44
ccs 13
cts 33
cp 0.3938
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 22 2
A createSettings() 0 8 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Ruben de Wit <[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 getApitoken()
22
 * @method static setApitoken(string $apitoken)
23
 */
24
class SpryngSMS 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: 'spryngsms',
36 1
			name: 'Spryng',
37 1
			fields: [
38 1
				new FieldDefinition(
39 1
					field: 'apitoken',
40 1
					prompt: 'Please enter your Spryng api token:',
41 1
				),
42 1
			],
43 1
		);
44
	}
45
46
	#[\Override]
47
	public function send(string $identifier, string $message) {
48
		try {
49
			$this->client->post(
50
				'https://rest.spryngsms.com/v1/messages?with%5B%5D=recipients',
51
				[
52
					'headers' => [
53
						'Accept' => 'application/json',
54
						'Authorization' => 'Bearer ' . $this->getApitoken(),
55
						'Content-Type' => 'application/json',
56
					],
57
					'json' => [
58
						'body' => $message,
59
						'encoding' => 'plain',
60
						'originator' => 'Nextcloud',
61
						'recipients' => [$identifier],
62
						'route' => '1',
63
					],
64
				]
65
			);
66
		} catch (Exception $ex) {
67
			throw new MessageTransmissionException();
68
		}
69
	}
70
}
71