Test Failed
Push — master ( 11704a...7617ec )
by Reüel
13:39 queued 05:55
created

rest_api_adyen_payments_result()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 72
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 14.789

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
eloc 38
c 4
b 0
f 0
nc 6
nop 1
dl 0
loc 72
ccs 12
cts 32
cp 0.375
crap 14.789
rs 8.6897

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
 * Payments result controller
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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 JsonSchema\Exception\ValidationException;
14
use Pronamic\WordPress\Pay\Plugin;
15
use WP_Error;
16
use WP_REST_Request;
17
18
/**
19
 * Payments result controller
20
 *
21
 * @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete
22
 *
23
 * @author  Remco Tolsma
24
 * @version 1.0.3
25
 * @since   1.0.0
26
 */
27
class PaymentsResultController {
28
	/**
29
	 * Setup.
30
	 *
31
	 * @return void
32
	 */
33 5
	public function setup() {
34 5
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
35 5
	}
36
37
	/**
38
	 * REST API init.
39
	 *
40
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
41
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
42
	 *
43
	 * @return void
44
	 */
45 10
	public function rest_api_init() {
46 10
		register_rest_route(
47 10
			Integration::REST_ROUTE_NAMESPACE,
48 10
			'/payments/result/(?P<config_id>\d+)',
49
			array(
50 10
				'methods'             => 'POST',
51 10
				'callback'            => array( $this, 'rest_api_adyen_payments_result' ),
52
				'permission_callback' => '__return_true',
53
				'args'                => array(
54 10
					'config_id'  => array(
55 10
						'description' => __( 'Gateway configuration ID.', 'pronamic_ideal' ),
56
						'type'        => 'integer',
57
					),
58 10
					'payload'    => array(
59 10
						'description' => __( 'Payload.', 'pronamic_ideal' ),
60
						'type'        => 'string',
61
					),
62 10
					'resultCode' => array(
63 10
						'description' => __( 'Result code.', 'pronamic_ideal' ),
64
						'type'        => 'string',
65
					),
66 10
					'resultText' => array(
67 10
						'description' => __( 'Result text.', 'pronamic_ideal' ),
68
						'type'        => 'string',
69
					),
70
				),
71
			)
72 10
		);
73
	}
74
75
	/**
76
	 * REST API Adyen payments result handler.
77
	 *
78
	 * @param WP_REST_Request $request Request.
79
	 * @return object
80 2
	 */
81 2
	public function rest_api_adyen_payments_result( WP_REST_Request $request ) {
82
		$config_id = $request->get_param( 'config_id' );
83 2
84
		if ( null === $config_id ) {
85
			return new WP_Error(
86
				'pronamic-pay-adyen-no-gateway-configuration-id',
87
				__( 'No gateway configuration ID given in `config_id` parameter.', 'pronamic_ideal' )
88
			);
89
		}
90 2
91
		$payload = $request->get_param( 'payload' );
92 2
93 1
		if ( null === $payload ) {
94 1
			return new WP_Error(
95 1
				'pronamic-pay-adyen-no-payload',
96
				__( 'No payload given in `payload` parameter.', 'pronamic_ideal' )
97
			);
98
		}
99
100 1
		// Gateway.
101
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
102 1
103
		if ( empty( $gateway ) ) {
104
			return new WP_Error(
105
				'pronamic-pay-adyen-gateway-not-found',
106
				sprintf(
107
					/* translators: %s: Gateway configuration ID */
108
					__( 'Could not found gateway with ID `%s`.', 'pronamic_ideal' ),
109
					$config_id
110
				),
111
				$config_id
112
			);
113
		}
114 1
115
		if ( ! isset( $gateway->client ) ) {
116
			return new WP_Error(
117
				'pronamic-pay-adyen-client-not-found',
118
				sprintf(
119
					/* translators: %s: Gateway configuration ID */
120
					__( 'Could not found client in gateway with ID `%s`.', 'pronamic_ideal' ),
121
					$config_id
122
				),
123
				$config_id
124
			);
125
		}
126
127 1
		// Client.
128
		$payment_result_request = new PaymentResultRequest( $payload );
129 1
130
		$payment_result_response = $gateway->client->get_payment_result( $payment_result_request );
131
132
		$merchant_reference = $payment_result_response->get_merchant_reference();
133
134
		// Payment.
135
		$payment = get_pronamic_payment( $merchant_reference );
136
137
		if ( empty( $payment ) ) {
138
			return new WP_Error(
139
				'pronamic-pay-adyen-payment-not-found',
140
				sprintf(
141
					/* translators: %s: Adyen merchant reference */
142
					__( 'Could not found payment with ID `%s`.', 'pronamic_ideal' ),
143
					$merchant_reference
144
				),
145
				$payment_result_response
146
			);
147
		}
148
149
		PaymentResultHelper::update_payment( $payment, $payment_result_response );
150
151
		// Return payment result response.
152
		return $payment_result_response->get_json();
153
	}
154
}
155