Passed
Push — master ( d4ad7c...cc3467 )
by Reüel
07:39 queued 12s
created

Client::send_request()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 94
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 7.0005

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 7
eloc 46
c 6
b 0
f 0
nc 7
nop 2
dl 0
loc 94
ccs 43
cts 44
cp 0.9773
crap 7.0005
rs 8.2448

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
/**
14
 * Adyen client
15
 *
16
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.5
20
 * @since   1.0.0
21
 */
22
class Client {
23
	/**
24
	 * Config.
25
	 *
26
	 * @var Config
27
	 */
28
	private $config;
29
30
	/**
31
	 * Constructs and initializes an Adyen client object.
32
	 *
33
	 * @param Config $config Adyen config.
34
	 */
35 12
	public function __construct( Config $config ) {
36 12
		$this->config = $config;
37 12
	}
38
39
	/**
40
	 * Send request with the specified action and parameters
41
	 *
42
	 * @param string  $method  Adyen API method.
43
	 * @param Request $request Request object.
44
	 * @return object
45
	 * @throws \Exception Throws exception when error occurs.
46
	 */
47 11
	private function send_request( $method, $request ) {
48
		// Request.
49 11
		$url = $this->config->get_api_url( $method );
50
51 10
		$response = \wp_remote_request(
52 10
			$url,
53
			array(
54 10
				'method'  => 'POST',
55
				'headers' => array(
56 10
					'X-API-key'    => $this->config->get_api_key(),
57 10
					'Content-Type' => 'application/json',
58
				),
59 10
				'body'    => \wp_json_encode( $request->get_json() ),
60
			)
61
		);
62
63 10
		if ( $response instanceof \WP_Error ) {
64
			throw new \Exception( $response->get_error_message() );
65
		}
66
67
		// Body.
68 10
		$body = \wp_remote_retrieve_body( $response );
69
70
		// Response.
71 10
		$response_code    = \wp_remote_retrieve_response_code( $response );
72 10
		$response_message = \wp_remote_retrieve_response_message( $response );
73
74
		/**
75
		 * On PHP 7 or higher the `json_decode` function will return `null` and
76
		 * `json_last_error` will return `4` (Syntax error). On PHP 5.6 or lower
77
		 * the `json_decode` will also return `null`, but json_last_error` will
78
		 * return `0` (No error). Therefor we check if the HTTP response body
79
		 * is an empty string.
80
		 *
81
		 * @link https://3v4l.org/
82
		 */
83 10
		if ( '' === $body ) {
84 1
			throw new \Exception(
85 1
				\sprintf(
86 1
					'Adyen response is empty, HTTP response: "%s %s".',
87 1
					$response_code,
88 1
					$response_message
89
				)
90
			);
91
		}
92
93
		// JSON.
94 9
		$data = json_decode( $body );
95
96
		// JSON error.
97 9
		$json_error = json_last_error();
98
99 9
		if ( \JSON_ERROR_NONE !== $json_error ) {
100 1
			throw new \Exception(
101 1
				\sprintf(
102 1
					'Could not JSON decode Adyen response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".',
103 1
					$response_code,
104 1
					$response_message,
105 1
					\strlen( $body ),
106 1
					\json_last_error_msg()
107
				),
108
				$json_error
109
			);
110
		}
111
112
		// Object.
113 8
		if ( ! \is_object( $data ) ) {
114 1
			throw new \Exception(
115 1
				\sprintf(
116 1
					'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".',
117 1
					$response_code,
118 1
					$response_message,
119 1
					$body
120
				),
121 1
				\intval( $response_code )
122
			);
123
		}
124
125
		// Error.
126 7
		if ( isset( $data->error ) ) {
127 2
			$error = Error::from_object( $data->error );
128
129 2
			throw $error;
130
		}
131
132
		// Service Exception.
133
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
134 5
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
135 1
			$service_exception = ServiceException::from_object( $data );
136
137 1
			throw $service_exception;
138
		}
139
140 4
		return $data;
141
	}
142
143
	/**
144
	 * Create payment.
145
	 *
146
	 * @param PaymentRequest $request Payment request.
147
	 *
148
	 * @return PaymentResponse
149
	 *
150
	 * @throws \Exception Throws error if request fails.
151
	 */
152 1
	public function create_payment( PaymentRequest $request ) {
153 1
		$data = $this->send_request( 'payments', $request );
154
155 1
		return PaymentResponse::from_object( $data );
156
	}
157
158
	/**
159
	 * Create payment session.
160
	 *
161
	 * @param PaymentSessionRequest $request Payment session request.
162
	 *
163
	 * @return PaymentSessionResponse
164
	 *
165
	 * @throws \Exception Throws error if request fails.
166
	 */
167 1
	public function create_payment_session( PaymentSessionRequest $request ) {
168 1
		$data = $this->send_request( 'paymentSession', $request );
169
170 1
		return PaymentSessionResponse::from_object( $data );
171
	}
172
173
	/**
174
	 * Get payment result.
175
	 *
176
	 * @param PaymentResultRequest $request Payment result request.
177
	 *
178
	 * @return PaymentResultResponse
179
	 *
180
	 * @throws \Exception Throws error if request fails.
181
	 */
182 2
	public function get_payment_result( PaymentResultRequest $request ) {
183 2
		$data = $this->send_request( 'payments/result', $request );
184
185 1
		return PaymentResultResponse::from_object( $data );
186
	}
187
188
	/**
189
	 * Get payment methods.
190
	 *
191
	 * @return PaymentMethodsResponse
192
	 *
193
	 * @throws \Exception Throws error if request fails.
194
	 */
195 7
	public function get_payment_methods() {
196 7
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
197
198 7
		$data = $this->send_request( 'paymentMethods', $request );
199
200 1
		return PaymentMethodsResponse::from_object( $data );
201
	}
202
}
203