Test Failed
Push — develop ( 9cf97e...8028c8 )
by Reüel
04:13
created

CLI   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 94
dl 0
loc 199
rs 10
c 2
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
C wp_cli_customers_synchronize() 0 103 10
A wp_cli_organizations_synchronize() 0 2 1
A wp_cli_customers_connect_wp_users() 0 35 2
A __construct() 0 25 1
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
		// 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 ) {
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

62
	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

62
	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...
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 ) {
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

73
	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

73
	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...
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
				'meta_query'  => array(
83
					array(
84
						'key'   => '_pronamic_gateway_id',
85
						'value' => 'mollie',
86
					),
87
					array(
88
						'key'     => '_pronamic_gateway_mollie_api_key',
89
						'compare' => 'EXISTS',
90
					),
91
				),
92
			)
93
		);
94
95
		if ( $query->have_posts() ) {
96
			while ( $query->have_posts() ) {
97
				$query->the_post();
98
99
				$api_key = get_post_meta( $post->ID, '_pronamic_gateway_mollie_api_key', true );
100
101
				\WP_CLI::log( $post->post_title );
102
				\WP_CLI::log( $api_key );
103
				\WP_CLI::log( '' );
104
105
				$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

105
				$client = new Client( /** @scrutinizer ignore-type */ $api_key );
Loading history...
106
107
				$urls = array(
108
					'https://api.mollie.com/v2/customers?limit=250',
109
				);
110
111
				$profile = Profile::from_object( $client->get_current_profile() );
112
113
				$profile_id = $this->profile_data_store->save_profile(
114
					$profile,
115
					array(
116
						'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; 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

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

142
						/** @scrutinizer ignore-call */ 
143
      \WP_CLI\Utils\format_items(
Loading history...
143
							'table',
144
							$response->_embedded->customers,
145
							array(
146
								'id',
147
								'mode',
148
								'name',
149
								'email',
150
								'locale',
151
							)
152
						);
153
154
						foreach ( $response->_embedded->customers as $object ) {
155
							$customer = Customer::from_object( $object );
156
157
							$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...
158
								$customer,
159
								array(
160
									'profile_id' => $profile_id,
161
								),
162
								array(
163
									'profile_id' => '%d',
164
								)
165
							);
166
						}
167
					}
168
169
					if ( isset( $response->_links->next->href ) ) {
170
						$urls[] = $response->_links->next->href;
171
					}
172
				}
173
			}
174
175
			\wp_reset_postdata();
176
		}
177
	}
178
179
	/**
180
	 * CLI connect Mollie customers to WordPress users.
181
	 *
182
	 * @link https://docs.mollie.com/reference/v2/customers-api/list-customers
183
	 * @link https://make.wordpress.org/cli/handbook/internal-api/wp-cli-add-command/
184
	 * @link https://developer.wordpress.org/reference/classes/wpdb/query/
185
	 * @param array $args       Arguments.
186
	 * @param array $assoc_args Associative arguments.
187
	 */
188
	public function wp_cli_customers_connect_wp_users( $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

188
	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...
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

188
	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...
189
		global $wpdb;
190
191
		$query = "
192
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
193
				customer_id,
194
				user_id
195
			)
196
			SELECT
197
				mollie_customer.id AS mollie_customer_id,
198
				wp_user.ID AS wp_user_id
199
			FROM
200
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
201
					INNER JOIN
202
				$wpdb->users AS wp_user
203
						ON mollie_customer.email = wp_user.user_email
204
			;
205
		";
206
207
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepare is OK.
208
		$result = $wpdb->query( $query );
209
210
		if ( false === $result ) {
211
			\WP_CLI::error(
212
				sprintf(
213
					'Database error: %s.',
214
					$wpdb->last_error
215
				)
216
			);
217
		}
218
219
		\WP_CLI::log(
220
			sprintf(
221
				'Connected %d users and Mollie customers.',
222
				$result
223
			)
224
		);
225
	}
226
}
227