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

SpryngSMS::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 OCP\Http\Client\IClient;
16
use OCP\Http\Client\IClientService;
17
18
/**
19
 * @method string getApitoken()
20
 * @method static setApitoken(string $apitoken)
21
 */
22
class SpryngSMS extends AProvider {
23
	public const SCHEMA = [
24
		'name' => 'Spryng',
25
		'fields' => [
26
			['field' => 'apitoken', 'prompt' => 'Please enter your Spryng api token:'],
27
		],
28
	];
29
	private IClient $client;
30
31
	public function __construct(
32
		IClientService $clientService,
33
	) {
34
		$this->client = $clientService->newClient();
35
	}
36
37
	#[\Override]
38
	public function send(string $identifier, string $message) {
39
		try {
40
			$this->client->post(
41
				'https://rest.spryngsms.com/v1/messages?with%5B%5D=recipients',
42
				[
43
					'headers' => [
44
						'Accept' => 'application/json',
45
						'Authorization' => 'Bearer ' . $this->getApitoken(),
46
						'Content-Type' => 'application/json',
47
					],
48
					'json' => [
49
						'body' => $message,
50
						'encoding' => 'plain',
51
						'originator' => 'Nextcloud',
52
						'recipients' => [$identifier],
53
						'route' => '1',
54
					],
55
				]
56
			);
57
		} catch (Exception $ex) {
58
			throw new MessageTransmissionException();
59
		}
60
	}
61
}
62