Completed
Push — master ( e69c2b...0fe61e )
by Christoph
15s queued 12s
created

SpryngSMS::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Ruben de Wit <[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\Provider;
25
26
use Exception;
27
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
28
use OCP\Http\Client\IClient;
29
use OCP\Http\Client\IClientService;
30
use OCP\IConfig;
31
32
class SpryngSMS implements IProvider {
33
34
	const PROVIDER_ID = 'spryng';
35
36
	/** @var IClient */
37
	private $client;
38
39
	/** @var SpryngSMSConfig */
40
	private $config;
41
42
	public function __construct(IClientService $clientService,
43
								SpryngSMSConfig $config) {
44
		$this->client = $clientService->newClient();
45
		$this->config = $config;
46
	}
47
48
	/**
49
	 * @param string $identifier
50
	 * @param string $message
51
	 *
52
	 * @throws SmsTransmissionException
53
	 */
54
	public function send(string $identifier, string $message) {
55
		$config = $this->getConfig();
56
		/** @var SpryngSMSConfig $providerConfig */
57
		try {
58
			$response = $this->client->post(
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
59
				'https://rest.spryngsms.com/v1/messages?with%5B%5D=recipients',
60
				[
61
					'headers' => [
62
						'Accept' => 'application/json',
63
						'Authorization' => 'Bearer ' . $config->getApiToken(),
64
						'Content-Type' => 'application/json',
65
					],
66
					'json' => [
67
						'body' => $message,
68
						'encoding' => 'plain',
69
						'originator' => 'Nextcloud',
70
						'recipients' => [$identifier],
71
						'route' => '1',
72
					],
73
				]
74
			);
75
		} catch (Exception $ex) {
76
			throw new SmsTransmissionException();
77
		}
78
	}
79
80
	/**
81
	 * @return SpryngSMSConfig
82
	 */
83
	public function getConfig(): IProviderConfig {
84
		return $this->config;
85
	}
86
87
}
88