Gateway   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 31
c 0
b 0
f 0
dl 0
loc 73
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getConfig() 0 2 1
B send() 0 36 11
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\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 GatewayConfig */
43
	private $config;
44
45
	/** @var ILogger */
46
	private $logger;
47
48
	public function __construct(IClientService $clientService,
49
		GatewayConfig $config,
50
		ILogger $logger) {
51
		$this->clientService = $clientService;
52
		$this->config = $config;
53
		$this->logger = $logger;
54
	}
55
56
	/**
57
	 * @param IUser $user
58
	 * @param string $identifier
59
	 * @param string $message
60
	 *
61
	 * @throws SmsTransmissionException
62
	 */
63
	public function send(IUser $user, string $identifier, string $message) {
64
		$client = $this->clientService->newClient();
65
		// determine type of gateway
66
		$response = $client->get($this->config->getUrl() . '/v1/about');
67
		if ($response->getStatusCode() === 200) {
68
			// New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api
69
			$response = $client->post(
70
				$this->config->getUrl() . '/v1/send/' . $identifier,
71
				[
72
					'json' => [ 'message' => $message ],
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
			if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) {
78
				$status = $response->getStatusCode();
79
				throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}");
80
			}
81
		} else {
82
			// Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway
83
			$response = $client->post(
84
				$this->config->getUrl() . '/v1/send/' . $identifier,
85
				[
86
					'body' => [
87
						'to' => $identifier,
88
						'message' => $message,
89
					],
90
					'json' => [ 'message' => $message ],
91
				]
92
			);
93
			$body = $response->getBody();
94
			$json = json_decode($body, true);
95
96
			if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) {
97
				$status = $response->getStatusCode();
98
				throw new SmsTransmissionException("error reported by Signal gateway, status=$status, body=$body}");
99
			}
100
		}
101
	}
102
103
	/**
104
	 * Get the gateway-specific configuration
105
	 *
106
	 * @return IGatewayConfig
107
	 */
108
	public function getConfig(): IGatewayConfig {
109
		return $this->config;
110
	}
111
}
112