Passed
Pull Request — master (#221)
by
unknown
03:29
created

HuaweiE3531   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 24
dl 0
loc 54
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 24 2
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
			$session_token_response = $this->client->get("{$url}/webserver/SesTokInfo");
60
			$session_token_xml = simplexml_load_string($session_token_response->getBody());
0 ignored issues
show
Bug introduced by
It seems like $session_token_response->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
			$session_token_xml = simplexml_load_string(/** @scrutinizer ignore-type */ $session_token_response->getBody());
Loading history...
61
62
			$date = date('Y-m-d H:i:s');
63
			$message_escaped = htmlspecialchars($message, ENT_XML1);
64
65
			$send_response = $this->client->post("{$url}/sms/send-sms", [
66
				'body' => "<request><Index>-1</Index><Phones><Phone>$identifier</Phone></Phones><Sca/><Content>$message_escaped</Content><Length>-1</Length><Reserved>1</Reserved><Date>$date</Date></request>",
67
				'headers' => [
68
					'Cookie' => $session_token_xml->SesInfo,
69
					'X-Requested-With' => 'XMLHttpRequest',
70
					'__RequestVerificationToken' => $session_token_xml->TokInfo,
71
					'Content-Type' => 'text/xml',
72
				],
73
			]);
74
			$send_xml = simplexml_load_string($send_response->getBody());
0 ignored issues
show
Unused Code introduced by
The assignment to $send_xml is dead and can be removed.
Loading history...
75
			// TODO: check that $send_xml content is "OK"
76
		} catch (Exception $ex) {
77
			throw new SmsTransmissionException();
78
		}
79
	}
80
81
	/**
82
	 * @return HuaweiE3531Config
83
	 */
84
	public function getConfig(): IProviderConfig {
85
		return $this->config;
86
	}
87
88
}
89