Completed
Push — master ( 5ace7d...b2d76c )
by Christoph
03:55 queued 11s
created

HuaweiE3531   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 26
dl 0
loc 57
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 27 3
A getConfig() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Martin Keßler <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider;
25
26
use Exception;
27
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
28
use OCP\Http\Client\IClient;
29
use OCP\Http\Client\IClientService;
30
use OCP\IConfig;
31
32
class HuaweiE3531 implements IProvider {
33
34
	const PROVIDER_ID = 'huawei_e3531';
35
36
	/** @var IClient */
37
	private $client;
38
39
	/** @var HuaweiE3531Config */
40
	private $config;
41
42
	public function __construct(IClientService $clientService,
43
								HuaweiE3531Config $config) {
44
		$this->client = $clientService->newClient();
45
		$this->config = $config;
46
	}
47
48
	/**
49
	 * @param string $identifier
50
	 * @param string $message
51
	 *
52
	 * @throws SmsTransmissionException
53
	 */
54
	public function send(string $identifier, string $message) {
55
		$config = $this->getConfig();
56
		$url = $config->getUrl();
57
58
		try {
59
			$sessionTokenResponse = $this->client->get("$url/webserver/SesTokInfo");
60
			$sessionTokenXml = simplexml_load_string($sessionTokenResponse->getBody());
0 ignored issues
show
Bug introduced by
It seems like $sessionTokenResponse->getBody() can also be of type resource; however, parameter $data of simplexml_load_string() 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

60
			$sessionTokenXml = simplexml_load_string(/** @scrutinizer ignore-type */ $sessionTokenResponse->getBody());
Loading history...
61
62
			$date = date('Y-m-d H:i:s');
63
			$messageEscaped = htmlspecialchars($message, ENT_XML1);
64
65
			$sendResponse = $this->client->post("$url/sms/send-sms", [
66
				'body' => "<request><Index>-1</Index><Phones><Phone>$identifier</Phone></Phones><Sca/><Content>$messageEscaped</Content><Length>-1</Length><Reserved>1</Reserved><Date>$date</Date></request>",
67
				'headers' => [
68
					'Cookie' => $sessionTokenXml->SesInfo,
69
					'X-Requested-With' => 'XMLHttpRequest',
70
					'__RequestVerificationToken' => $sessionTokenXml->TokInfo,
71
					'Content-Type' => 'text/xml',
72
				],
73
			]);
74
			$sendXml = simplexml_load_string($sendResponse->getBody());
75
		} catch (Exception $ex) {
76
			throw new SmsTransmissionException();
77
		}
78
79
		if ((string) $sendXml !== "OK") {
80
			throw new SmsTransmissionException();
81
		}
82
	}
83
84
	/**
85
	 * @return HuaweiE3531Config
86
	 */
87
	public function getConfig(): IProviderConfig {
88
		return $this->config;
89
	}
90
91
}
92