Test Failed
Push — develop ( d09f54...51bd25 )
by Remco
04:20
created

Upgrade300::convert_user_meta()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 67
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 67
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.0' );
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
62
				PRIMARY KEY  ( id ),
63
				UNIQUE KEY mollie_id ( mollie_id )
64
			) $charset_collate;
65
66
			CREATE TABLE $wpdb->pronamic_pay_mollie_profiles (
67
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
68
				mollie_id VARCHAR( 16 ) NOT NULL,
69
				organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,
70
				name VARCHAR( 128 ) DEFAULT NULL,
71
				email VARCHAR( 100 ) DEFAULT NULL,
72
73
				PRIMARY KEY  ( id ),
74
				UNIQUE KEY mollie_id ( mollie_id ),
75
				KEY organization_id ( organization_id )
76
			) $charset_collate;
77
78
			CREATE TABLE $wpdb->pronamic_pay_mollie_customers (
79
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
80
				mollie_id VARCHAR( 16 ) NOT NULL,
81
				organization_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,
82
				profile_id BIGINT( 20 ) UNSIGNED DEFAULT NULL,
83
				test_mode BOOL NOT NULL,
84
				email VARCHAR( 100 ) DEFAULT NULL,
85
86
				PRIMARY KEY  ( id ),
87
				UNIQUE KEY mollie_id ( mollie_id ),
88
				KEY organization_id ( organization_id ),
89
				KEY profile_id ( profile_id ),
90
				KEY test_mode ( test_mode ),
91
				KEY email ( email )
92
			) $charset_collate;
93
94
			CREATE TABLE $wpdb->pronamic_pay_mollie_customer_users (
95
				id BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
96
				customer_id BIGINT( 20 ) UNSIGNED NOT NULL,
97
				user_id BIGINT( 20 ) UNSIGNED NOT NULL,
98
99
				PRIMARY KEY  ( id ),
100
				UNIQUE KEY customer_user ( customer_id, user_id )
101
			) $charset_collate;
102
		";
103
104
		/**
105
		 * Execute.
106
		 */
107
		\dbDelta( $queries );
108
109
		/**
110
		 * Foreign keys.
111
		 *
112
		 * @link https://core.trac.wordpress.org/ticket/19207
113
		 * @link https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
114
		 */
115
		$queries = array();
116
117
		$queries['fk_profile_organization_id'] = "
118
			ALTER TABLE $wpdb->pronamic_pay_mollie_profiles
119
			ADD CONSTRAINT fk_profile_organization_id
120
			FOREIGN KEY ( organization_id )
121
			REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )
122
			ON DELETE RESTRICT
123
			ON UPDATE RESTRICT
124
			;
125
		";
126
127
		$queries['fk_customer_organization_id'] = "
128
			ALTER TABLE $wpdb->pronamic_pay_mollie_customers
129
			ADD CONSTRAINT fk_customer_organization_id
130
			FOREIGN KEY ( organization_id )
131
			REFERENCES $wpdb->pronamic_pay_mollie_organizations ( id )
132
			ON DELETE RESTRICT
133
			ON UPDATE RESTRICT
134
			;
135
		";
136
137
		$queries['fk_customer_profile_id'] = "
138
			ALTER TABLE $wpdb->pronamic_pay_mollie_customers
139
			ADD CONSTRAINT fk_customer_profile_id
140
			FOREIGN KEY ( profile_id )
141
			REFERENCES $wpdb->pronamic_pay_mollie_profiles ( id )
142
			ON DELETE RESTRICT
143
			ON UPDATE RESTRICT
144
			;
145
		";
146
147
		$queries['fk_customer_id'] = "
148
			ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users
149
			ADD CONSTRAINT fk_customer_id
150
			FOREIGN KEY customer_id ( customer_id )
151
			REFERENCES $wpdb->pronamic_pay_mollie_customers ( id )
152
			ON DELETE RESTRICT
153
			ON UPDATE RESTRICT
154
			;
155
		";
156
157
		$queries['fk_customer_user_id'] = "
158
			ALTER TABLE $wpdb->pronamic_pay_mollie_customer_users
159
			ADD CONSTRAINT fk_customer_user_id
160
			FOREIGN KEY user_id ( user_id )
161
			REFERENCES $wpdb->users ( id )
162
			ON DELETE CASCADE
163
			ON UPDATE CASCADE
164
			;
165
		";
166
167
		foreach ( $queries as $index_name => $query ) {
168
			$result = $wpdb->query( $query );
169
170
			if ( false === $result ) {
171
				throw new \Exception(
172
					\sprintf(
173
						'Could not add foreign key: %s, database error: %s.',
174
						$index_name,
175
						$wpdb->last_error
176
					)
177
				);
178
			}
179
		}
180
181
		/**
182
		 * Convert user meta.
183
		 */
184
		$this->convert_user_meta();
185
	}
186
187
	/**
188
	 * Convert user meta.
189
	 *
190
	 * @throws \Exception Throws exception when database update query fails.
191
	 */
192
	private function convert_user_meta() {
193
		global $wpdb;
194
195
		$query = "
196
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customers (
197
				mollie_id,
198
				test_mode
199
			)
200
			SELECT
201
				meta_value AS mollie_id,
202
				'_pronamic_pay_mollie_customer_id_test' = meta_key AS test_mode
203
			FROM
204
				$wpdb->usermeta
205
			WHERE
206
				meta_key IN (
207
					'_pronamic_pay_mollie_customer_id',
208
					'_pronamic_pay_mollie_customer_id_test'
209
				)
210
					AND
211
				meta_value != ''
212
			;
213
		";
214
215
		$result = $wpdb->query( $query );
216
217
		if ( false === $result ) {
218
			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...
219
				sprintf(
220
					'Could not convert user meta, database error: %s.',
221
					$wpdb->last_error
222
				)
223
			);
224
		}
225
226
		$query = "
227
			INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users (
228
				customer_id,
229
				user_id
230
			)
231
			SELECT
232
				mollie_customer.id AS mollie_customer_id,
233
				wp_user.ID AS wp_user_id
234
			FROM
235
				$wpdb->pronamic_pay_mollie_customers AS mollie_customer
236
					INNER JOIN
237
				$wpdb->usermeta AS wp_user_meta
238
						ON wp_user_meta.meta_value = mollie_customer.mollie_id
239
					INNER JOIN
240
				$wpdb->users AS wp_user
241
						ON wp_user_meta.user_id = wp_user.ID
242
			WHERE
243
				wp_user_meta.meta_key IN (
244
					'_pronamic_pay_mollie_customer_id',
245
					'_pronamic_pay_mollie_customer_id_test'
246
				)
247
					AND
248
				wp_user_meta.meta_value != ''
249
			;
250
		";
251
252
		$result = $wpdb->query( $query );
253
254
		if ( false === $result ) {
255
			throw new Exception(
256
				sprintf(
257
					'Could not convert user meta, database error: %s.',
258
					$wpdb->last_error
259
				)
260
			);
261
		}
262
	}
263
}
264