WebSms::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 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 getUser()
22
 * @method static setUser(string $user)
23
 * @method string getPassword()
24
 * @method static setPassword(string $password)
25
 */
26
class WebSms extends AProvider {
27
	private IClient $client;
28
29 1
	public function __construct(
30
		IClientService $clientService,
31
	) {
32 1
		$this->client = $clientService->newClient();
33
	}
34
35 1
	public function createSettings(): Settings {
36 1
		return new Settings(
37 1
			id: 'websms_de',
38 1
			name: 'WebSMS.de',
39 1
			fields: [
40 1
				new FieldDefinition(
41 1
					field: 'user',
42 1
					prompt: 'Please enter your websms.de username:',
43 1
				),
44 1
				new FieldDefinition(
45 1
					field: 'password',
46 1
					prompt: 'Please enter your websms.de password:',
47 1
				),
48 1
			]
49 1
		);
50
	}
51
52
	#[\Override]
53
	public function send(string $identifier, string $message) {
54
		$user = $this->getUser();
55
		$password = $this->getPassword();
56
		try {
57
			$this->client->post('https://api.websms.com/rest/smsmessaging/text', [
58
				'headers' => [
59
					'Authorization' => 'Basic ' . base64_encode("$user:$password"),
60
					'Content-Type' => 'application/json',
61
				],
62
				'json' => [
63
					'messageContent' => $message,
64
					'test' => false,
65
					'recipientAddressList' => [$identifier],
66
				],
67
			]);
68
		} catch (Exception $ex) {
69
			throw new MessageTransmissionException();
70
		}
71
	}
72
}
73