Failed Conditions
Push — develop ( 8c2bc3...fa93a4 )
by Remco
06:45 queued 02:49
created

src/CLI.php (6 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 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
	 * 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
		// Data Stores.
51
		$this->profile_data_store  = new ProfileDataStore();
0 ignored issues
show
Bug Best Practice introduced by
The property profile_data_store does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
		$this->customer_data_store = new CustomerDataStore();
0 ignored issues
show
Bug Best Practice introduced by
The property customer_data_store does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
	}
54
55
	/**
56
	 * CLI organizations synchronize.
57
	 *
58
	 * @link https://docs.mollie.com/reference/v2/organizations-api/current-organization
59
	 * @param array $args       Arguments.
60
	 * @param array $assoc_args Associative arguments.
61
	 */
62
	public function wp_cli_organizations_synchronize( $args, $assoc_args ) {
63
		\WP_CLI::error( 'Command not implemented yet.' );
64
	}
65
66
	/**
67
	 * CLI customers synchronize.
68
	 *
69
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
70
	 * @param array $args       Arguments.
71
	 * @param array $assoc_args Associative arguments.
72
	 */
73
	public function wp_cli_customers_synchronize( $args, $assoc_args ) {
74
		global $post;
75
		global $wpdb;
76
77
		$query = new \WP_Query(
78
			array(
79
				'post_type'   => 'pronamic_gateway',
80
				'post_status' => 'publish',
81
				'nopaging'    => true,
82
				// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Slow query allowed on CLI.
83
				'meta_query'  => array(
84
					array(
85
						'key'   => '_pronamic_gateway_id',
86
						'value' => 'mollie',
87
					),
88
					array(
89
						'key'     => '_pronamic_gateway_mollie_api_key',
90
						'compare' => 'EXISTS',
91
					),
92
				),
93
			)
94
		);
95
96
		if ( $query->have_posts() ) {
97
			while ( $query->have_posts() ) {
98
				$query->the_post();
99
100
				$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true );
101
102
				\WP_CLI::log( $post->post_title );
103
				\WP_CLI::log( $api_key );
104
				\WP_CLI::log( '' );
105
106
				$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

106
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
107
108
				$urls = array(
109
					'https://api.mollie.com/v2/customers?limit=250',
110
				);
111
112
				$profile = Profile::from_object( $client->get_current_profile() );
113
114
				$profile_id = $this->profile_data_store->save_profile(
115
					$profile,
116
					array(
117
						'api_key_live' => ( 'live_' === substr( $api_key, 0, 5 ) ) ? $api_key : null,
0 ignored issues
show
It seems like $api_key can also be of type false; 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

117
						'api_key_live' => ( 'live_' === substr( /** @scrutinizer ignore-type */ $api_key, 0, 5 ) ) ? $api_key : null,
Loading history...
118
						'api_key_test' => ( 'test_' === substr( $api_key, 0, 5 ) ) ? $api_key : null,
119
					),
120
					array(
121
						'api_key_live' => '%s',
122
						'api_key_test' => '%s',
123
					)
124
				);
125
126
				while ( ! empty( $urls ) ) {
127
					$url = array_shift( $urls );
128
129
					\WP_CLI::log( $url );
130
131
					$response = $client->send_request( $url );
132
133
					if ( isset( $response->count ) ) {
134
						\WP_CLI::log(
135
							\sprintf(
136
								'Found %d customer(s).',
137
								$response->count
138
							)
139
						);
140
					}
141
142
					if ( isset( $response->_embedded->customers ) ) {
143
						\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

143
						/** @scrutinizer ignore-call */ 
144
      \WP_CLI\Utils\format_items(
Loading history...
144
							'table',
145
							$response->_embedded->customers,
146
							array(
147
								'id',
148
								'mode',
149
								'name',
150
								'email',
151
								'locale',
152
							)
153
						);
154
155
						foreach ( $response->_embedded->customers as $object ) {
156
							$customer = Customer::from_object( $object );
157
158
							$customer_id = $this->customer_data_store->save_customer(
159
								$customer,
160
								array(
161
									'profile_id' => $profile_id,
162
								),
163
								array(
164
									'profile_id' => '%d',
165
								)
166
							);
167
						}
168
					}
169
170
					if ( isset( $response->_links->next->href ) ) {
171
						$urls[] = $response->_links->next->href;
172
					}
173
				}
174
			}
175
176
			\wp_reset_postdata();
177
		}
178
	}
179
180
	/**
181
	 * CLI connect Mollie customers to WordPress users.
182
	 *
183
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
184
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
185
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
186
	 * @param array $args       Arguments.
187
	 * @param array $assoc_args Associative arguments.
188
	 */
189
	public function wp_cli_customers_connect_wp_users( $args, $assoc_args ) {
190
		global $wpdb;
191
192
		$query = "
193
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
194
				customer_id,
195
				user_id
196
			)
197
			SELECT
198
				mollie_customer.id AS mollie_customer_id,
199
				wp_user.ID AS wp_user_id
200
			FROM
201
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
202
					INNER JOIN
203
				$wpdb->users AS wp_user
204
						ON mollie_customer.email = wp_user.user_email
205
			;
206
		";
207
208
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepare is OK.
209
		$result = $wpdb->query( $query );
210
211
		if ( false === $result ) {
212
			\WP_CLI::error(
213
				sprintf(
214
					'Database error: %s.',
215
					$wpdb->last_error
216
				)
217
			);
218
		}
219
220
		\WP_CLI::log(
221
			sprintf(
222
				'Connected %d users and Mollie customers.',
223
				$result
224
			)
225
		);
226
	}
227
}
228