Failed Conditions
Push — master ( faa7df...d58e58 )
by Reüel
04:49 queued 11s
created

CLI::wp_cli_customers_connect_wp_users()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

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 35
ccs 0
cts 30
cp 0
crap 6
rs 9.7666
1
<?php
2
/**
3
 * CLI
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 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-2021 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.1.0
21
 * @since   2.1.0
22
 * @link    https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-cli.php
23
 */
24
class CLI {
25
	/**
26
	 * Customer data store.
27
	 *
28
	 * @var CustomerDataStore
29
	 */
30
	protected $customer_data_store;
31
32
	/**
33
	 * Profile data store.
34
	 *
35
	 * @var ProfileDataStore
36
	 */
37
	protected $profile_data_store;
38
39
	/**
40
	 * Construct CLI.
41
	 */
42
	public function __construct() {
43
		\WP_CLI::add_command(
44
			'pronamic-pay mollie organizations synchronize',
45
			function( $args, $assoc_args ) {
46
				$this->wp_cli_organizations_synchronize( $args, $assoc_args );
47
			},
48
			array(
49
				'shortdesc' => 'Synchronize Mollie organizations to WordPress (not implemented yet).',
50
			)
51
		);
52
53
		\WP_CLI::add_command(
54
			'pronamic-pay mollie customers synchronize',
55
			function( $args, $assoc_args ) {
56
				$this->wp_cli_customers_synchronize( $args, $assoc_args );
57
			},
58
			array(
59
				'shortdesc' => 'Synchronize Mollie customers to WordPress.',
60
			)
61
		);
62
63
		\WP_CLI::add_command(
64
			'pronamic-pay mollie customers connect-wp-users',
65
			function( $args, $assoc_args ) {
66
				$this->wp_cli_customers_connect_wp_users( $args, $assoc_args );
67
			},
68
			array(
69
				'shortdesc' => 'Connect Mollie customers to WordPress users by email.',
70
			)
71
		);
72
73
		// Data Stores.
74
		$this->profile_data_store  = new ProfileDataStore();
75
		$this->customer_data_store = new CustomerDataStore();
76
	}
77
78
	/**
79
	 * CLI organizations synchronize.
80
	 *
81
	 * @link https://docs.mollie.com/reference/v2/organizations-api/current-organization
82
	 * @param array<string> $args       Arguments.
83
	 * @param array<string> $assoc_args Associative arguments.
84
	 * @return void
85
	 */
86
	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

86
	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

86
	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...
87
		\WP_CLI::error( 'Command not implemented yet.' );
88
	}
89
90
	/**
91
	 * CLI customers synchronize.
92
	 *
93
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
94
	 * @param array<string> $args       Arguments.
95
	 * @param array<string> $assoc_args Associative arguments.
96
	 * @return void
97
	 */
98
	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

98
	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

98
	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...
99
		global $post;
100
		global $wpdb;
101
102
		$query = new \WP_Query(
103
			array(
104
				'post_type'   => 'pronamic_gateway',
105
				'post_status' => 'publish',
106
				'nopaging'    => true,
107
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Slow query allowed on CLI.
108
				'meta_query'  => array(
109
					array(
110
						'key'   => '_pronamic_gateway_id',
111
						'value' => 'mollie',
112
					),
113
					array(
114
						'key'     => '_pronamic_gateway_mollie_api_key',
115
						'compare' => 'EXISTS',
116
					),
117
				),
118
			)
119
		);
120
121
		if ( $query->have_posts() ) {
122
			while ( $query->have_posts() ) {
123
				$query->the_post();
124
125
				$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true );
126
127
				\WP_CLI::log( $post->post_title );
128
				\WP_CLI::log( $api_key );
0 ignored issues
show
Bug introduced by
It seems like $api_key can also be of type false; however, parameter $message of WP_CLI::log() 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

128
				\WP_CLI::log( /** @scrutinizer ignore-type */ $api_key );
Loading history...
129
				\WP_CLI::log( '' );
130
131
				$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

131
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
132
133
				$urls = array(
134
					'https://api.mollie.com/v2/customers?limit=250',
135
				);
136
137
				$profile = Profile::from_object( $client->get_current_profile() );
138
139
				$profile_id = $this->profile_data_store->save_profile(
140
					$profile,
141
					array(
142
						'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 and null; 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

142
						'api_key_live' => ( 'live_' === substr( /** @scrutinizer ignore-type */ $api_key, 0, 5 ) ) ? $api_key : null,
Loading history...
143
						'api_key_test' => ( 'test_' === substr( $api_key, 0, 5 ) ) ? $api_key : null,
144
					),
145
					array(
146
						'api_key_live' => '%s',
147
						'api_key_test' => '%s',
148
					)
149
				);
150
151
				while ( ! empty( $urls ) ) {
152
					$url = (string) array_shift( $urls );
153
154
					\WP_CLI::log( $url );
155
156
					$response = $client->send_request( $url );
157
158
					if ( isset( $response->count ) ) {
159
						\WP_CLI::log(
160
							\sprintf(
161
								'Found %d customer(s).',
162
								$response->count
163
							)
164
						);
165
					}
166
167
					if ( \property_exists( $response, '_embedded' ) && isset( $response->_embedded->customers ) ) {
168
						\WP_CLI\Utils\format_items(
169
							'table',
170
							$response->_embedded->customers,
171
							array(
172
								'id',
173
								'mode',
174
								'name',
175
								'email',
176
								'locale',
177
							)
178
						);
179
180
						foreach ( $response->_embedded->customers as $object ) {
181
							$customer = Customer::from_object( $object );
182
183
							$customer_id = $this->customer_data_store->save_customer(
0 ignored issues
show
Unused Code introduced by
The assignment to $customer_id is dead and can be removed.
Loading history...
184
								$customer,
185
								array(
186
									'profile_id' => $profile_id,
187
								),
188
								array(
189
									'profile_id' => '%d',
190
								)
191
							);
192
						}
193
					}
194
195
					if ( \property_exists( $response, '_links' ) && isset( $response->_links->next->href ) ) {
196
						$urls[] = $response->_links->next->href;
197
					}
198
				}
199
			}
200
201
			\wp_reset_postdata();
202
		}
203
	}
204
205
	/**
206
	 * CLI connect Mollie customers to WordPress users.
207
	 *
208
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
209
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
210
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
211
	 * @param array<string> $args       Arguments.
212
	 * @param array<string> $assoc_args Associative arguments.
213
	 * @return void
214
	 */
215
	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

215
	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

215
	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...
216
		global $wpdb;
217
218
		$query = "
219
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
220
				customer_id,
221
				user_id
222
			)
223
			SELECT
224
				mollie_customer.id AS mollie_customer_id,
225
				wp_user.ID AS wp_user_id
226
			FROM
227
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
228
					INNER JOIN
229
				$wpdb->users AS wp_user
230
						ON mollie_customer.email = wp_user.user_email
231
			;
232
		";
233
234
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepare is OK.
235
		$result = $wpdb->query( $query );
236
237
		if ( false === $result ) {
238
			\WP_CLI::error(
239
				sprintf(
240
					'Database error: %s.',
241
					$wpdb->last_error
242
				)
243
			);
244
		}
245
246
		\WP_CLI::log(
247
			sprintf(
248
				'Connected %d users and Mollie customers.',
249
				$result
250
			)
251
		);
252
	}
253
}
254