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

SipGate::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 22
ccs 0
cts 19
cp 0
rs 9.7
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 Claus-Justus Heine <[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 getTokenId()
22
 * @method static setTokenId(string $tokenId)
23
 * @method string getAccessToken()
24
 * @method static setAccessToken(string $accessToken)
25
 * @method string getWebSmsExtension()
26
 * @method static setWebSmsExtension(string $webSmsExtension)
27
 */
28
class SipGate extends AProvider {
29
	private IClient $client;
30
31 1
	public function __construct(
32
		IClientService $clientService,
33
	) {
34 1
		$this->client = $clientService->newClient();
35
	}
36
37 1
	public function createSettings(): Settings {
38 1
		return new Settings(
39 1
			id: 'sipgate',
40 1
			name: 'SipGate',
41 1
			fields: [
42 1
				new FieldDefinition(
43 1
					field: 'token_id',
44 1
					prompt: 'Please enter your sipgate token-id:',
45 1
				),
46 1
				new FieldDefinition(
47 1
					field: 'access_token',
48 1
					prompt: 'Please enter your sipgate access token:',
49 1
				),
50 1
				new FieldDefinition(
51 1
					field: 'web_sms_extension',
52 1
					prompt: 'Please enter your sipgate web-sms extension:',
53 1
				),
54 1
			],
55 1
		);
56
	}
57
58
	#[\Override]
59
	public function send(string $identifier, string $message) {
60
		$tokenId = $this->getTokenId();
61
		$accessToken = $this->getAccessToken();
62
		$webSmsExtension = $this->getWebSmsExtension();
63
64
		try {
65
			$this->client->post('https://api.sipgate.com/v2/sessions/sms', [
66
				'headers' => [
67
					'Authorization' => 'Basic ' . base64_encode("$tokenId:$accessToken"),
68
					'Content-Type' => 'application/json',
69
					'Accept' => 'application/json',
70
				],
71
				'json' => [
72
					'smsId' => $webSmsExtension,
73
					'message' => $message,
74
					'recipient' => $identifier,
75
					'sendAt' => null,
76
				],
77
			]);
78
		} catch (Exception $ex) {
79
			throw new MessageTransmissionException('SipGate Send Failed', $ex->getCode(), $ex);
80
		}
81
	}
82
}
83