Test Failed
Push — master ( b86487...e5e5a2 )
by Ravinder
07:05
created

Give_Tools_Import_Donors::count()   C

Complexity

Conditions 10
Paths 4

Size

Total Lines 86
Code Lines 48

Duplication

Lines 13
Ratio 15.12 %

Importance

Changes 0
Metric Value
cc 10
eloc 48
nc 4
nop 3
dl 13
loc 86
rs 5.3068
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Delete Donors.
4
 *
5
 * This class handles batch processing of deleting donor data.
6
 *
7
 * @subpackage  Admin/Tools/Give_Tools_Delete_Donors
8
 * @copyright   Copyright (c) 2016, WordImpress
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @since       1.8.12
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
19
/**
20
 * Give_Tools_Import_Donors Class
21
 *
22
 * @since 1.8.13
23
 */
24
class Give_Tools_Import_Donors extends Give_Batch_Export {
25
26
	var $request;
27
28
	/**
29
	 * Used to store form id's that are going to get recount.
30
	 * @var array.
31
	 * @since 1.8.13
32
	 */
33
	var $form_key = 'give_temp_delete_form_ids';
34
35
	/**
36
	 * Used to store donation id's that are going to get deleted.
37
	 * @var array.
38
	 * @since 1.8.12
39
	 */
40
	var $donation_key = 'give_temp_delete_donation_ids';
41
42
	/**
43
	 * Used to store donors id's that are going to get deleted.
44
	 * @var array.
45
	 * @since 1.8.12
46
	 */
47
	var $donor_key = 'give_temp_delete_donor_ids';
48
49
	/**
50
	 * Used to store the step where the step will be. ( 'count', 'donations', 'donors' ).
51
	 * @var array.
52
	 * @since 1.8.12
53
	 */
54
	var $step_key = 'give_temp_delete_step';
55
56
	/**
57
	 * Used to store to get the page count in the loop.
58
	 * @var array.
59
	 * @since 1.8.12
60
	 */
61
	var $step_on_key = 'give_temp_delete_step_on';
62
63
	/**
64
	 * Contain total number of step .
65
	 * @var array.
66
	 * @since 1.8.12
67
	 */
68
	var $total_step;
69
70
	/**
71
	 * Counting contain total number of step that completed.
72
	 * @var array.
73
	 * @since 1.8.12
74
	 */
75
	var $step_completed;
76
77
	/**
78
	 * Our export type. Used for export-type specific filters/actions
79
	 * @var string
80
	 * @since 1.8.12
81
	 */
82
	public $export_type = '';
83
84
	/**
85
	 * Allows for a non-form batch processing to be run.
86
	 * @since  1.8.12
87
	 * @var boolean
88
	 */
89
	public $is_void = true;
90
91
	/**
92
	 * Sets the number of items to pull on each step
93
	 * @since  1.8.12
94
	 * @var integer
95
	 */
96
	public $per_step = 10;
97
98
	/**
99
	 * Set's all the donors id's
100
	 * @since  1.8.12
101
	 * @var array
102
	 */
103
	public $donor_ids = array();
104
105
	/**
106
	 * Get the Export Data
107
	 *
108
	 * @access public
109
	 * @since 1.8.12
110
	 * @global object $wpdb Used to query the database using the WordPress Database API
111
	 *
112
	 * @return array|bool $data The data for the CSV file
113
	 */
114 View Code Duplication
	public function pre_fetch() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
		$donation_ids = array();
116
		$donor_ids    = array();
117
118
		// Check if the ajax request if running for the first time.
119
		if ( 1 === (int) $this->step ) {
120
			// Delete all the form ids.
121
			$this->delete_option( $this->form_key );
122
			// Delete all the donation ids.
123
			$this->delete_option( $this->donation_key );
124
			// Delete all the donor ids.
125
			$this->delete_option( $this->donor_key );
126
127
			// Delete all the step and set to 'count' which if the first step in the process of deleting the donors.
128
			$this->update_option( $this->step_key, 'count' );
129
130
			// Delete tha page count of the step.
131
			$this->update_option( $this->step_on_key, '0' );
132
		} else {
133
			// Get the old donors list.
134
			$donor_ids = $this->get_option( $this->donor_key );
135
136
			// Get the old donation list.
137
			$donation_ids = $this->get_option( $this->donation_key );
138
		}
139
140
		// Get the step and check for it if it's on the first step( 'count' ) or not.
141
		$step = (int) $this->get_step();
142
		if ( 1 === $step ) {
143
			/**
144
			 * Will add or update the donation and donor data by running wp query.
145
			 */
146
			$this->count( $step, $donation_ids, $donor_ids );
147
		}
148
	}
