Test Failed
Push — develop ( 4a14d2...eae840 )
by Remco
04:45
created

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

101
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
102
103
				$urls = array(
104
					'https://api.mollie.com/v2/customers?limit=250',
105
				);
106
107
				while ( ! empty( $urls ) ) {
108
					$url = array_shift( $urls );
109
110
					\WP_CLI::log( $url );
111
112
					$response = $client->send_request( $url );
113
114
					if ( isset( $response->count ) ) {
115
						\WP_CLI::log(
116
							\sprintf(
117
								'Found %d customer(s).',
118
								$response->count
119
							)
120
						);
121
					}
122
123
					if ( isset( $response->_embedded->customers ) ) {
124
						\WP_CLI\Utils\format_items(
0 ignored issues
show
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

124
						/** @scrutinizer ignore-call */ 
125
      \WP_CLI\Utils\format_items(
Loading history...
125
							'table',
126
							$response->_embedded->customers,
127
							array(
128
								'id',
129
								'mode',
130
								'name',
131
								'email',
132
								'locale',
133
							)
134
						);
135
136
						foreach ( $response->_embedded->customers as $object ) {
137
							$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->pronamic_pay_mollie_customers WHERE mollie_id = %s", $object->id ) );
138
139
							$data = array(
140
								'test_mode' => ( 'test' === $object->mode ),
141
								'email'     => $object->email,
142
								'name'      => $object->name,
143
							);
144
145
							$format = array(
146
								'test_mode' => '%d',
147
								'email'     => '%s',
148
								'name'      => '%s',
149
							);
150
151
							if ( null === $id ) {
152
								$data['mollie_id']   = $object->id;
153
								$foramt['mollie_id'] = '%s';
154
155
								$result = $wpdb->insert(
156
									$wpdb->pronamic_pay_mollie_customers,
157
									$data,
158
									$format
159
								);
160
161
								if ( false === $result ) {
162
									\WP_CLI::error(
163
										sprintf(
164
											'Database error: %s.',
165
											$wpdb->last_error
166
										)
167
									);
168
								}
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
								$id = $wpdb->insert_id;
192
							}
193
						}
194
					}
195
196
					if ( isset( $response->_links->next->href ) ) {
197
						$urls[] = $response->_links->next->href;
198
					}
199
				}
200
			}
201
202
			\wp_reset_postdata();
203
		}
204
205
		\WP_CLI::error( 'Command not fully implemented yet.' );
206
	}
207
208
	/**
209
	 * CLI connect Mollie customers to WordPress users.
210
	 *
211
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
212
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
213
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
214
	 * @param array $args       Arguments.
215
	 * @param array $assoc_args Associative arguments.
216
	 */
217
	public function wp_cli_customers_connect_wp_users( $args, $assoc_args ) {
218
		global $wpdb;
219
220
		$query = "
221
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
222
				customer_id,
223
				user_id
224
			)
225
			SELECT
226
				mollie_customer.id AS mollie_customer_id,
227
				wp_user.ID AS wp_user_id
228
			FROM
229
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
230
					INNER JOIN
231
				$wpdb->users AS wp_user
232
						ON mollie_customer.email = wp_user.user_email
233
			;
234
		";
235
236
		$result = $wpdb->query( $query );
237
238
		if ( false === $result ) {
239
			\WP_CLI::error(
240
				sprintf(
241
					'Database error: %s.',
242
					$wpdb->last_error
243
				)
244
			);
245
		}
246
247
		\WP_CLI::log(
248
			sprintf(
249
				'Connected %d users and Mollie customers.',
250
				$result
251
			)
252
		);
253
	}
254
}
255