Test Failed
Push — develop ( 8028c8...517b56 )
by Reüel
03:22
created

Upgrade300::convert_user_meta()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 69
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 69
rs 9.584

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Upgrade 3.0.0
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use Pronamic\WordPress\Pay\Upgrades\Upgrade;
14
15
/**
16
 * Upgrade 3.0.0
17
 *
18
 * @author  Remco Tolsma
19
 * @version 3.0.0
20
 * @since   3.0.0
21
 */
22
class Upgrade300 extends Upgrade {
23
	/**
24
	 * Construct 3.0.0 upgrade.
25
	 */
26
	public function __construct() {
27
		parent::__construct( '3.0.3' );
28
	}
29
30
	/**
31
	 * Execute.
32
	 *
33
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L992-L1072
34
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/schema.php#L25-L344
35
	 * @link https://developer.wordpress.org/reference/functions/dbdelta/
36
	 * @link https://github.com/wp-premium/gravityforms/blob/2.4.16/includes/class-gf-upgrade.php#L518-L531
37
	 * @throws \Exception Throws exception when database update query fails.
38
	 */
39
	public function execute() {
40
		global $wpdb;
41
42
		/**
43
		 * Requirements.
44
		 */
45
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
46
47
		/**
48
		 * Other.
49
		 */
50
		$charset_collate = $wpdb->get_charset_collate();
51
52
		/**
53
		 * Queries.
54
		 */
55
		$queries = "
56
			CREATE TABLE $wpdb->pronamic_pay_mollie_organizations (
57
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
58
				mollie_id VARCHAR( 16 ) NOT NULL,
59
				name VARCHAR( 128 ) DEFAULT NULL,
60
				email VARCHAR( 100 ) DEFAULT NULL,
61
				PRIMARY KEY  ( id ),
62
				UNIQUE KEY mollie_id ( mollie_id )
63
			) $charset_collate;
64
			CREATE TABLE $wpdb->pronamic_pay_mollie_profiles (
65
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
66
				mollie_id VARCHAR( 16 ) NOT NULL,
67
				organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,
68
				name VARCHAR( 128 ) DEFAULT NULL,
69
				email VARCHAR( 100 ) DEFAULT NULL,
70
				api_key_test VARCHAR( 35 ) DEFAULT NULL,
71
				api_key_live VARCHAR( 35 ) DEFAULT NULL,
72
				PRIMARY KEY  ( id ),
73
				UNIQUE KEY mollie_id ( mollie_id ),
74
				KEY organization_id ( organization_id )
75
			) $charset_collate;
76
			CREATE TABLE $wpdb->pronamic_pay_mollie_customers (
77
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
78
				mollie_id VARCHAR( 16 ) NOT NULL,
79
				organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,
80
				profile_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,
81
				test_mode BOOL NOT NULL,
82
				email VARCHAR( 100 ) DEFAULT NULL,
83
				name VARCHAR( 255 ) DEFAULT NULL,
84
				PRIMARY KEY  ( id ),
85
				UNIQUE KEY mollie_id ( mollie_id ),
86
				KEY organization_id ( organization_id ),
87
				KEY profile_id ( profile_id ),
88
				KEY test_mode ( test_mode ),
89
				KEY email ( email )
90
			) $charset_collate;
91
			CREATE TABLE $wpdb->pronamic_pay_mollie_customer_users (
92
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
93
				customer_id BIGINT( 20 ) UNSIGNED NOT NULL,
94
				user_id BIGINT( 20 ) UNSIGNED NOT NULL,
95
				PRIMARY KEY  ( id ),
96
				UNIQUE KEY customer_user ( customer_id, user_id )
97
			) $charset_collate;
98
		";
99
100
		/**
101
		 * Execute.
102
		 *
103
		 * @link https://developer.wordpress.org/reference/functions/dbdelta/
104
		 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-admin/includes/upgrade.php#L2538-L2915
105
		 */
106
		\dbDelta( $queries );
107
108
		/**
109
		 * Foreign keys.
110
		 *
111
		 * @link https://core.trac.wordpress.org/ticket/19207
112
		 * @link https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
113
		 */
114
		$data = array(
115
			(object) array(
116
				'table' => $wpdb->pronamic_pay_mollie_profiles,
117
				'name'  => 'fk_profile_organization_id',
118
				'query' => "
119
					ALTER TABLE  $wpdb->pronamic_pay_mollie_profiles
120
					ADD CONSTRAINT fk_profile_organization_id
121
					FOREIGN KEY ( organization_id )
122
					REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )
123
					ON DELETE RESTRICT
124
					ON UPDATE RESTRICT
125
					;
126
				",
127
			),
128
			(object) array(
129
				'table' => $wpdb->pronamic_pay_mollie_customers,
130
				'name'  => 'fk_customer_organization_id',
131
				'query' => "
132
					ALTER TABLE $wpdb->pronamic_pay_mollie_customers
133
					ADD CONSTRAINT fk_customer_organization_id
134
					FOREIGN KEY ( organization_id )
135
					REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )
136
					ON DELETE RESTRICT
137
					ON UPDATE RESTRICT
138
					;
139
				",
140
			),
141
			(object) array(
142
				'table' => $wpdb->pronamic_pay_mollie_customers,
143
				'name'  => 'fk_customer_profile_id',
144
				'query' => "
145
					ALTER TABLE $wpdb->pronamic_pay_mollie_customers
146
					ADD CONSTRAINT fk_customer_profile_id
147
					FOREIGN KEY ( profile_id )
148
					REFERENCES $wpdb->pronamic_pay_mollie_profiles ( id )
149
					ON DELETE RESTRICT
150
					ON UPDATE RESTRICT
151
					;
152
				",
153
			),
154
			(object) array(
155
				'table' => $wpdb->pronamic_pay_mollie_customer_users,
156
				'name'  => 'fk_customer_id',
157
				'query' => "
158
					ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users
159
					ADD CONSTRAINT fk_customer_id
160
					FOREIGN KEY customer_id ( customer_id )
161
					REFERENCES $wpdb->pronamic_pay_mollie_customers ( id )
162
					ON DELETE RESTRICT
163
					ON UPDATE RESTRICT
164
					;
165
				",
166
			),
167
			(object) array(
168
				'table' => $wpdb->pronamic_pay_mollie_customer_users,
169
				'name'  => 'fk_customer_user_id',
170
				'query' => "
171
					ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users
172
					ADD CONSTRAINT fk_customer_user_id
173
					FOREIGN KEY user_id ( user_id )
174
					REFERENCES $wpdb->users ( id )
175
					ON DELETE CASCADE
176
					ON UPDATE CASCADE
177
					;
178
				",
179
			),
180
		);
