Completed
Push — master ( a04533...2c451c )
by Vitor
19s queued 14s
created

Gateway   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 35
c 1
b 0
f 1
dl 0
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 31 4
A __construct() 0 8 1
A getConfig() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Rainer Dohmen <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway for XMPP
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\XMPP;
25
26
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
27
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
28
use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
29
use OCP\Http\Client\IClient;
30
use OCP\Http\Client\IClientService;
31
use OCP\IConfig;
32
use OCP\ILogger;
33
use OCP\IUser;
34
35
class Gateway implements IGateway {
36
37
	/** @var IClient */
38
	private $client;
39
40
	/** @var GatewayConfig */
41
	private $gatewayConfig;
42
43
	/** @var IConfig */
44
	private $config;
45
46
	/** @var ILogger */
47
	private $logger;
48
49
	public function __construct(IClientService $clientService,
50
								GatewayConfig $gatewayConfig,
51
								IConfig $config,
52
								ILogger $logger) {
53
		$this->client = $clientService->newClient();
54
		$this->gatewayConfig = $gatewayConfig;
55
		$this->config = $config;
56
		$this->logger = $logger;
57
	}
58
59
	/**
60
	 * @param IUser $user
61
	 * @param string $identifier
62
	 * @param string $message
63
	 *
64
	 * @throws SmsTransmissionException
65
	 */
66
	public function send(IUser $user, string $identifier, string $message) {
67
		$this->logger->debug("sending xmpp message to $identifier, message: $message");
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::debug() has been deprecated: 20.0.0 use \Psr\Log\LoggerInterface::debug ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
		/** @scrutinizer ignore-deprecated */ $this->logger->debug("sending xmpp message to $identifier, message: $message");

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
68
69
		$sender = $this->gatewayConfig->getSender();
70
		$password = $this->gatewayConfig->getPassword();
71
		$server = $this->gatewayConfig->getServer();
72
		$method = $this->gatewayConfig->getMethod();
73
		$user = $this->gatewayConfig->getUsername();
74
		$url = $server.$identifier;
75
76
		if ($method === "1") {
77
			$from = $user;
78
		}
79
		if ($method === "2") {
80
			$from = $sender;
81
		}
82
		$this->logger->debug("URL: $url, sender: $sender, method: $method");
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::debug() has been deprecated: 20.0.0 use \Psr\Log\LoggerInterface::debug ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
		/** @scrutinizer ignore-deprecated */ $this->logger->debug("URL: $url, sender: $sender, method: $method");

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
83
84
		try {
85
			$ch = curl_init();
86
			curl_setopt($ch, CURLOPT_URL, $url);
87
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
88
			curl_setopt($ch, CURLOPT_POST, 1);
89
			curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
90
			curl_setopt($ch, CURLOPT_USERPWD, $from.":".$password);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $from does not seem to be defined for all execution paths leading up to this point.
Loading history...
91
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
92
			$result = curl_exec($ch);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
93
			curl_close($ch);
94
			$this->logger->debug("XMPP message to $identifier sent");
0 ignored issues
show
Deprecated Code introduced by
The function OCP\ILogger::debug() has been deprecated: 20.0.0 use \Psr\Log\LoggerInterface::debug ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

94
			/** @scrutinizer ignore-deprecated */ $this->logger->debug("XMPP message to $identifier sent");

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
95
		} catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Ser...\Gateway\XMPP\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
96
			throw new SmsTransmissionException();
97
		}
98
	}
99
100
	/**
101
	 * Get the gateway-specific configuration
102
	 *
103
	 * @return IGatewayConfig
104
	 */
105
	public function getConfig(): IGatewayConfig {
106
		return $this->gatewayConfig;
107
	}
108
}
109