Test Failed
Push — develop ( a23a3e...8638ff )
by Remco
13:30
created

src/Client.php (10 issues)

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
use WP_Error;
19
20
/**
21
 * Title: MultiSafepay Connect client
22
 * Description:
23
 * Copyright: 2005-2019 Pronamic
24
 * Company: Pronamic
25
 *
26
 * @author  Remco Tolsma
27
 * @version 2.0.2
28
 * @since   1.0.0
29
 */
30
class Client {
31
	/**
32
	 * Error
33
	 *
34
	 * @var WP_Error
35
	 */
36
	private $error;
37
38
	/**
39
	 * API URL
40
	 *
41
	 * @var string
42
	 */
43
	public $api_url;
44
45
	/**
46
	 * Constructs and initializes an MultiSafepay Connect client
47
	 */
48 3
	public function __construct() {
49 3
		$this->api_url = MultiSafepay::API_PRODUCTION_URL;
50 3
	}
51
52
	/**
53
	 * Get error
54
	 *
55
	 * @return WP_Error
56
	 */
57
	public function get_error() {
58
		return $this->error;
59
	}
60
61
	/**
62
	 * Parse XML.
63
	 *
64
	 * @param SimpleXMLElement $xml XML to parse.
65
	 *
66
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
67
	 */
68 3
	private function parse_xml( $xml ) {
69 3
		switch ( $xml->getName() ) {
70 3
			case IDealIssuersRequestMessage::NAME:
71 1
				return IDealIssuersResponseMessage::parse( $xml );
72
73 2
			case GatewaysRequestMessage::NAME:
74 1
				return GatewaysResponseMessage::parse( $xml );
75
76 1
			case DirectTransactionRequestMessage::NAME:
77 1
				return DirectTransactionResponseMessage::parse( $xml );
78
79
			case RedirectTransactionRequestMessage::NAME:
80
				return RedirectTransactionResponseMessage::parse( $xml );
81
82
			case StatusRequestMessage::NAME:
83
				return StatusResponseMessage::parse( $xml );
84
		}
85
86
		return false;
87
	}
88
89
	/**
90
	 * Request.
91
	 *
92
	 * @param string $message Message.
93
	 *
94
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
95
	 */
96 3
	private function request( $message ) {
97 3
		$return = false;
98
99 3
		$result = Core_Util::remote_get_body(
100 3
			$this->api_url,
101 3
			200,
102
			array(
103 3
				'method' => 'POST',
104 3
				'body'   => (string) $message,
105
			)
106
		);
107
108 3
		if ( is_wp_error( $result ) ) {
109
			$this->error = $result;
0 ignored issues
show
Documentation Bug introduced by
It seems like $result can also be of type string. However, the property $error is declared as type WP_Error. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
110
111
			return false;
112
		}
113
114 3
		$xml = Core_Util::simplexml_load_string( $result );
0 ignored issues
show
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

114
		$xml = Core_Util::simplexml_load_string( /** @scrutinizer ignore-type */ $result );
Loading history...
115
116 3
		if ( is_wp_error( $xml ) ) {
117
			$this->error = $xml;
118
		} else {
119 3
			$return = $this->parse_xml( $xml );
120
121 3
			if ( is_object( $return ) && isset( $return->result ) && 'error' === $return->result ) {
122
				$this->error = new WP_Error( 'multisafepay_error', $xml->error->description, $xml->error );
123
				$return      = false;
124
			}
125
		}
126
127 3
		return $return;
128
	}
129
130
	/**
131
	 * Get iDEAL issuers
132
	 *
133
	 * @param Merchant $merchant Merchant.
134
	 *
135
	 * @since 1.2.0
136
	 */
137 1
	public function get_ideal_issuers( $merchant ) {
138 1
		$return = false;
139
140 1
		$request = new IDealIssuersRequestMessage( $merchant );
141
142 1
		$response = $this->request( $request );
143
144 1
		if ( $response ) {
145 1
			$return = $response->issuers;
0 ignored issues
show
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
The property issuers does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean issuer_id?
Loading history...
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
146
		}
147
148 1
		return $return;
149
	}
150
151
	/**
152
	 * Get gateways.
153
	 *
154
	 * @param Merchant $merchant Merchant.
155
	 * @param Customer $customer Customer.
156
	 *
157
	 * @since 1.2.0
158
	 */
159 1
	public function get_gateways( $merchant, $customer ) {
160 1
		$return = false;
161
162 1
		$request = new GatewaysRequestMessage( $merchant, $customer );
163
164 1
		$response = $this->request( $request );
165
166 1
		if ( $response ) {
167 1
			$return = $response->gateways;
0 ignored issues
show
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
The property gateways does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean gateway_info?
Loading history...
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
168
		}
169
170 1
		return $return;
171
	}
172
173
	/**
174
	 * Start transaction
175
	 *
176
	 * @param array $message Message.
177
	 */
178 1
	public function start_transaction( $message ) {
179 1
		$return = false;
180
181 1
		$response = $this->request( $message );
0 ignored issues
show
$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

181
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
182
183 1
		if ( $response ) {
184 1
			$return = $response;
185
		}
186
187 1
		return $return;
188
	}
189
190
	/**
191
	 * Get status
192
	 *
193
	 * @param array $message Message.
194
	 */
195
	public function get_status( $message ) {
196
		$return = false;
197
198
		$response = $this->request( $message );
0 ignored issues
show
$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

198
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
199
200
		if ( $response ) {
201
			$return = $response;
202
		}
203
204
		return $return;
205
	}
206
}
207