Test Failed
Push — develop ( 21667b...724bc2 )
by Remco
05:38 queued 13s
created

CLI::insert_or_update_customer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 36
nc 4
nop 3
dl 0
loc 58
rs 9.344
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
 * CLI
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
/**
14
 * Title: CLI
15
 * Description:
16
 * Copyright: 2005-2020 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 3.0.0
21
 * @since   3.0.0
22
 * @link    https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-cli.php
23
 */
24
class CLI {
25
	/**
26
	 * Construct CLI.
27
	 */
28
	public function __construct() {
29
		\WP_CLI::add_command(
0 ignored issues
show
Bug introduced by
The type WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
			'pronamic-pay mollie organizations synchronize',
31
			function( $args, $assoc_args ) {
32
				$this->wp_cli_organizations_synchronize( $args, $assoc_args );
33
			}
34
		);
35
36
		\WP_CLI::add_command(
37
			'pronamic-pay mollie customers synchronize',
38
			function( $args, $assoc_args ) {
39
				$this->wp_cli_customers_synchronize( $args, $assoc_args );
40
			}
41
		);
42
43
		\WP_CLI::add_command(
44
			'pronamic-pay mollie customers connect-wp-users',
45
			function( $args, $assoc_args ) {
46
				$this->wp_cli_customers_connect_wp_users( $args, $assoc_args );
47
			}
48
		);
49
	}
50
51
	/**
52
	 * CLI organizations synchronize.
53
	 *
54
	 * @link https://docs.mollie.com/reference/v2/organizations-api/current-organization
55
	 * @param array $args       Arguments.
56
	 * @param array $assoc_args Associative arguments.
57
	 */
58
	public function wp_cli_organizations_synchronize( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
	public function wp_cli_organizations_synchronize( /** @scrutinizer ignore-unused */ $args, $assoc_args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $assoc_args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
	public function wp_cli_organizations_synchronize( $args, /** @scrutinizer ignore-unused */ $assoc_args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
		\WP_CLI::error( 'Command not implemented yet.' );
60
	}
61
62
	/**
63
	 * Insert or update Mollie profile.
64
	 *
65
	 * @param object $object Mollie profile object.
66
	 * @param array  $data   Data.
67
	 * @param array  $format Format.
68
	 * @return int Mollie proflie ID.
69
	 */
70
	private function insert_or_update_profile( $object, $data = array(), $format = array() ) {
71
		global $wpdb;
72
73
		$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->pronamic_pay_mollie_profiles WHERE mollie_id = %s", $object->id ) );
74
75
		$data['email']   = $object->email;
76
		$format['email'] = '%s';
77
78
		$data['name']   = $object->name;
79
		$format['name'] = '%s';
80
81
		if ( null === $id ) {
82
			$data['mollie_id']   = $object->id;
83
			$foramt['mollie_id'] = '%s';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$foramt was never initialized. Although not strictly required by PHP, it is generally a good practice to add $foramt = array(); before regardless.
Loading history...
84
85
			$result = $wpdb->insert(
86
				$wpdb->pronamic_pay_mollie_profiles,
87
				$data,
88
				$format
89
			);
90
91
			if ( false === $result ) {
92
				\WP_CLI::error(
93
					sprintf(
94
						'Database error: %s.',
95
						$wpdb->last_error
96
					)
97
				);
98
			}
99
100
			$id = $wpdb->insert_id;
101
		} else {
102
			$result = $wpdb->update(
103
				$wpdb->pronamic_pay_mollie_profiles,
104
				$data,
105
				array(
106
					'id' => $id,
107
				),
108
				$format,
109
				array(
110
					'id' => '%d'
111
				)
112
			);
113
114
			if ( false === $result ) {
115
				\WP_CLI::error(
116
					sprintf(
117
						'Database error: %s.',
118
						$wpdb->last_error
119
					)
120
				);
121
			}
122
		}
123
124
		return $id;
125
	}
126
127
	/**
128
	 * Insert or update Mollie customer.
129
	 *
130
	 * @param object $object Mollie customer object.
131
	 * @param array  $data   Data.
132
	 * @param array  $format Format.
133
	 * @return int Mollie customer ID.
134
	 */
135
	private function insert_or_update_customer( $object, $data = array(), $format = array() ) {
136
		global $wpdb;
137
138
		$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->pronamic_pay_mollie_customers WHERE mollie_id = %s", $object->id ) );
139
140
		$data['test_mode']   = ( 'test' === $object->mode );
141
		$format['test_mode'] = '%d';
142
143
		$data['email']   = $object->email;
144
		$format['email'] = '%s';
145
146
		$data['name']   = $object->name;
147
		$format['name'] = '%s';
148
149
		if ( null === $id ) {
150
			$data['mollie_id']   = $object->id;
151
			$foramt['mollie_id'] = '%s';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$foramt was never initialized. Although not strictly required by PHP, it is generally a good practice to add $foramt = array(); before regardless.
Loading history...
152
153
			$result = $wpdb->insert(
154
				$wpdb->pronamic_pay_mollie_customers,
155
				$data,
156
				$format
157
			);
158
159
			if ( false === $result ) {
160
				\WP_CLI::error(
161
					sprintf(
162
						'Database error: %s.',
163
						$wpdb->last_error
164
					)
165
				);
166
			}
167
168
			$id = $wpdb->insert_id;
169
		} else {
170
			$result = $wpdb->update(
171
				$wpdb->pronamic_pay_mollie_customers,
172
				$data,
173
				array(
174
					'id' => $id,
175
				),
176
				$format,
177
				array(
178
					'id' => '%d'
179
				)
180
			);
181
182
			if ( false === $result ) {
183
				\WP_CLI::error(
184
					sprintf(
185
						'Database error: %s.',
186
						$wpdb->last_error
187
					)
188
				);
189
			}
190
		}
191
192
		return $id;
193
	}
194
195
	/**
196
	 * CLI customers synchronize.
197
	 *
198
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
199
	 * @param array $args       Arguments.
200
	 * @param array $assoc_args Associative arguments.
201
	 */
202
	public function wp_cli_customers_synchronize( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $assoc_args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
	public function wp_cli_customers_synchronize( $args, /** @scrutinizer ignore-unused */ $assoc_args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
	public function wp_cli_customers_synchronize( /** @scrutinizer ignore-unused */ $args, $assoc_args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
		global $post;
204
		global $wpdb;
205
206
		$query = new \WP_Query(
207
			array(
208
				'post_type'   => 'pronamic_gateway',
209
				'post_status' => 'publish',
210
				'nopaging'    => true,
211
				'meta_query'  => array(
212
					array(
213
						'key'   => '_pronamic_gateway_id',
214
						'value' => 'mollie',
215
					),
216
					array(
217
						'key'     => '_pronamic_gateway_mollie_api_key',
218
						'compare' => 'EXISTS',
219
					),
220
				),
221
			)
222
		);
223
224
		if ( $query->have_posts() ) {
225
			while ( $query->have_posts() ) {
226
				$query->the_post();
227
228
				$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true );
229
230
				\WP_CLI::log( $post->post_title );
231
				\WP_CLI::log( $api_key );
232
				\WP_CLI::log( '' );
233
234
				$client = new Client( $api_key );
0 ignored issues
show
Bug introduced by
It seems like $api_key can also be of type false; however, parameter $api_key of Pronamic\WordPress\Pay\G...e\Client::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
235
236
				$urls = array(
237
					'https://api.mollie.com/v2/customers?limit=250',
238
				);
239
240
				$profile = $client->get_current_profile();
241
242
				$profile_id = $this->insert_or_update_profile(
243
					$profile,
244
					array(
245
						'api_key_live' => ( 'live_' === substr( $api_key, 0, 5 ) ) ? $api_key : null,
0 ignored issues
show
Bug introduced by
It seems like $api_key can also be of type false; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
						'api_key_live' => ( 'live_' === substr( /** @scrutinizer ignore-type */ $api_key, 0, 5 ) ) ? $api_key : null,
Loading history...
246
						'api_key_test' => ( 'test_' === substr( $api_key, 0, 5 ) ) ? $api_key : null,
247
					),
248
					array(
249
						'api_key_live' => '%s',
250
						'api_key_test' => '%s',
251
					)
252
				);
253
254
				while ( ! empty( $urls ) ) {
255
					$url = array_shift( $urls );
256
257
					\WP_CLI::log( $url );
258
259
					$response = $client->send_request( $url );
260
261
					if ( isset( $response->count ) ) {
262
						\WP_CLI::log(
263
							\sprintf(
264
								'Found %d customer(s).',
265
								$response->count
266
							)
267
						);
268
					}
269
270
					if ( isset( $response->_embedded->customers ) ) {
271
						\WP_CLI\Utils\format_items(
0 ignored issues
show
Bug introduced by
The function format_items was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
						/** @scrutinizer ignore-call */ 
272
      \WP_CLI\Utils\format_items(
Loading history...
272
							'table',
273
							$response->_embedded->customers,
274
							array(
275
								'id',
276
								'mode',
277
								'name',
278
								'email',
279
								'locale',
280
							)
281
						);
282
283
						foreach ( $response->_embedded->customers as $object ) {
284
							$customer_id = $this->insert_or_update_customer(
0 ignored issues
show
Unused Code introduced by
The assignment to $customer_id is dead and can be removed.
Loading history...
285
								$object,
286
								array(
287
									'profile_id' => $profile_id,
288
								),
289
								array(
290
									'profile_id' => '%d',
291
								)
292
							);
293
						}
294
					}
295
296
					if ( isset( $response->_links->next->href ) ) {
297
						$urls[] = $response->_links->next->href;
298
					}
299
				}
300
			}
301
302
			\wp_reset_postdata();
303
		}
304
	}
305
306
	/**
307
	 * CLI connect Mollie customers to WordPress users.
308
	 *
309
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
310
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
311
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
312
	 * @param array $args       Arguments.
313
	 * @param array $assoc_args Associative arguments.
314
	 */
315
	public function wp_cli_customers_connect_wp_users( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

315
	public function wp_cli_customers_connect_wp_users( /** @scrutinizer ignore-unused */ $args, $assoc_args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $assoc_args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

315
	public function wp_cli_customers_connect_wp_users( $args, /** @scrutinizer ignore-unused */ $assoc_args ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
316
		global $wpdb;
317
318
		$query = "
319
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
320
				customer_id,
321
				user_id
322
			)
323
			SELECT
324
				mollie_customer.id AS mollie_customer_id,
325
				wp_user.ID AS wp_user_id
326
			FROM
327
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
328
					INNER JOIN
329
				$wpdb->users AS wp_user
330
						ON mollie_customer.email = wp_user.user_email
331
			;
332
		";
333
334
		$result = $wpdb->query( $query );
335
336
		if ( false === $result ) {
337
			\WP_CLI::error(
338
				sprintf(
339
					'Database error: %s.',
340
					$wpdb->last_error
341
				)
342
			);
343
		}
344
345
		\WP_CLI::log(
346
			sprintf(
347
				'Connected %d users and Mollie customers.',
348
				$result
349
			)
350
		);
351
	}
352
}
353