Test Failed
Push — develop ( 13fc2a...744f0f )
by Reüel
04:50
created

src/CLI.php (2 issues)

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
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 ) {
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 ) {
70
		global $post;
71
72
		$query = new \WP_Query( array(
73
			'post_type'   => 'pronamic_gateway',
74
			'post_status' => 'publish',
75
			'nopaging'    => true,
76
			'meta_query'  => array(
77
				array(
78
					'key'     => '_pronamic_gateway_id',
79
					'value'   => 'mollie',
80
				),
81
				array(
82
					'key'     => '_pronamic_gateway_mollie_api_key',
83
					'compare' => 'EXISTS',
84
				),
85
			)
86
		) );
87
88
		if ( $query->have_posts() ) {
89
			while ( $query->have_posts() ) {
90
				$query->the_post();
91
92
				$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true );
93
94
				\WP_CLI::log( $post->post_title );
95
				\WP_CLI::log( $api_key );
96
				\WP_CLI::log( '' );
97
98
				$client = new Client( $api_key );
0 ignored issues
show
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

98
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
99
			}
100
101
			\wp_reset_postdata();
102
		}
103
104
		\WP_CLI::error( 'Command not fully implemented yet.' );
105
	}
106
107
	/**
108
	 * CLI connect Mollie customers to WordPress users.
109
	 *
110
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
111
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
112
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
113
	 * @param array $args       Arguments.
114
	 * @param array $assoc_args Associative arguments.
115
	 */
116
	public function wp_cli_customers_connect_wp_users( $args, $assoc_args ) {
117
		global $wpdb;
118
119
		$query = "
120
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
121
				customer_id,
122
				user_id
123
			)
124
			SELECT
125
				mollie_customer.id AS mollie_customer_id,
126
				wp_user.ID AS wp_user_id
127
			FROM
128
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
129
					INNER JOIN
130
				$wpdb->users AS wp_user
131
						ON mollie_customer.email = wp_user.user_email
132
			;
133
		";
134
135
		$result = $wpdb->query( $query );
136
137
		if ( false === $result ) {
138
			\WP_CLI::error(
139
				sprintf(
140
					'Database error: %s.',
141
					$wpdb->last_error
142
				)
143
			);
144
		}
145
146
		\WP_CLI::log(
147
			sprintf(
148
				'Connected %d users and Mollie customers.',
149
				$result
150
			)
151
		);
152
	}
153
}
154