149
150
	/**
151
	 * Will Update or Add the donation and donors ids in the with option table for there respected key.
152
	 *
153
	 * @param string $step On which the current ajax is running.
154
	 * @param array $donation_ids Contain the list of all the donation id's that has being add before this
155
	 * @param array $donor_ids Contain the list of all the donors id's that has being add before this
156
	 */
157
	private function count( $step, $donation_ids = array(), $donor_ids = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
159
		// Get the Page count by default it's zero.
160
		$paged = (int) $this->get_step_page();
161
		// Incresed the page count by one.
162
		++ $paged;
163
164
		/**
165
		 * Filter add to alter the argument before the wp quest run
166
		 */
167
		$args = apply_filters( 'give_tools_reset_stats_total_args', array(
168
			'post_type'      => 'give_payment',
169
			'post_status'    => 'any',
170
			'posts_per_page' => $this->per_step,
171
			'paged'          => $paged,
172
			'meta_key'       => '_give_payment_import',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
173
			'meta_value_num' => 1,
174
			'meta_compare'   => '=',
175
		) );
176
177
		// Reset the post data.
178
		wp_reset_postdata();
179
		// Getting the new donation.
180
		$donation_posts = new WP_Query( $args );
181
182
		// The Loop.
183
		if ( $donation_posts->have_posts() ) {
184
			while ( $donation_posts->have_posts() ) {
185
				$add_author = true;
186
				$donation_posts->the_post();
187
				global $post;
188
				// Add the donation id in side the array.
189
				$donation_ids[] = $post->ID;
190
191
				$donor_id = (int) get_post_meta( $post->ID, '_give_payment_customer_id', true );
192
				if ( ! empty( $donor_id ) ) {
193
					$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
194
					if ( ! empty( $donor->id ) ) {
195
						if ( empty( $donor->user_id ) && ! empty( $donor->payment_ids ) ) {
196
							$add_author = false;
197
							$count      = (int) count( $donor->payment_ids );
198
							if ( 1 === $count ) {
199
								Give()->donors->delete( $donor->id );
200
							} else {
201
								$donor->remove_payment( $post->ID );
202
								$donor->decrease_donation_count();
203
							}
204
						}
205
					}
206
				}
207
208
				if ( ! empty( $add_author ) ) {
209
					// Add the donor id in side the array.
210
					$donor_ids[] = (int) $post->post_author;
211
				}
212
			}
213
			/* Restore original Post Data */
214
		}
215
216
		// Get the total number of post found.
217
		$total_donation = (int) $donation_posts->found_posts;
218
219
		// Maximum number of page can be display
220
		$max_num_pages = (int) $donation_posts->max_num_pages;
221
222
		// Check current page is less then max number of page or not
223 View Code Duplication
		if ( $paged < $max_num_pages ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
			// Update the curretn page virable for the next step
225
			$this->update_option( $this->step_on_key, $paged );
226
227
			// Calculating percentage.
228
			$page_remain          = $max_num_pages - $paged;
229
			$this->total_step     = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) );
230
			$this->step_completed = $paged;
231
		} else {
232
			$donation_ids_count = count( $donor_ids );
0 ignored issues
show
Unused Code introduced by
$donation_ids_count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
233
			$this->update_option( $this->step_key, 'donation' );
234
			$this->update_option( $this->step_on_key, '0' );
235
		}
236
237
		$donor_ids = array_unique( $donor_ids );