181
182
		foreach ( $data as $item ) {
183
			/**
184
			 * Check if foreign key exists
185
			 *
186
			 * @link https://github.com/woocommerce/woocommerce/blob/3.9.0/includes/class-wc-install.php#L663-L681
187
			 */
188
			$result = $wpdb->get_var(
189
				$wpdb->prepare(
190
					"
191
				SELECT COUNT(*)
192
				FROM information_schema.TABLE_CONSTRAINTS
193
				WHERE CONSTRAINT_SCHEMA = %s
194
				AND CONSTRAINT_NAME = %s
195
				AND CONSTRAINT_TYPE = 'FOREIGN KEY'
196
				AND TABLE_NAME = %s
197
				",
198
					$wpdb->dbname,
199
					$item->name,
200
					$item->table
201
				)
202
			);
203
204
			if ( null === $result ) {
205
				throw new \Exception(
206
					\sprintf(
207
						'Could not count foreign keys: %s, database error: %s.',
208
						$$item->name,
209
						$wpdb->last_error
210
					)
211
				);
212
			}
213
214
			$number_constraints = \intval( $result );
215
216
			if ( 0 === $number_constraints ) {
217
				// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.
218
				$result = $wpdb->query( $item->query );
219
220
				if ( false === $result ) {
221
					throw new \Exception(
222
						\sprintf(
223
							'Could not add foreign key: %s, database error: %s.',
224
							$item->name,
225
							$wpdb->last_error
226
						)
227
					);
228
				}
229
			}
230
		}
231
232
		/**
233
		 * Convert user meta.
234
		 */
235
		$this->convert_user_meta();
236
	}
237
238
	/**
239
	 * Convert user meta.
240
	 *
241
	 * @throws \Exception Throws exception when database update query fails.
242
	 */
243
	private function convert_user_meta() {
244
		global $wpdb;
245
246
		$query = "
247
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customers (
248
				mollie_id,
249
				test_mode
250
			)
251
			SELECT
252
				meta_value AS mollie_id,
253
				'_pronamic_pay_mollie_customer_id_test' = meta_key AS test_mode
254
			FROM
255
				$wpdb->usermeta
256
			WHERE
257
				meta_key IN (
258
					'_pronamic_pay_mollie_customer_id',
259
					'_pronamic_pay_mollie_customer_id_test'
260
				)
261
					AND
262
				meta_value != ''
263
			;
264
		";
265
266
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.
267
		$result = $wpdb->query( $query );
268
269
		if ( false === $result ) {
270
			throw new Exception(
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Gateways\Mollie\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
271
				sprintf(
272
					'Could not convert user meta, database error: %s.',
273
					$wpdb->last_error
274
				)
275
			);
276
		}
277
278
		$query = "
279
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
280
				customer_id,
281
				user_id
282
			)
283
			SELECT
284
				mollie_customer.id AS mollie_customer_id,
285
				wp_user.ID AS wp_user_id
286
			FROM
287
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
288
					INNER JOIN
289
				$wpdb->usermeta AS wp_user_meta
290
						ON wp_user_meta.meta_value = mollie_customer.mollie_id
291
					INNER JOIN
292
				$wpdb->users AS wp_user
293
						ON wp_user_meta.user_id = wp_user.ID
294
			WHERE
295
				wp_user_meta.meta_key IN (
296
					'_pronamic_pay_mollie_customer_id',
297
					'_pronamic_pay_mollie_customer_id_test'
298
				)
299
					AND
300
				wp_user_meta.meta_value != ''
301
			;
302
		";
303
304
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared.
305
		$result = $wpdb->query( $query );
306
307
		if ( false === $result ) {
308
			throw new Exception(
309
				sprintf(
310
					'Could not convert user meta, database error: %s.',
311
					$wpdb->last_error
312
				)
313
			);
314
		}
315
	}
316
}
317