Test Failed
Push — develop ( d09f54...51bd25 )
by Remco
04:20
created

CLI::wp_cli_customers_connect_wp_users()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 34
rs 9.7666
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
	 * CLI customers synchronize.
64
	 *
65
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
66
	 * @param array $args       Arguments.
67
	 * @param array $assoc_args Associative arguments.
68
	 */
69
	public function wp_cli_customers_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

69
	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...
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

69
	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...
70
		\WP_CLI::error( 'Command not implemented yet.' );
71
	}
72
73
	/**
74
	 * CLI connect Mollie customers to WordPress users.
75
	 *
76
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
77
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
78
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
79
	 * @param array $args       Arguments.
80
	 * @param array $assoc_args Associative arguments.
81
	 */
82
	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

82
	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

82
	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...
83
		global $wpdb;
84
85
		$query = "
86
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
87
				customer_id,
88
				user_id
89
			)
90
			SELECT
91
				mollie_customer.id AS mollie_customer_id,
92
				wp_user.ID AS wp_user_id
93
			FROM
94
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
95
					INNER JOIN
96
				$wpdb->users AS wp_user
97
						ON mollie_customer.email = wp_user.user_email
98
			;
99
		";
100
101
		$result = $wpdb->query( $query );
102
103
		if ( false === $result ) {
104
			\WP_CLI::error(
105
				sprintf(
106
					'Database error: %s.',
107
					$wpdb->last_error
108
				)
109
			);
110
		}
111
112
		\WP_CLI::log(
113
			sprintf(
114
				'Connected %d users and Mollie customers.',
115
				$result
116
			)
117
		);
118
	}
119
}
120