Completed
Push — master ( 261493...1ee18f )
by Vitor
19s queued 13s
created

SipGate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
c 1
b 0
f 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfig() 0 2 1
A send() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Claus-Justus Heine <[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
31
class SipGate implements IProvider {
32
	public const PROVIDER_ID = 'sipgate';
33
34
	/** @var IClient */
35
	private $client;
36
37
	/** @var WebSmsConfig */
38
	private $config;
39
40
	public function __construct(IClientService $clientService,
41
								SipGateConfig $config) {
42
		$this->client = $clientService->newClient();
43
		$this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type OCA\TwoFactorGateway\Ser...\Provider\SipGateConfig is incompatible with the declared type OCA\TwoFactorGateway\Ser...S\Provider\WebSmsConfig of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
	}
45
46
	/**
47
	 * @param string $identifier
48
	 * @param string $message
49
	 *
50
	 * @throws SmsTransmissionException
51
	 */
52
	public function send(string $identifier, string $message) {
53
		$config = $this->getConfig();
54
		$tokenId = $config->getTokenId();
0 ignored issues
show
Bug introduced by
The method getTokenId() does not exist on OCA\TwoFactorGateway\Ser...S\Provider\WebSmsConfig. ( Ignorable by Annotation )

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

54
		/** @scrutinizer ignore-call */ 
55
  $tokenId = $config->getTokenId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
		$accessToken = $config->getAccessToken();
0 ignored issues
show
Bug introduced by
The method getAccessToken() does not exist on OCA\TwoFactorGateway\Ser...S\Provider\WebSmsConfig. ( Ignorable by Annotation )

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

55
		/** @scrutinizer ignore-call */ 
56
  $accessToken = $config->getAccessToken();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
		$webSmsExtension = $config->getWebSmsExtension();
0 ignored issues
show
Bug introduced by
The method getWebSmsExtension() does not exist on OCA\TwoFactorGateway\Ser...S\Provider\WebSmsConfig. ( Ignorable by Annotation )

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

56
		/** @scrutinizer ignore-call */ 
57
  $webSmsExtension = $config->getWebSmsExtension();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
		try {
59
			$this->client->post('https://api.sipgate.com/v2/sessions/sms', [
60
				'headers' => [
61
					'Authorization' => 'Basic ' . base64_encode("$tokenId:$accessToken"),
62
					'Content-Type' => 'application/json',
63
					'Accept' => 'application/json',
64
				],
65
				'json' => [
66
				  "smsId" => $webSmsExtension,
67
				  "message" => $message,
68
				  "recipient" => $identifier,
69
				  "sendAt" => null,
70
				],
71
			]);
72
		} catch (Exception $ex) {
73
			throw new SmsTransmissionException('SipGate Send Failed', $ex->getCode(), $ex);
74
		}
75
	}
76
77
	/**
78
	 * @return SipGateConfig
79
	 */
80
	public function getConfig(): IProviderConfig {
81
		return $this->config;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->config returns the type OCA\TwoFactorGateway\Ser...S\Provider\WebSmsConfig which is incompatible with the documented return type OCA\TwoFactorGateway\Ser...\Provider\SipGateConfig.
Loading history...
82
	}
83
84
}
85