Failed Conditions
Push — develop ( 8495a3...d3c302 )
by Remco
08:35 queued 03:06
created

rest_api_omnikassa_2_webhook_item()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 67
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 37
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 67
ccs 0
cts 49
cp 0
crap 42
rs 8.7057

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
 * Webhook controller
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use Pronamic\WordPress\Pay\GatewayPostType;
14
use Pronamic\WordPress\Pay\Plugin;
15
16
/**
17
 * Webhook controller
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.3.3
21
 * @since   2.3.0
22
 */
23
class WebhookController {
24
	/**
25
	 * Setup.
26
	 *
27
	 * @return void
28
	 */
29
	public function setup() {
30
		\add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
31
32
		\add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
33
	}
34
35
	/**
36
	 * REST API init.
37
	 *
38
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
39
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
40
	 * @return void
41
	 */
42
	public function rest_api_init() {
43
		\register_rest_route(
44
			Integration::REST_ROUTE_NAMESPACE,
45
			'/webhook',
46
			array(
47
				'methods'             => 'POST',
48
				'callback'            => array( $this, 'rest_api_omnikassa_2_webhook' ),
49
				'permission_callback' => '__return_true',
50
			)
51
		);
52
53
		\register_rest_route(
54
			Integration::REST_ROUTE_NAMESPACE,
55
			'/webhook/(?P<id>[\d]+)',
56
			array(
57
				'args'   => array(
58
					'id' => array(
59
						'description' => __( 'Unique identifier for the gateway configuration post.', 'pronamic_ideal' ),
60
						'type'        => 'integer',
61
					),
62
				),
63
				'methods'             => 'POST',
64
				'callback'            => array( $this, 'rest_api_omnikassa_2_webhook_item' ),
65
				'permission_callback' => '__return_true',
66
			)
67
		);
68
	}
69
70
	/**
71
	 * REST API OmniKassa 2.0 webhook handler.
72
	 *
73
	 * @param \WP_REST_Request $request Request.
74
	 * @return object
75
	 * @throws \Exception Throws exception when something unexpected happens ;-).
76
	 */
77
	public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) {
78
		// Query.
79
		$query = new \WP_Query(
80
			array(
81
				'post_type'   => GatewayPostType::POST_TYPE,
82
				'post_status' => 'publish',
83
				'nopaging'    => true,
84
				'meta_query'  => array(
85
					array(
86
						'key'   => '_pronamic_gateway_id',
87
						'value' => 'rabobank-omnikassa-2',
88
					),
89
				),
90
			)
91
		);
92
93
		$results = array();
94
95
		foreach ( $query->posts as $post ) {
96
			$id = \get_post_field( 'ID', $post );
97
98
			$request->set_param( 'id', $id );
99
100
			$results[] = $this->rest_api_omnikassa_2_webhook_item( $request );
101
		}
102
103
		// Response.
104
		$response = new \WP_REST_Response( array(
105
			'success' => true,
106
			'results' => $results,
107
		) );
108
109
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
110
111
		return $response;
112
	}
113
114
	/**
115
	 * REST API OmniKassa 2.0 webhook handler.
116
	 *
117
	 * @param \WP_REST_Request $request Request.
118
	 * @return object
119
	 * @throws \Exception Throws exception when something unexpected happens ;-).
120
	 */
121
	public function rest_api_omnikassa_2_webhook_item( \WP_REST_Request $request ) {
122
		// Input.
123
		$json = $request->get_body();
124
125
		try {
126
			$notification = Notification::from_json( $json );
127
		} catch ( \JsonSchema\Exception\ValidationException $e ) {
128
			// Invalid input data.
129
			return new \WP_Error(
130
				'rest_omnikassa_2_notification_invalid',
131
				\__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ),
132
				array(
133
					'status'       => 400,
134
					'notification' => $json,
135
				)
136
			);
137
		}
138
139
		// Gateway configuration.
140
		$id = $request->get_param( 'id' );
141
142
		if ( null === $id ) {
143
			return new \WP_Error(
144
				'rest_omnikassa_2_gateway_no_id',
145
				__( 'No gateway ID given in `id` parameter.', 'pronamic_ideal' )
146
			);
147
		}
148
149
		$gateway = Plugin::get_gateway( $id );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\Plugin::get_gateway($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...
150
151
		if ( ! $gateway instanceof Gateway ) {
152
			// Invalid gateway.
153
			return new \WP_Error(
154
				'rest_omnikassa_2_gateway_invalid',
155
				\__( 'Invalid OmniKassa 2.0 gateway.', 'pronamic_ideal ' ),
156
				array(
157
					'status' => 400,
158
					'id'     => $id,
159
				)
160
			);
161
		}
162
163
		try {
164
			$gateway->handle_notification( $notification );
165
		} catch ( \Pronamic\WordPress\Pay\Gateways\OmniKassa2\UnknownOrderIdsException $e ) {
166
			/**
167
			 * We don't return an error for unknown order IDs, since OmniKassa
168
			 * otherwise assumes that the notification could not be processed.
169
			 */
170
		} catch ( \Exception $e ) {
171
			return new \WP_Error(
172
				'rest_omnikassa_2_exception',
173
				 $e->getMessage(),
174
				array(
175
					'status'       => 400,
176
					'notification' => $json,
177
					'id'           => $id,
178
				)
179
			);
180
		}
181
		
182
		// Response.
183
		$response = new \WP_REST_Response( $data );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
184
185
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
186
187
		return $response;
188
	}
189
190
	/**
191
	 * WordPress loaded, check for deprecated webhook call.
192
	 *
193
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
194
	 * @return void
195
	 */
196
	public function wp_loaded() {
197
		if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) {
198
			return;
199
		}
200
201
		\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' );
202
203
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
204
	}
205
}
206