Test Failed
Push — master ( 1d3b59...426730 )
by Devin
01:16
created

Give_Background_Updater::task()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 83
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 43
nc 16
nop 1
dl 0
loc 83
rs 5.48
c 0
b 0
f 0

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
 * Background Updater
4
 *
5
 * Uses https://github.com/A5hleyRich/wp-background-processing to handle DB
6
 * updates in the background.
7
 *
8
 * @class    Give_Background_Updater
9
 * @version  2.0.0
10
 * @package  Give/Classes
11
 * @category Class
12
 * @author   WordImpress
13
 */
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
/**
19
 * Give_Background_Updater Class.
20
 */
21
class Give_Background_Updater extends WP_Background_Process {
22
23
	/**
24
	 * @var string
25
	 */
26
	protected $action = 'give_db_updater';
27
28
	/**
29
	 * Dispatch updater.
30
	 *
31
	 * Updater will still run via cron job if this fails for any reason.
32
	 */
33
	public function dispatch() {
34
		/* @var WP_Background_Process $dispatched */
35
		parent::dispatch();
36
	}
37
38
39
	/**
40
	 * Get all batches.
41
	 *
42
	 * @since  2.0
43
	 * @access public
44
	 * @return stdClass
45
	 */
46
	public function get_all_batch() {
47
		return parent::get_batch();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (get_batch() instead of get_all_batch()). Are you sure this is correct? If so, you might want to change this to $this->get_batch().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
48
	}
49
50
	/**
51
	 * Handle cron healthcheck
52
	 *
53
	 * Restart the background process if not already running
54
	 * and data exists in the queue.
55
	 */
56
	public function handle_cron_healthcheck() {
57
		if ( $this->is_process_running() || $this->is_paused_process()  ) {
58
			// Background process already running.
59
			return;
60
		}
61
62
		if ( $this->is_queue_empty() ) {
63
			// No data to process.
64
			$this->clear_scheduled_event();
65
66
			return;
67
		}
68
69
		$this->handle();
70
	}
71
72
	/**
73
	 * Schedule fallback event.
74
	 */
75
	protected function schedule_event() {
76
		if ( ! wp_next_scheduled( $this->cron_hook_identifier ) && ! $this->is_paused_process() ) {
77
			wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier );
78
		}
79
	}
80
81
	/**
82
	 * Task
83
	 *
84
	 * Override this method to perform any actions required on each
85
	 * queue item. Return the modified item for further processing
86
	 * in the next pass through. Or, return false to remove the
87
	 * item from the queue.
88
	 *
89
	 * @param array $update Update info
90
	 *
91
	 * @return mixed
92
	 */
93
	protected function task( $update ) {
94
		// Pause upgrade immediately if admin pausing upgrades.
95
		if( $this->is_paused_process() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
96
			wp_die();
97
		}
98
99
		if ( empty( $update ) ) {
100
			return false;
101
		}
102
103
		/* @var  Give_Updates $give_updates */
104
		$give_updates  = Give_Updates::get_instance();
105
		$resume_update = get_option(
106
			'give_doing_upgrade',
107
0 ignored issues
show
Coding Style introduced by
There should be no empty lines in a multi-line function call.
Loading history...
108
			// Default update.
109
			array(
110
				'update_info' => $update,
111
				'step'        => 1,
112
				'update'      => 1,
113
				'heading'     => sprintf( 'Update %s of {update_count}', 1 ),
114
				'percentage'  => $give_updates->percentage,
115
			)
116
		);
117
118
		// Continuously skip update if previous update does not complete yet.
119
		if (
120
			$resume_update['update_info']['id'] !== $update['id'] &&
121
			! give_has_upgrade_completed( $resume_update['update_info']['id'] )
122
		) {
123
			return $update;
124
		}
125
126
		// Set params.
127
		$resume_update['update_info'] = $update;
128
		$give_updates->step           = absint( $resume_update['step'] );
129
		$give_updates->update         = absint( $resume_update['update'] );
130
		$is_parent_update_completed   = $give_updates->is_parent_updates_completed( $update );
131
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
132
133
		// Skip update if dependency update does not complete yet.
134
		if ( empty( $is_parent_update_completed ) ) {
135
			// @todo: set error when you have only one update with invalid dependency
136
			if ( ! is_null( $is_parent_update_completed ) ) {
137
				return $update;
138
			}
139
140
			return false;
141
		}
142
143
		// Disable cache.
144
		Give_Cache::disable();
145
146
		// Run update.
147
		if ( is_array( $update['callback'] ) ) {
148
			$update['callback'][0]->$update['callback'][1]();
149
		} else {
150
			$update['callback']();
151
		}
152
153
		// Set update info.
154
		$doing_upgrade_args = array(
155
			'update_info'      => $update,
156
			'step'             => ++ $give_updates->step,
157
			'update'           => $give_updates->update,
158
			'heading'          => sprintf( 'Update %s of %s', $give_updates->update, get_option( 'give_db_update_count' ) ),
159
			'percentage'       => $give_updates->percentage,
160
			'total_percentage' => $give_updates->get_db_update_processing_percentage(),
161
		);
162
163
		// Cache upgrade.
164
		update_option( 'give_doing_upgrade', $doing_upgrade_args );
165
166
		// Enable cache.
167
		Give_Cache::enable();
168
169
		// Check if current update completed or not.
170
		if ( give_has_upgrade_completed( $update['id'] ) ) {
171
			return false;
172
		}
173
174
		return $update;
175
	}
