Failed Conditions
Push — develop ( 53a6f7...3b06d5 )
by Reüel
07:12 queued 11s
created

Client::start_transaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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-2019 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
		$xml = Core_Util::simplexml_load_string( $result );
0 ignored issues
show
Bug introduced by
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

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

151
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
152
153 1
		if ( $response ) {
154 1
			$return = $response;
155
		}
156
157 1
		return $return;
158
	}
159
160
	/**
161
	 * Get status
162
	 *
163
	 * @param array $message Message.
164
	 */
165
	public function get_status( $message ) {
166
		$return = false;
167
168
		$response = $this->request( $message );
0 ignored issues
show
Bug introduced by
$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

168
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
169
170
		if ( $response ) {
171
			$return = $response;
172
		}
173
174
		return $return;
175
	}
176
}
177