Passed
Push — develop ( 3de7dc...9810f4 )
by Remco
03:47
created

Client::send_request()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 60
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6.5064

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 6
nop 2
dl 0
loc 60
ccs 22
cts 29
cp 0.7586
crap 6.5064
rs 8.8177
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
 * 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 Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\XML\Security;
16
use WP_Error;
17
18
/**
19
 * Adyen client
20
 *
21
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.0
25
 * @since   1.0.0
26
 */
27
class Client {
28
	/**
29
	 * Config.
30
	 *
31
	 * @var Config
32
	 */
33
	private $config;
34
35
	/**
36
	 * Constructs and initializes an Adyen client object.
37
	 *
38
	 * @param Config $config Adyen config.
39
	 */
40 7
	public function __construct( Config $config ) {
41 7
		$this->config = $config;
42 7
	}
43
44
	/**
45
	 * Send request with the specified action and parameters
46
	 *
47
	 * @param string  $method  Adyen API method.
48
	 * @param Request $request Request object.
49
	 * @return object
50
	 * @throws Exception Throws exception when error occurs.
51
	 */
52 7
	private function send_request( $method, $request ) {
53
		// Request.
54 7
		$url = $this->config->get_api_url( $method );
55
56 7
		$response = wp_remote_request(
57 7
			$url,
58
			array(
59 7
				'method'  => 'POST',
60
				'headers' => array(
61 7
					'X-API-key'    => $this->config->get_api_key(),
62 7
					'Content-Type' => 'application/json',
63
				),
64 7
				'body'    => wp_json_encode( $request->get_json() ),
65
			)
66
		);
67
68 7
		if ( $response instanceof WP_Error ) {
69 1
			throw new Exception( $response->get_error_message() );
70
		}
71
72
		// Body.
73 6
		$body = wp_remote_retrieve_body( $response );
74
75 6
		$data = json_decode( $body );
76
77
		// JSON error.
78 6
		$json_error = json_last_error();
79
80 6
		if ( JSON_ERROR_NONE !== $json_error ) {
81
			throw new Exception(
82
				json_last_error_msg(),
83
				$json_error
84
			);
85
		}
86
87
		// Object.
88 6
		if ( ! is_object( $data ) ) {
89
			$code = wp_remote_retrieve_response_code( $response );
90
91
			throw new Exception(
92
				sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ),
93
				intval( $code )
94
			);
95
		}
96
97
		// Error.
98 6
		if ( isset( $data->error ) ) {
99 1
			$error = Error::from_object( $data->error );
100
101 1
			throw $error;
102
		}
103
104
		// Service Exception.
105 5
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
106 1
			$service_exception = ServiceException::from_object( $data );
107
108 1
			throw $service_exception;
109
		}
110
111 4
		return $data;
112
	}
113
114
	/**
115
	 * Create payment.
116
	 *
117
	 * @param PaymentRequest $request Payment request.
118
	 * @return PaymentResponse
119
	 */
120 1
	public function create_payment( PaymentRequest $request ) {
121 1
		$data = $this->send_request( 'payments', $request );
122
123 1
		return PaymentResponse::from_object( $data );
124
	}
125
126
	/**
127
	 * Create payment session.
128
	 *
129
	 * @param PaymentSessionRequest $request Payment session request.
130
	 * @return PaymentSessionResponse
131
	 */
132 1
	public function create_payment_session( PaymentSessionRequest $request ) {
133 1
		$data = $this->send_request( 'paymentSession', $request );
134
135 1
		return PaymentSessionResponse::from_object( $data );
136
	}
137
138
	/**
139
	 * Get payment result.
140
	 *
141
	 * @param PaymentResultRequest $request Payment result request.
142
	 * @return PaymentResultResponse
143
	 */
144 1
	public function get_payment_result( PaymentResultRequest $request ) {
145 1
		$data = $this->send_request( 'payments/result', $request );
146
147 1
		return PaymentResultResponse::from_object( $data );
148
	}
149
150
	/**
151
	 * Get payment methods.
152
	 *
153
	 * @return PaymentMethodsResponse
154
	 */
155 4
	public function get_payment_methods() {
156 4
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
157
158 4
		$data = $this->send_request( 'paymentMethods', $request );
159
160 1
		return PaymentMethodsResponse::from_object( $data );
161
	}
162
}
163