Completed
Push — master ( 636672...b19edb )
by Christoph
12s
created

WebSmsGateway::getShortName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Service\Gateway\SMS;
25
26
use Exception;
27
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
28
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
29
use OCP\Http\Client\IClient;
30
use OCP\Http\Client\IClientService;
31
use OCP\IConfig;
32
use OCP\IL10N;
33
use OCP\IUser;
34
35
class WebSmsGateway implements IGateway {
36
37
	/** @var IClient */
38
	private $client;
39
40
	/** @var IConfig */
41
	private $config;
42
43
	/** @var IL10N */
44
	private $l10n;
45
46
	public function __construct(IClientService $clientService,
47
								IConfig $config,
48
								IL10N $l10n) {
49
		$this->client = $clientService->newClient();
50
		$this->config = $config;
51
		$this->l10n = $l10n;
52
	}
53
54
	/**
55
	 * @param IUser $user
56
	 * @param string $idenfier
57
	 * @param string $message
58
	 *
59
	 * @throws SmsTransmissionException
60
	 */
61
	public function send(IUser $user, string $idenfier, string $message) {
62
		$user = $this->config->getAppValue('twofactor_gateway', 'websms_de_user');
63
		$password = $this->config->getAppValue('twofactor_gateway', 'websms_de_password');
64
		try {
65
			$this->client->post('https://api.websms.com/rest/smsmessaging/text', [
66
				'headers' => [
67
					'Authorization' => 'Basic ' . base64_encode("$user:$password"),
68
					'Content-Type' => 'application/json',
69
				],
70
				'json' => [
71
					'messageContent' => $message,
72
					'test' => false,
73
					'recipientAddressList' => [$idenfier],
74
				],
75
			]);
76
		} catch (Exception $ex) {
77
			throw new SmsTransmissionException();
78
		}
79
	}
80
81
	/**
82
	 * Get a short description of this gateway's name so that users know how
83
	 * their messages are delivered, e.g. "Telegram"
84
	 *
85
	 * @return string
86
	 */
87
	public function getShortName(): string {
88
		return 'SMS';
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94
	public function getProviderDescription(): string {
95
		return $this->l10n->t('Authenticate via SMS');
96
	}
97
}
98