Passed
Pull Request — master (#640)
by Vitor
03:39
created

Gateway::send()   B

Complexity

Conditions 11
Paths 4

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 37
rs 7.3166
c 0
b 0
f 0
cc 11
nc 4
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\Signal;
11
12
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
13
use OCA\TwoFactorGateway\Provider\Gateway\AGateway;
14
use OCP\Http\Client\IClientService;
15
use OCP\IAppConfig;
16
use OCP\IUser;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\Console\Helper\QuestionHelper;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Question\Question;
22
23
/**
24
 * An integration of https://gitlab.com/morph027/signal-web-gateway
25
 *
26
 * @method string getUrl()
27
 * @method static setUrl(string $url)
28
 */
29
class Gateway extends AGateway {
30
	public const SCHEMA = [
31
		'name' => 'Signal',
32
		'instructions' => 'The gateway can send authentication to your Signal mobile and deskop app.',
33
		'fields' => [
34
			['field' => 'url', 'prompt' => 'Please enter the URL of the Signal gateway (leave blank to use default):', 'default' => 'http://localhost:5000'],
35
		],
36
	];
37
38
	public function __construct(
39
		public IAppConfig $appConfig,
40
		private IClientService $clientService,
41
		private LoggerInterface $logger,
42
	) {
43
		parent::__construct($appConfig);
44
	}
45
46
	#[\Override]
47
	public function send(IUser $user, string $identifier, string $message, array $extra = []): void {
48
		$client = $this->clientService->newClient();
49
		// determine type of gateway
50
		$response = $client->get($this->getUrl() . '/v1/about');
51
		if ($response->getStatusCode() === 200) {
52
			// New style gateway https://gitlab.com/morph027/signal-cli-dbus-rest-api
53
			$response = $client->post(
54
				$this->getUrl() . '/v1/send/' . $identifier,
55
				[
56
					'json' => [ 'message' => $message ],
57
				]
58
			);
59
			$body = (string) $response->getBody();
60
			$json = json_decode($body, true);
61
			if ($response->getStatusCode() !== 201 || is_null($json) || !is_array($json) || !isset($json['timestamp'])) {
62
				$status = $response->getStatusCode();
63
				throw new MessageTransmissionException("error reported by Signal gateway, status=$status, body=$body}");
64
			}
65
		} else {
66
			// Try old deprecated gateway https://gitlab.com/morph027/signal-web-gateway
67
			$response = $client->post(
68
				$this->getUrl() . '/v1/send/' . $identifier,
69
				[
70
					'body' => [
71
						'to' => $identifier,
72
						'message' => $message,
73
					],
74
					'json' => [ 'message' => $message ],
75
				]
76
			);
77
			$body = $response->getBody();
78
			$json = json_decode($body, true);
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type null and 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

78
			$json = json_decode(/** @scrutinizer ignore-type */ $body, true);
Loading history...
79
80
			if ($response->getStatusCode() !== 200 || is_null($json) || !is_array($json) || !isset($json['success']) || $json['success'] !== true) {
81
				$status = $response->getStatusCode();
82
				throw new MessageTransmissionException("error reported by Signal gateway, status=$status, body=$body}");
83
			}
84
		}
85
	}
86
87
	#[\Override]
88
	public function cliConfigure(InputInterface $input, OutputInterface $output): int {
89
		$helper = new QuestionHelper();
90
		$urlQuestion = new Question(self::SCHEMA['fields'][0]['prompt'], self::SCHEMA['fields'][0]['default']);
91
		$url = $helper->ask($input, $output, $urlQuestion);
92
		$output->writeln("Using $url.");
93
94
		$this->setUrl($url);
95
		return 0;
96
	}
97
}
98