Completed
Push — master ( 83a557...8e0143 )
by Remco
11:52 queued 05:23
created

Client::send_request()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 61
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6.0016

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 30
c 3
b 0
f 0
nc 6
nop 2
dl 0
loc 61
ccs 27
cts 28
cp 0.9643
crap 6.0016
rs 8.8177

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