Test Failed
Push — develop ( 27299e...c51b01 )
by Reüel
05:16
created

Client::send_request()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 75
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7.0018

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 38
c 4
b 0
f 0
nc 7
nop 2
dl 0
loc 75
ccs 29
cts 30
cp 0.9667
crap 7.0018
rs 8.3786

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
 * Adyen client
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use Exception;
14
use WP_Error;
15
16
/**
17
 * Adyen client
18
 *
19
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 */
25
class Client {
26
	/**
27
	 * Config.
28
	 *
29
	 * @var Config
30
	 */
31
	private $config;
32
33
	/**
34
	 * Constructs and initializes an Adyen client object.
35
	 *
36
	 * @param Config $config Adyen config.
37
	 */
38 11
	public function __construct( Config $config ) {
39 11
		$this->config = $config;
40 11
	}
41
42
	/**
43
	 * Send request with the specified action and parameters
44
	 *
45
	 * @param string  $method  Adyen API method.
46
	 * @param Request $request Request object.
47
	 * @return object
48
	 * @throws Exception Throws exception when error occurs.
49
	 */
50 10
	private function send_request( $method, $request ) {
51
		// Request.
52 10
		$url = $this->config->get_api_url( $method );
53
54 9
		$response = wp_remote_request(
55 9
			$url,
56
			array(
57 9
				'method'  => 'POST',
58
				'headers' => array(
59 9
					'X-API-key'    => $this->config->get_api_key(),
60 9
					'Content-Type' => 'application/json',
61
				),
62 9
				'body'    => wp_json_encode( $request->get_json() ),
63
			)
64
		);
65
66 9
		if ( $response instanceof WP_Error ) {
67
			throw new \Pronamic\WordPress\Pay\GatewayException( 'adyen', $response->get_error_message() );
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\GatewayException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
		}
69
70
		// Body.
71 9
		$body = wp_remote_retrieve_body( $response );
72
73 9
		// Handle HTTP 4xx and 5xx response.
74
		// @link https://docs.adyen.com/development-resources/response-handling
75
		$response_code = wp_remote_retrieve_response_code( $response );
76 9
77
		if ( in_array( $response_code, array( 400, 401, 403, 404, 422, 500 ), true ) ) {
78 9
			throw new \Pronamic\WordPress\Pay\GatewayException(
79 1
				'adyen',
80 1
				sprintf( '%1$s (%2$s)', wp_remote_retrieve_response_message( $response ), $response_code ),
81
				$response
82
			);
83
		}
84
85
		$data = json_decode( $body );
86 8
87 1
		// JSON error.
88
		$json_error = json_last_error();
89 1
90 1
		if ( JSON_ERROR_NONE !== $json_error ) {
91 1
			throw new \Pronamic\WordPress\Pay\GatewayException(
92
				'adyen',
93
				sprintf( 'JSON: %s', json_last_error_msg() ),
94
				$json_error
95
			);
96 7
		}
97 2
98
		// Object.
99 2
		if ( ! is_object( $data ) ) {
100
			$code = wp_remote_retrieve_response_code( $response );
101
102
			throw new \Pronamic\WordPress\Pay\GatewayException(
103
				'adyen',
104 5
				sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ),
105 1
				intval( $code )
106
			);
107 1
		}
108
109
		// Error.
110 4
		if ( isset( $data->error ) ) {
111
			$error = Error::from_object( $data->error );
112
113
			throw $error;
114
		}
115
116
		// Service Exception.
117
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
118
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
119
			$service_exception = ServiceException::from_object( $data );
120
121
			throw $service_exception;
122 1
		}
123 1
124
		return $data;
125 1
	}
126
127
	/**
128
	 * Create payment.
129
	 *
130
	 * @param PaymentRequest $request Payment request.
131
	 *
132
	 * @return PaymentResponse
133
	 *
134
	 * @throws Exception Throws error if request fails.
135
	 */
136
	public function create_payment( PaymentRequest $request ) {
137 1
		$data = $this->send_request( 'payments', $request );
138 1
139
		return PaymentResponse::from_object( $data );
140 1
	}
141
142
	/**
143
	 * Create payment session.
144
	 *
145
	 * @param PaymentSessionRequest $request Payment session request.
146
	 *
147
	 * @return PaymentSessionResponse
148
	 *
149
	 * @throws Exception Throws error if request fails.
150
	 */
151
	public function create_payment_session( PaymentSessionRequest $request ) {
152 2
		$data = $this->send_request( 'paymentSession', $request );
153 2
154
		return PaymentSessionResponse::from_object( $data );
155 1
	}
156
157
	/**
158
	 * Get payment result.
159
	 *
160
	 * @param PaymentResultRequest $request Payment result request.
161
	 *
162
	 * @return PaymentResultResponse
163
	 *
164
	 * @throws Exception Throws error if request fails.
165 6
	 */
166 6
	public function get_payment_result( PaymentResultRequest $request ) {
167
		$data = $this->send_request( 'payments/result', $request );
168 6
169
		return PaymentResultResponse::from_object( $data );
170 1
	}
171
172
	/**
173
	 * Get payment methods.
174
	 *
175
	 * @return PaymentMethodsResponse
176
	 *
177
	 * @throws Exception Throws error if request fails.
178
	 */
179
	public function get_payment_methods() {
180
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
181
182
		$data = $this->send_request( 'paymentMethods', $request );
183
184
		return PaymentMethodsResponse::from_object( $data );
185
	}
186
}
187