176
177
	/**
178
	 * Complete
179
	 *
180
	 * Override if applicable, but ensure that the below actions are
181
	 * performed, or, call parent::complete().
182
	 */
183
	protected function complete() {
184
		if( $this->is_paused_process() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
185
			return false;
186
		}
187
188
		parent::complete();
189
190
		delete_option( 'give_db_update_count' );
191
		delete_option( 'give_doing_upgrade' );
192
		add_option( 'give_show_db_upgrade_complete_notice', 1, '', 'no' );
193
194
		// Flush cache.
195
		Give_Cache::get_instance()->flush_cache();
196
197
		if( $cache_keys = Give_Cache::get_options_like('') ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
198
			Give_Cache::delete( $cache_keys );
199
		}
200
	}
201
202
	/**
203
	 * Get memory limit
204
	 *
205
	 * @return int
206
	 */
207
	protected function get_memory_limit() {
208
		if ( function_exists( 'ini_get' ) ) {
209
			$memory_limit = ini_get( 'memory_limit' );
210
		} else {
211
			// Sensible default.
212
			$memory_limit = '128M';
213
		}
214
215
		if ( ! $memory_limit || '-1' === $memory_limit ) {
216
			// Unlimited, set to 32GB.
217
			$memory_limit = '32000M';
218
		}
219
220
		return intval( $memory_limit ) * 1024 * 1024;
221
	}
222
223
	/**
224
	 * Maybe process queue
225
	 *
226
	 * Checks whether data exists within the queue and that
227
	 * the process is not already running.
228
	 */
229
	public function maybe_handle() {
230
		// Don't lock up other requests while processing
231
		session_write_close();
0 ignored issues
show
introduced by
The use of PHP session function session_write_close() is prohibited.
Loading history...
232
233
		if ( $this->is_process_running() || $this->is_paused_process() ) {
234
			// Background process already running.
235
			wp_die();
236
		}
237
238
		if ( $this->is_queue_empty() ) {
239
			// No data to process.
240
			wp_die();
241
		}
242
243
		check_ajax_referer( $this->identifier, 'nonce' );
244
245
		$this->handle();
246
247
		wp_die();
248
	}
249
250
251
	/**
252
	 * Check if backgound upgrade paused or not.
253
	 *
254
	 * @since 2.0
255
	 * @access public
256
	 * @return bool
257
	 */
258
	public function is_paused_process(){
259
		// Not using get_option because i am facing some caching releated issue.
260
		global $wpdb;
261
262
		$options = $wpdb->get_results( "SELECT * FROM $wpdb->options WHERE option_name='give_paused_batches' LIMIT 1" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
263
264
		return ! empty( $options );
265
	}
266
267
268
	/**
269
	 * Get identifier
270
	 *
271
	 * @since  2.0
272
	 * @access public
273
	 * @return mixed|string
274
	 */
275
	public function get_identifier() {
276
		return $this->identifier;
277
	}
278
279
	/**
280
	 * Get cron identifier
281
	 *
282
	 * @since  2.0
283
	 * @access public
284
	 * @return mixed|string
285
	 */
286
	public function get_cron_identifier() {
287
		return $this->cron_hook_identifier;
288
	}
289
}
290