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

SipGate::__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 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 OCP\Http\Client\IClient;
16
use OCP\Http\Client\IClientService;
17
18
/**
19
 * @method string getTokenId()
20
 * @method static setTokenId(string $tokenId)
21
 * @method string getAccessToken()
22
 * @method static setAccessToken(string $accessToken)
23
 * @method string getWebSmsExtension()
24
 * @method static setWebSmsExtension(string $webSmsExtension)
25
 */
26
class SipGate extends AProvider {
27
	public const SCHEMA = [
28
		'name' => 'SipGate',
29
		'fields' => [
30
			['field' => 'token_id',        'prompt' => 'Please enter your sipgate token-id:'],
31
			['field' => 'access_token',    'prompt' => 'Please enter your sipgate access token:'],
32
			['field' => 'web_sms_extension','prompt' => 'Please enter your sipgate web-sms extension:'],
33
		],
34
	];
35
	private IClient $client;
36
37
	public function __construct(
38
		IClientService $clientService,
39
	) {
40
		$this->client = $clientService->newClient();
41
	}
42
43
	#[\Override]
44
	public function send(string $identifier, string $message) {
45
		$tokenId = $this->getTokenId();
46
		$accessToken = $this->getAccessToken();
47
		$webSmsExtension = $this->getWebSmsExtension();
48
49
		try {
50
			$this->client->post('https://api.sipgate.com/v2/sessions/sms', [
51
				'headers' => [
52
					'Authorization' => 'Basic ' . base64_encode("$tokenId:$accessToken"),
53
					'Content-Type' => 'application/json',
54
					'Accept' => 'application/json',
55
				],
56
				'json' => [
57
					'smsId' => $webSmsExtension,
58
					'message' => $message,
59
					'recipient' => $identifier,
60
					'sendAt' => null,
61
				],
62
			]);
63
		} catch (Exception $ex) {
64
			throw new MessageTransmissionException('SipGate Send Failed', $ex->getCode(), $ex);
65
		}
66
	}
67
}
68