Completed
Pull Request — master (#92)
by Christoph
03:15
created

Gateway::getConfig()   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
 * @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 OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
30
use OCP\Http\Client\IClientService;
31
use OCP\IL10N;
32
use OCP\ILogger;
33
use OCP\IUser;
34
35
/**
36
 * An integration of https://gitlab.com/morph027/signal-web-gateway
37
 */
38
class Gateway implements IGateway {
39
40
	/** @var IClientService */
41
	private $clientService;
42
43
	/** @var GatewayConfig */
44
	private $config;
45
46
	/** @var ILogger */
47
	private $logger;
48
49
	public function __construct(IClientService $clientService,
50
								GatewayConfig $config,
51
								ILogger $logger) {
52
		$this->clientService = $clientService;
53
		$this->config = $config;
54
		$this->logger = $logger;
55
	}
56
57
	/**
58
	 * @param IUser $user
59
	 * @param string $identifier
60
	 * @param string $message
61
	 *
62
	 * @throws SmsTransmissionException
63
	 */
64
	public function send(IUser $user, string $identifier, string $message) {
65
		$client = $this->clientService->newClient();
66
		$response = $client->post(
67
			$this->config->getUrl(),
68
			[
69
				'body' => [
70
					'to' => $identifier,
71
					'message' => $message,
72
				],
73
			]
74
		);
75
		$body = $response->getBody();
76
		$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

76
		$json = json_decode(/** @scrutinizer ignore-type */ $body, true);
Loading history...
77
78
		if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) {
79
			$status = $response->getStatusCode();
80
			throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}");
81
		}
82
	}
83
84
	/**
85
	 * Get the gateway-specific configuration
86
	 *
87
	 * @return IGatewayConfig
88
	 */
89
	public function getConfig(): IGatewayConfig {
90
		return $this->config;
91
	}
92
93
}
94