Passed
Push — master ( 2479a1...29894a )
by Vitor
02:30 queued 41s
created

ClickatellPortal::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
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 ClickatellPortal implements IProvider {
33
	public const PROVIDER_ID = 'clickatellportal';
34
35
	/** @var IClient */
36
	private $client;
37
38
	/** @var ClickatellPortalConfig */
39
	private $config;
40
41
	public function __construct(IClientService $clientService,
42
								ClickatellPortalConfig $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
			$from = $config->getFromNumber();
57
			$from = !is_null($from) ? sprintf('&from=%s', urlencode($from)) : '';
0 ignored issues
show
introduced by
The condition is_null($from) is always false.
Loading history...
58
			$response = $this->client->get(vsprintf('https://platform.clickatell.com/messages/http/send?apiKey=%s&to=%s&content=%s%s', [
59
				urlencode($config->getApiKey()),
60
				urlencode($identifier),
61
				urlencode($message),
62
				$from,
63
			]));
64
		} catch (Exception $ex) {
65
			throw new SmsTransmissionException();
66
		}
67
68
		if ($response->getStatusCode() !== 202) {
69
			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

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