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

Gateway   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 20
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProviderDescription() 0 2 1
A __construct() 0 6 1
A send() 0 17 6
A getShortName() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\TwoFactorGateway\Service\Gateway\Signal;
26
27
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
28
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
29
use OCP\Http\Client\IClientService;
30
use OCP\IL10N;
31
use OCP\ILogger;
32
use OCP\IUser;
33
34
/**
35
 * An integration of https://gitlab.com/morph027/signal-web-gateway
36
 */
37
class Gateway implements IGateway {
38
39
	/** @var IClientService */
40
	private $clientService;
41
42
	/** @var ILogger */
43
	private $logger;
44
45
	/** @var IL10N */
46
	private $l10n;
47
48
	public function __construct(IClientService $clientService,
49
								ILogger $logger,
50
								IL10N $l10n) {
51
		$this->clientService = $clientService;
52
		$this->logger = $logger;
53
		$this->l10n = $l10n;
54
	}
55
56
	/**
57
	 * @param IUser $user
58
	 * @param string $idenfier
59
	 * @param string $message
60
	 *
61
	 * @throws SmsTransmissionException
62
	 */
63
	public function send(IUser $user, string $idenfier, string $message) {
64
		// TODO: make configurable
65
		$endpoint = 'http://localhost:5000';
66
67
		$client = $this->clientService->newClient();
68
		$response = $client->post($endpoint, [
69
			'body' => [
70
				'to' => $idenfier,
71
				'message' => $message,
72
			],
73
		]);
74
		$body = $response->getBody();
75
		$json = json_decode($body, true);
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type resource; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
		$json = json_decode(/** @scrutinizer ignore-type */ $body, true);
Loading history...
76
77
		if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) {
78
			$status = $response->getStatusCode();
79
			throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}");
80
		}
81
	}
82
83
	/**
84
	 * Get a short description of this gateway's name so that users know how
85
	 * their messages are delivered, e.g. "Telegram"
86
	 *
87
	 * @return string
88
	 */
89
	public function getShortName(): string {
90
		return 'Signal';
91
	}
92
93
	/**
94
	 * @return string
95
	 */
96
	public function getProviderDescription(): string {
97
		return $this->l10n->t('Authenticate via Signal');
98
	}
99
}
100