Passed
Push — master ( b8e26c...2f113f )
by Remco
05:42
created

Client::request()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 57
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 12
nop 4
dl 0
loc 57
rs 8.5546
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Client
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use WP_Error;
14
15
/**
16
 * Client
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.0.2
20
 * @since   1.0.0
21
 */
22
class Client {
23
	/**
24
	 * URL OmniKassa API.
25
	 *
26
	 * @var string
27
	 */
28
	const URL_PRODUCTION = 'https://betalen.rabobank.nl/omnikassa-api/';
29
30
	/**
31
	 * URL OmniKassa sandbox API.
32
	 *
33
	 * @var string
34
	 */
35
	const URL_SANDBOX = 'https://betalen.rabobank.nl/omnikassa-api-sandbox/';
36
37
	/**
38
	 * Error
39
	 *
40
	 * @var WP_Error
41
	 */
42
	private $error;
43
44
	/**
45
	 * The URL.
46
	 *
47
	 * @var string
48
	 */
49
	private $url;
50
51
	/**
52
	 * Error.
53
	 *
54
	 * @return WP_Error
55
	 */
56
	public function get_error() {
57
		return $this->error;
58
	}
59
60
	/**
61
	 * Get the URL.
62
	 *
63
	 * @return string
64
	 */
65
	public function get_url() {
66
		return $this->url;
67
	}
68
69
	/**
70
	 * Set the action URL
71
	 *
72
	 * @param string $url URL.
73
	 */
74
	public function set_url( $url ) {
75
		$this->url = $url;
76
	}
77
78
	/**
79
	 * Get refresh token.
80
	 *
81
	 * @return string
82
	 */
83
	public function get_refresh_token() {
84
		return $this->refresh_token;
85
	}
86
87
	/**
88
	 * Set refresh token.
89
	 *
90
	 * @param string $refresh_token Refresh token.
91
	 */
92
	public function set_refresh_token( $refresh_token ) {
93
		$this->refresh_token = $refresh_token;
0 ignored issues
show
Bug Best Practice introduced by
The property refresh_token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94
	}
95
96
	/**
97
	 * Get signing key.
98
	 *
99
	 * @return string
100
	 */
101
	public function get_signing_key() {
102
		return $this->signing_key;
103
	}
104
105
	/**
106
	 * Set signing key.
107
	 *
108
	 * @param string $signing_key Signing key.
109
	 */
110
	public function set_signing_key( $signing_key ) {
111
		$this->signing_key = $signing_key;
0 ignored issues
show
Bug Best Practice introduced by
The property signing_key does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
	}
113
114
	/**
115
	 * Request URL with specified method, token.
116
	 *
117
	 * @param string      $method   HTTP request method.
118
	 * @param string      $endpoint URL endpoint to request.
119
	 * @param string      $token    Authorization token.
120
	 * @param object|null $object   Object.
121
	 */
122
	private function request( $method, $endpoint, $token, $object = null ) {
123
		// URL.
124
		$url = $this->get_url() . $endpoint;
125
126
		// Arguments.
127
		$args = array(
128
			'method'  => $method,
129
			'headers' => array(
130
				'Authorization' => 'Bearer ' . $token,
131
			),
132
		);
133
134
		if ( null !== $object ) {
135
			$args['headers']['Content-Type'] = 'application/json';
136
137
			$args['body'] = wp_json_encode( $object );
138
		}
139
140
		// Request.
141
		$response = wp_remote_request( $url, $args );
142
143
		if ( is_wp_error( $response ) ) {
144
			$this->error = $response;
0 ignored issues
show
Documentation Bug introduced by
It seems like $response can also be of type array. 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...
145
146
			$this->error->add( 'omnikassa_2_error', 'HTTP Request Failed' );
147
148
			return false;
149
		}
150
151
		// Body.
152
		$body = wp_remote_retrieve_body( $response );
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_body() does only seem to accept array, 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

152
		$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
153
154
		$data = json_decode( $body );
155
156
		if ( ! is_object( $data ) ) {
157
			$this->error = new \WP_Error( 'omnikassa_2_error', 'Could not parse response.', $data );
158
159
			return false;
160
		}
161
162
		// Error.
163
		if ( isset( $data->errorCode ) ) {
164
			$message = 'Unknown error.';
165
166
			if ( isset( $data->consumerMessage ) ) {
167
				$message = $data->consumerMessage;
168
			} elseif ( isset( $data->errorMessage ) ) {
169
				$message = $data->errorMessage;
170
			}
171
172
			$this->error = new \WP_Error( 'omnikassa_2_error', $message, $data );
173
174
			return false;
175
		}
176
177
		// Ok.
178
		return $data;
179
	}
180
181
	/**
182
	 * Get access token.
183
	 *
184
	 * @return string
185
	 */
186
	public function get_access_token_data() {
187
		return $this->request( 'GET', 'gatekeeper/refresh', $this->get_refresh_token() );
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->request('G...s->get_refresh_token()) returns the type object|false which is incompatible with the documented return type string.
Loading history...
188
	}
189
190
	/**
191
	 * Order announce.
192
	 *
193
	 * @param Config $config Config.
194
	 * @param Order  $order  Order.
195
	 * @return object|bool
196
	 */
197
	public function order_announce( $config, Order $order ) {
198
		$object = $order->get_json();
199
200
		$object->signature = Security::get_signature( $order, $config->signing_key );
201
202
		return $this->request( 'POST', 'order/server/api/order', $config->access_token, $object );
203
	}
204
205
	/**
206
	 * Get order results by the notification token.
207
	 *
208
	 * @param string $notification_token Notification token.
209
	 * @return OrderResults
210
	 */
211
	public function get_order_results( $notification_token ) {
212
		$result = $this->request( 'GET', 'order/server/api/events/results/merchant.order.status.changed', $notification_token );
213
214
		return OrderResults::from_object( $result );
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type false; however, parameter $object of Pronamic\WordPress\Pay\G...rResults::from_object() does only seem to accept stdClass, 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

214
		return OrderResults::from_object( /** @scrutinizer ignore-type */ $result );
Loading history...
215
	}
216
}
217