238
		$this->update_option( $this->donor_key, $donor_ids );
239
		$this->update_option( $this->donation_key, $donation_ids );
240
241
		wp_reset_postdata();
242
	}
243
244
	/**
245
	 * Return the calculated completion percentage.
246
	 *
247
	 * @since 1.8.12
248
	 * @return int
249
	 */
250
	public function get_percentage_complete() {
251
		return ceil( ( 100 * $this->step_completed ) / $this->total_step );
252
	}
253
254
	public function process_step() {
255
256
		if ( ! $this->can_export() ) {
257
			wp_die( __( 'You do not have permission to delete Import transactions.', 'give' ), __( 'Error', 'give' ), array( 'response' => 403 ) );
258
		}
259
260
		$had_data = $this->get_data();
261
262
		if ( $had_data ) {
263
			$this->done = false;
264
265
			return true;
266
		} else {
267
			update_option( 'give_earnings_total', give_get_total_earnings( true ) );
268
			Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
269
270
			$this->delete_option( $this->donation_key );
271
272
			// Reset the sequential order numbers
273
			if ( give_get_option( 'enable_sequential' ) ) {
274
				delete_option( 'give_last_payment_number' );
275
			}
276
277
			$this->done    = true;
278
			$this->message = __( 'Imported donor and transactions successfully deleted.', 'give' );
279
280
			return false;
281
		}
282
	}
283
284
	/**
285
	 * Get the Export Data
286
	 *
287
	 * @access public
288
	 * @since 1.8.12
289
	 * @global object $wpdb Used to query the database using the WordPress Database API
290
	 *
291
	 * @return array|bool $data The data for the CSV file
292
	 */
293
	public function get_data() {
294
295
		// Get the donation id's.
296
		$donation_ids = $this->get_option( $this->donation_key );
297
298
		/**
299
		 * Return false id not Import donation is found.
300
		 */
301
		if ( empty( $donation_ids ) ) {
302
			$this->is_empty   = true;
303
			$this->total_step = 1;
304
305
			return false;
306
		}
307
308
		// Get the current step.
309
		$step = (int) $this->get_step();
310
311
		// get teh donor ids.
312
		$donor_ids = $this->get_option( $this->donor_key );
313
314
		// In step to we delete all the donation in loop.
315
		if ( 2 === $step ) {
316
			$pass_to_donor = false;
317
			$page          = (int) $this->get_step_page();
318
			$page ++;
319
			$count = count( $donation_ids );
320
321
			$this->total_step     = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
322
			$this->step_completed = $page;
323
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
324
325 View Code Duplication
			if ( $count > $this->per_step ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
326
327
				$this->update_option( $this->step_on_key, $page );
328
				$donation_ids = $this->get_delete_ids( $donation_ids, $page );
329
				$current_page = (int) ceil( $count / $this->per_step );
330
331
				if ( $page === $current_page ) {
332
					$pass_to_donor = true;
333
				}
334
			} else {
335
				$pass_to_donor = true;
336
			}
337
338
			if ( true === $pass_to_donor ) {
339
				$this->update_option( $this->step_key, 'donor' );
340
				$this->update_option( $this->step_on_key, '0' );
341
			}
342
343
			// Get the old form list.
344
			$form_ids = (array) $this->get_option( $this->form_key );
345
346
			foreach ( $donation_ids as $item ) {
347
				$form_ids[] = get_post_meta( $item, '_give_payment_form_id', true );
348
				wp_delete_post( $item, true );
349
			}
350
351
			// update the new form list.
352
			$this->update_option( $this->form_key, $form_ids );
353
		}
354
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
355
356
		// Here we delete all the donor
357
		if ( 3 === $step ) {
358
359
			// Get the old form list.
360
			$form_ids = (array) $this->get_option( $this->form_key );
361
			if ( ! empty( $form_ids ) ) {
362
				$form_ids = array_unique( $form_ids );
363
				foreach ( $form_ids as $form_id ) {
364
					give_recount_form_income_donation( (int) $form_id );
365
				}
366
			}
367
			// update the new form list.
368
			$this->update_option( $this->form_key, array() );
369
370
			$page  = (int) $this->get_step_page();
371
			$count = count( $donor_ids );
372
373
			$this->total_step     = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
374
			$this->step_completed = $page + ( count( $donation_ids ) / $this->per_step );
375
376
			$args = apply_filters( 'give_tools_reset_stats_total_args', array(
377
				'post_status'    => 'any',
378
				'posts_per_page' => 1,
379
				'author'         => $donor_ids[ $page ]
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
380
			) );
381
382
			$donations = array();
383
			$payments = new Give_Payments_Query( $args );
384
			$payments = $payments->get_payments();
385
			if ( empty( $payments ) ) {
386
				Give()->donors->delete_by_user_id( $donor_ids[ $page ] );
387
			} else {
388
				foreach ( $payments as $payment ) {
389
					$donations[] = $payment->ID;
390
				}
391
392
				$donor          = new Give_Donor( $donor_ids[ $page ], true );
393
				$data_to_update = array(
394
					'purchase_count' => count( $donations ),
395
					'payment_ids'    => implode( ',', $donations ),
396
				);
397
				$donor->update( $data_to_update );
398
			}
399
400
			$page ++;
401
			$this->update_option( $this->step_on_key, $page );
402
			if ( $count === $page ) {
403
				$this->is_empty = false;
404
405
				return false;
406
			}
407
408
			return true;
409
		}
410
411
		return true;
412
	}
