ClickatellCentral   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 2
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfig() 0 2 1
A send() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christian Schrötter <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 *
9
 * Nextcloud - Two-factor Gateway
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider;
26
27
use Exception;
28
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
29
use OCP\Http\Client\IClient;
30
use OCP\Http\Client\IClientService;
31
32
class ClickatellCentral implements IProvider {
33
	public const PROVIDER_ID = 'clickatellcentral';
34
35
	/** @var IClient */
36
	private $client;
37
38
	/** @var ClickatellCentralConfig */
39
	private $config;
40
41
	public function __construct(IClientService $clientService,
42
		ClickatellCentralConfig $config) {
43
		$this->client = $clientService->newClient();
44
		$this->config = $config;
45
	}
46
47
	/**
48
	 * @param string $identifier
49
	 * @param string $message
50
	 *
51
	 * @throws SmsTransmissionException
52
	 */
53
	public function send(string $identifier, string $message) {
54
		$config = $this->getConfig();
55
		try {
56
			$response = $this->client->get(vsprintf('https://api.clickatell.com/http/sendmsg?user=%s&password=%s&api_id=%u&to=%s&text=%s', [
57
				urlencode($config->getUser()),
58
				urlencode($config->getPassword()),
59
				$config->getApi(),
60
				urlencode($identifier),
61
				urlencode($message),
62
			]));
63
		} catch (Exception $ex) {
64
			throw new SmsTransmissionException();
65
		}
66
67
		if ($response->getStatusCode() !== 200 || substr($response->getBody(), 0, 4) !== 'ID: ') {
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type resource; however, parameter $string of substr() 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

67
		if ($response->getStatusCode() !== 200 || substr(/** @scrutinizer ignore-type */ $response->getBody(), 0, 4) !== 'ID: ') {
Loading history...
68
			throw new SmsTransmissionException($response->getBody());
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type resource; however, parameter $message of OCA\TwoFactorGateway\Exc...xception::__construct() 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

68
			throw new SmsTransmissionException(/** @scrutinizer ignore-type */ $response->getBody());
Loading history...
69
		}
70
	}
71
72
	/**
73
	 * @return ClickatellCentralConfig
74
	 */
75
	public function getConfig(): IProviderConfig {
76
		return $this->config;
77
	}
78
}
79