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

WebSms   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 23
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 18 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christoph Wurst <[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 getUser()
20
 * @method static setUser(string $user)
21
 * @method string getPassword()
22
 * @method static setPassword(string $password)
23
 */
24
class WebSms extends AProvider {
25
	public const SCHEMA = [
26
		'id' => 'websms_de',
27
		'name' => 'WebSMS.de',
28
		'fields' => [
29
			['field' => 'user',     'prompt' => 'Please enter your websms.de username:'],
30
			['field' => 'password', 'prompt' => 'Please enter your websms.de password:'],
31
		],
32
	];
33
	private IClient $client;
34
35
	public function __construct(
36
		IClientService $clientService,
37
	) {
38
		$this->client = $clientService->newClient();
39
	}
40
41
	#[\Override]
42
	public function send(string $identifier, string $message) {
43
		$user = $this->getUser();
44
		$password = $this->getPassword();
45
		try {
46
			$this->client->post('https://api.websms.com/rest/smsmessaging/text', [
47
				'headers' => [
48
					'Authorization' => 'Basic ' . base64_encode("$user:$password"),
49
					'Content-Type' => 'application/json',
50
				],
51
				'json' => [
52
					'messageContent' => $message,
53
					'test' => false,
54
					'recipientAddressList' => [$identifier],
55
				],
56
			]);
57
		} catch (Exception $ex) {
58
			throw new MessageTransmissionException();
59
		}
60
	}
61
}
62