413
414 View Code Duplication
	public function get_delete_ids( $donation_ids, $page ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
415
		$index            = $page --;
0 ignored issues
show
Unused Code introduced by
$index is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
416
		$count            = count( $donation_ids );
0 ignored issues
show
Unused Code introduced by
$count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
417
		$temp             = 0;
418
		$current_page     = 0;
419
		$post_delete      = $this->per_step;
420
		$page_donation_id = array();
421
422
		foreach ( $donation_ids as $item ) {
423
			$temp ++;
424
			$page_donation_id[ $current_page ][] = $item;
425
			if ( $temp === $post_delete ) {
426
				$current_page ++;
427
				$temp = 0;
428
			}
429
		}
430
431
		return $page_donation_id[ $page ];
432
	}
433
434
	/**
435
	 * Given a key, get the information from the Database Directly
436
	 *
437
	 * @since  1.8.12
438
	 *
439
	 * @param  string $key The option_name
440
	 *
441
	 * @return mixed       Returns the data from the database
442
	 */
443
	public function get_option( $key, $defalut_value = false ) {
444
		return get_option( $key, $defalut_value );
445
	}
446
447
	/**
448
	 * Give a key, store the value
449
	 *
450
	 * @since  1.8.12s
451
	 *
452
	 * @param  string $key The option_name
453
	 * @param  mixed $value The value to store
454
	 *
455
	 * @return void
456
	 */
457
	public function update_option( $key, $value ) {
458
		update_option( $key, $value, false );
459
	}
460
461
	/**
462
	 * Delete an option
463
	 *
464
	 * @since  1.8.12
465
	 *
466
	 * @param  string $key The option_name to delete
467
	 *
468
	 * @return void
469
	 */
470
	public function delete_option( $key ) {
471
		delete_option( $key );
472
	}
473
474
	/**
475
	 * Get the current step in number.
476
	 *
477
	 * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors.
478
	 *
479
	 * @return int|string
480
	 */
481 View Code Duplication
	private function get_step() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
482
		$step_key = (string) $this->get_option( $this->step_key, false );
483
		if ( 'count' === $step_key ) {
484
			return 1;
485
		} elseif ( 'donation' === $step_key ) {
486
			return 2;
487
		} elseif ( 'donor' === $step_key ) {
488
			return 3;
489
		} else {
490
			return $step_key;
491
		}
492
	}
493
494
	/**
495
	 * Get the current $page value in the ajax.
496
	 */
497
	private function get_step_page() {
498
		return $this->get_option( $this->step_on_key, false );
499
	}
500
}
501