Failed Conditions
Push — develop ( 9705a9...c29d28 )
by Reüel
03:41
created

Client   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 76.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 50
c 1
b 0
f 0
dl 0
loc 150
ccs 42
cts 55
cp 0.7635
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A parse_xml() 0 19 6
A get_gateways() 0 12 2
A request() 0 23 5
A get_status() 0 10 2
A start_transaction() 0 10 2
A get_ideal_issuers() 0 12 2
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay;
4
5
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
6
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\MultiSafepay;
7
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionRequestMessage;
8
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionResponseMessage;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\GatewaysRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\GatewaysResponseMessage;
11
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\IDealIssuersRequestMessage;
12
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\IDealIssuersResponseMessage;
13
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage;
14
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionResponseMessage;
15
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage;
16
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusResponseMessage;
17
use SimpleXMLElement;
18
19
/**
20
 * Title: MultiSafepay Connect client
21
 * Description:
22
 * Copyright: 2005-2020 Pronamic
23
 * Company: Pronamic
24
 *
25
 * @author  Remco Tolsma
26
 * @version 2.0.5
27
 * @since   1.0.0
28
 */
29
class Client {
30
	/**
31
	 * API URL
32
	 *
33
	 * @var string
34
	 */
35
	public $api_url;
36
37
	/**
38
	 * Constructs and initializes an MultiSafepay Connect client
39
	 */
40 3
	public function __construct() {
41 3
		$this->api_url = MultiSafepay::API_PRODUCTION_URL;
42 3
	}
43
44
	/**
45
	 * Parse XML.
46
	 *
47
	 * @param SimpleXMLElement $xml XML to parse.
48
	 *
49
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
50
	 */
51 3
	private function parse_xml( $xml ) {
52 3
		switch ( $xml->getName() ) {
53 3
			case IDealIssuersRequestMessage::NAME:
54 1
				return IDealIssuersResponseMessage::parse( $xml );
55
56 2
			case GatewaysRequestMessage::NAME:
57 1
				return GatewaysResponseMessage::parse( $xml );
58
59 1
			case DirectTransactionRequestMessage::NAME:
60 1
				return DirectTransactionResponseMessage::parse( $xml );
61
62
			case RedirectTransactionRequestMessage::NAME:
63
				return RedirectTransactionResponseMessage::parse( $xml );
64
65
			case StatusRequestMessage::NAME:
66
				return StatusResponseMessage::parse( $xml );
67
		}
68
69
		return false;
70
	}
71
72
	/**
73
	 * Request.
74
	 *
75
	 * @param string $message Message.
76
	 *
77
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
78
	 */
79 3
	private function request( $message ) {
80 3
		$result = Core_Util::remote_get_body(
81 3
			$this->api_url,
82 3
			200,
83
			array(
84 3
				'method' => 'POST',
85 3
				'body'   => (string) $message,
86
			)
87
		);
88
89 3
		if ( is_wp_error( $result ) ) {
90
			throw new \Exception( $result->get_error_message() );
91
		}
92
93 3
		$xml = Core_Util::simplexml_load_string( $result );
0 ignored issues
show
Bug introduced by Remco Tolsma
It seems like $result can also be of type WP_Error; however, parameter $string of Pronamic\WordPress\Pay\C...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

93
		$xml = Core_Util::simplexml_load_string( /** @scrutinizer ignore-type */ $result );
Loading history...
94
95 3
		$return = $this->parse_xml( $xml );
96
97 3
		if ( is_object( $return ) && isset( $return->result ) && 'error' === $return->result ) {
98
			throw new \Exception( $xml->error->description );
99
		}
100
101 3
		return $return;
102
	}
103
104
	/**
105
	 * Get iDEAL issuers
106
	 *
107
	 * @param Merchant $merchant Merchant.
108
	 *
109
	 * @since 1.2.0
110
	 */
111 1
	public function get_ideal_issuers( $merchant ) {
112 1
		$return = false;
113
114 1
		$request = new IDealIssuersRequestMessage( $merchant );
115
116 1
		$response = $this->request( $request );
117
118 1
		if ( $response ) {
119 1
			$return = $response->issuers;
0 ignored issues
show
Bug introduced by Remco Tolsma
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by Remco Tolsma
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
Bug introduced by Remco Tolsma
The property issuers does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean issuer_id?
Loading history...
120
		}
121
122 1
		return $return;
123
	}
124
125
	/**
126
	 * Get gateways.
127
	 *
128
	 * @param Merchant $merchant Merchant.
129
	 * @param Customer $customer Customer.
130
	 *
131
	 * @since 1.2.0
132
	 */
133 1
	public function get_gateways( $merchant, $customer ) {
134 1
		$return = false;
135
136 1
		$request = new GatewaysRequestMessage( $merchant, $customer );
137
138 1
		$response = $this->request( $request );
139
140 1
		if ( $response ) {
141 1
			$return = $response->gateways;
0 ignored issues
show
Bug introduced by Remco Tolsma
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by Remco Tolsma
The property gateways does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean gateway_info?
Loading history...
Bug introduced by Remco Tolsma
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
142
		}
143
144 1
		return $return;
145
	}
146
147
	/**
148
	 * Start transaction
149
	 *
150
	 * @param array $message Message.
151
	 */
152 1
	public function start_transaction( $message ) {
153 1
		$return = false;
154
155 1
		$response = $this->request( $message );
0 ignored issues
show
Bug introduced by Remco Tolsma
$message of type array is incompatible with the type string expected by parameter $message of Pronamic\WordPress\Pay\G...fepay\Client::request(). ( Ignorable by Annotation )

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

155
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
156
157 1
		if ( $response ) {
158 1
			$return = $response;
159
		}
160
161 1
		return $return;
162
	}
163
164
	/**
165
	 * Get status
166
	 *
167
	 * @param array $message Message.
168
	 */
169
	public function get_status( $message ) {
170
		$return = false;
171
172
		$response = $this->request( $message );
0 ignored issues
show
Bug introduced by Remco Tolsma
$message of type array is incompatible with the type string expected by parameter $message of Pronamic\WordPress\Pay\G...fepay\Client::request(). ( Ignorable by Annotation )

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

172
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
173
174
		if ( $response ) {
175
			$return = $response;
176
		}
177
178
		return $return;
179
	}
180
}
181