Test Failed
Push — feature/update-process ( bd4647 )
by Ravinder
04:45
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
195
	/**
196
	 * Get memory limit
197
	 *
198
	 * @return int
199
	 */
200
	protected function get_memory_limit() {
201
		if ( function_exists( 'ini_get' ) ) {
202
			$memory_limit = ini_get( 'memory_limit' );
203
		} else {
204
			// Sensible default.
205
			$memory_limit = '128M';
206
		}
207
208
		if ( ! $memory_limit || '-1' === $memory_limit ) {
209
			// Unlimited, set to 32GB.
210
			$memory_limit = '32000M';
211
		}
212
213
		return intval( $memory_limit ) * 1024 * 1024;
214
	}
215
216
	/**
217
	 * Maybe process queue
218
	 *
219
	 * Checks whether data exists within the queue and that
220
	 * the process is not already running.
221
	 */
222
	public function maybe_handle() {
223
		// Don't lock up other requests while processing
224
		session_write_close();
0 ignored issues
show
introduced by
The use of PHP session function session_write_close() is prohibited.
Loading history...
225
226
		if ( $this->is_process_running() || $this->is_paused_process() ) {
227
			// Background process already running.
228
			wp_die();
229
		}
230
231
		if ( $this->is_queue_empty() ) {
232
			// No data to process.
233
			wp_die();
234
		}
235
236
		check_ajax_referer( $this->identifier, 'nonce' );
237
238
		$this->handle();
239
240
		wp_die();
241
	}
242
243
244
	/**
245
	 * Check if backgound upgrade paused or not.
246
	 *
247
	 * @since 2.0
248
	 * @access public
249
	 * @return bool
250
	 */
251
	public function is_paused_process(){
252
		// Not using get_option because i am facing some caching releated issue.
253
		global $wpdb;
254
255
		$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...
256
257
		return ! empty( $options );
258
	}
259
260
261
	/**
262
	 * Get identifier
263
	 *
264
	 * @since  2.0
265
	 * @access public
266
	 * @return mixed|string
267
	 */
268
	public function get_identifier() {
269
		return $this->identifier;
270
	}
271
}
272