Test Failed
Push — develop ( b72058...5cbfdc )
by Reüel
04:45
created

CLI::wp_cli_customers_synchronize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 23
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 38
rs 9.552
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
		global $post;
71
72
		$query = new \WP_Query(
73
			array(
74
				'post_type'   => 'pronamic_gateway',
75
				'post_status' => 'publish',
76
				'nopaging'    => true,
77
				'meta_query'  => array(
78
					array(
79
						'key'   => '_pronamic_gateway_id',
80
						'value' => 'mollie',
81
					),
82
					array(
83
						'key'     => '_pronamic_gateway_mollie_api_key',
84
						'compare' => 'EXISTS',
85
					),
86
				),
87
			)
88
		);
89
90
		if ( $query->have_posts() ) {
91
			while ( $query->have_posts() ) {
92
				$query->the_post();
93
94
				$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true );
95
96
				\WP_CLI::log( $post->post_title );
97
				\WP_CLI::log( $api_key );
98
				\WP_CLI::log( '' );
99
100
				$client = new Client( $api_key );
0 ignored issues
show
Unused Code introduced by
The assignment to $client is dead and can be removed.
Loading history...
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

100
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
101
			}
102
103
			\wp_reset_postdata();
104
		}
105
106
		\WP_CLI::error( 'Command not fully implemented yet.' );
107
	}
108
109
	/**
110
	 * CLI connect Mollie customers to WordPress users.
111
	 *
112
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
113
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
114
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
115
	 * @param array $args       Arguments.
116
	 * @param array $assoc_args Associative arguments.
117
	 */
118
	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

118
	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

118
	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...
119
		global $wpdb;
120
121
		$query = "
122
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
123
				customer_id,
124
				user_id
125
			)
126
			SELECT
127
				mollie_customer.id AS mollie_customer_id,
128
				wp_user.ID AS wp_user_id
129
			FROM
130
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
131
					INNER JOIN
132
				$wpdb->users AS wp_user
133
						ON mollie_customer.email = wp_user.user_email
134
			;
135
		";
136
137
		$result = $wpdb->query( $query );
138
139
		if ( false === $result ) {
140
			\WP_CLI::error(
141
				sprintf(
142
					'Database error: %s.',
143
					$wpdb->last_error
144
				)
145
			);
146
		}
147
148
		\WP_CLI::log(
149
			sprintf(
150
				'Connected %d users and Mollie customers.',
151
				$result
152
			)
153
		);
154
	}
155
}
156