Test Failed
Pull Request — master (#238)
by Jonathan
05:20
created

Object_Sync_Sf_Queue   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 0
dl 0
loc 244
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A add_actions() 0 4 1
A action_scheduler_batch_size() 0 5 1
A action_scheduler_concurrent_batches() 0 5 1
A get_frequencies() 0 26 5
A get_frequency() 0 26 4
A add() 0 3 1
A schedule_single() 0 3 1
A schedule_recurring() 0 3 1
A schedule_cron() 0 3 1
A cancel() 0 3 1
A get_next() 0 10 2
A search() 0 3 1
1
<?php
2
/**
3
 * Object Sync for Salesforce Queue
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit; // Exit if accessed directly.
8
}
9
10
/**
11
 * Object_Sync_Sf_Queue
12
 *
13
 * Manage the queue
14
 *
15
 */
16
class Object_Sync_Sf_Queue {
17
18
	protected $wpdb;
19
	protected $version;
20
	protected $slug;
21
	protected $option_prefix;
22
	protected $schedulable_classes;
23
24
	public function __construct( $wpdb, $version, $slug, $option_prefix, $schedulable_classes ) {
25
		$this->wpdb                = $wpdb;
26
		$this->version             = $version;
27
		$this->slug                = $slug;
28
		$this->option_prefix       = isset( $option_prefix ) ? $option_prefix : 'object_sync_for_salesforce_';
29
		$this->schedulable_classes = $schedulable_classes;
30
31
		$this->add_actions();
32
	}
33
34
	/**
35
	 * Add actions
36
	 *
37
	 */
38
	private function add_actions() {
39
		add_filter( 'action_scheduler_queue_runner_batch_size', array( $this, 'action_scheduler_batch_size' ) );
40
		add_filter( 'action_scheduler_queue_runner_concurrent_batches', array( $this, 'action_scheduler_concurrent_batches' ) );
41
	}
42
43
	/**
44
	 * Set the batch size.
45
	 *
46
	 * @param int  $batch_size
47
	 * @return int  $batch_size
48
	 */
49
	public function action_scheduler_batch_size( $batch_size ) {
0 ignored issues
show
Unused Code introduced by
The parameter $batch_size 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...
50
		// default for this library is 20 so that is where we start
51
		$batch_size = filter_var( get_option( $this->option_prefix . 'action_scheduler_batch_size', 20 ), FILTER_VALIDATE_INT );
52
		return $batch_size;
53
	}
54
55
	/**
56
	 * Set the number of concurrent batches that can run.
57
	 *
58
	 * @param int  $concurrent_batches
59
	 * @return int  $concurrent_batches
60
	 */
61
	public function action_scheduler_concurrent_batches( $concurrent_batches ) {
0 ignored issues
show
Unused Code introduced by
The parameter $concurrent_batches 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...
62
		// default for this library is 5 so that is where we start
63
		$concurrent_batches = filter_var( get_option( $this->option_prefix . 'action_scheduler_concurrent_batches', 5 ), FILTER_VALIDATE_INT );
64
		return $concurrent_batches;
65
	}
66
67
	/**
68
	 * Get all the schedules with their frequencies, sorted
69
	 *
70
	 * @param string  $unit The unit of time
71
	 * @param string  $sort Which direction to sort
72
	 * @return array $this->schedulable_classes
73
	 */
74
	public function get_frequencies( $unit = 'seconds', $sort = 'asc' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $unit 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...
75
76
		foreach ( $this->schedulable_classes as $key => $schedule ) {
77
			$this->schedulable_classes[ $key ]['frequency'] = $this->get_frequency( $key, 'seconds' );
78
		}
79
80
		if ( 'asc' === $sort ) {
81
			uasort( $this->schedulable_classes, function( $a, $b ) {
82
				// we want zero values at the top of an ascending sort
83
				if ( 0 === $a['frequency'] ) {
84
					return 1;
85
				}
86
				if ( 0 === $b['frequency'] ) {
87
					return -1;
88
				}
89
				return $a['frequency'] - $b['frequency'];
90
			});
91
		} else {
92
			uasort( $this->schedulable_classes, function( $a, $b ) {
93
				return $b['frequency'] - $a['frequency'];
94
			});
95
		}
96
97
		return $this->schedulable_classes;
98
99
	}
100
101
	/**
102
	 * Get a single schedule item's frequency
103
	 *
104
	 * @param string $name The name of the schedule
105
	 * @param string  $unit The unit of time
106
	 * @return int How often it runs in that unit of time
107
	 */
108
	public function get_frequency( $name, $unit ) {
109
		$schedule_number = filter_var( get_option( $this->option_prefix . $name . '_schedule_number', '' ), FILTER_VALIDATE_INT );
110
		$schedule_unit   = get_option( $this->option_prefix . $name . '_schedule_unit', '' );
111
112
		switch ( $schedule_unit ) {
113
			case 'minutes':
114
				$seconds = 60;
115
				$minutes = 1;
116
				break;
117
			case 'hours':
118
				$seconds = 3600;
119
				$minutes = 60;
120
				break;
121
			case 'days':
122
				$seconds = 86400;
123
				$minutes = 1440;
124
				break;
125
			default:
126
				$seconds = 0;
127
				$minutes = 0;
128
		}
129
130
		$total = ${$unit} * $schedule_number;
131
132
		return $total;
133
	}
134
135
	/**
136
	 * Enqueue an action to run one time, as soon as possible
137
	 *
138
	 * @param string $hook The hook to trigger.
139
	 * @param array  $args Arguments to pass when the hook triggers.
140
	 * @param string $group The group to assign this job to.
141
	 * @return string The action ID.
142
	 */
143
	public function add( $hook, $args = array(), $group = '' ) {
144
		return $this->schedule_single( time(), $hook, $args, $group );
145
	}
146
147
	/**
148
	 * Schedule an action to run once at some time in the future
149
	 *
150
	 * @param int    $timestamp When the job will run.
151
	 * @param string $hook The hook to trigger.
152
	 * @param array  $args Arguments to pass when the hook triggers.
153
	 * @param string $group The group to assign this job to.
154
	 * @return string The action ID.
155
	 */
156
	public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) {
157
		return as_schedule_single_action( $timestamp, $hook, $args, $group );
158
	}
159
160
	/**
161
	 * Schedule a recurring action
162
	 *
163
	 * @param int    $timestamp When the first instance of the job will run.
164
	 * @param int    $interval_in_seconds How long to wait between runs.
165
	 * @param string $hook The hook to trigger.
166
	 * @param array  $args Arguments to pass when the hook triggers.
167
	 * @param string $group The group to assign this job to.
168
	 * @return string The action ID.
169
	 */
170
	public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
171
		return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
172
	}
173
174
	/**
175
	 * Schedule an action that recurs on a cron-like schedule.
176
	 *
177
	 * @param int    $timestamp The schedule will start on or after this time.
178
	 * @param string $cron_schedule A cron-link schedule string.
179
	 * @see http://en.wikipedia.org/wiki/Cron
180
	 *   *    *    *    *    *    *
181
	 *   ┬    ┬    ┬    ┬    ┬    ┬
182
	 *   |    |    |    |    |    |
183
	 *   |    |    |    |    |    + year [optional]
184
	 *   |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
185
	 *   |    |    |    +---------- month (1 - 12)
186
	 *   |    |    +--------------- day of month (1 - 31)
187
	 *   |    +-------------------- hour (0 - 23)
188
	 *   +------------------------- min (0 - 59)
189
	 * @param string $hook The hook to trigger.
190
	 * @param array  $args Arguments to pass when the hook triggers.
191
	 * @param string $group The group to assign this job to.
192
	 * @return string The action ID
193
	 */
194
	public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) {
195
		return as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group );
196
	}
197
198
	/**
199
	 * Dequeue all actions with a matching hook (and optionally matching args and group) so they are not run.
200
	 *
201
	 * Any recurring actions with a matching hook will also be cancelled, not just the next scheduled action.
202
	 *
203
	 * Technically, one action in a recurring or Cron action is scheduled at any one point in time. The next
204
	 * in the sequence is scheduled after the previous one is run, so only the next scheduled action needs to
205
	 * be cancelled/dequeued to stop the sequence.
206
	 *
207
	 * @param string $hook The hook that the job will trigger.
208
	 * @param array  $args Args that would have been passed to the job.
209
	 * @param string $group Group name.
210
	 */
211
	public function cancel( $hook, $args = array(), $group = '' ) {
212
		as_unschedule_action( $hook, $args, $group );
213
	}
214
215
	/**
216
	 * Get the date and time for the next scheduled occurence of an action with a given hook
217
	 * (an optionally that matches certain args and group), if any.
218
	 *
219
	 * @param string $hook Hook name.
220
	 * @param array  $args Arguments.
221
	 * @param string $group Group name.
222
	 * @return time|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook.
223
	 */
224
	public function get_next( $hook, $args = null, $group = '' ) {
225
226
		$next_timestamp = as_next_scheduled_action( $hook, $args, $group );
227
228
		if ( $next_timestamp ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $next_timestamp of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
229
			return $next_timestamp;
230
		}
231
232
		return null;
233
	}
234
235
	/**
236
	 * Find scheduled actions
237
	 *
238
	 * @param array  $args Possible arguments, with their default values:
239
	 *        'hook' => '' - the name of the action that will be triggered
240
	 *        'args' => null - the args array that will be passed with the action
241
	 *        'date' => null - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
242
	 *        'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
243
	 *        'modified' => null - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
244
	 *        'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
245
	 *        'group' => '' - the group the action belongs to
246
	 *        'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
247
	 *        'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
248
	 *        'per_page' => 5 - Number of results to return
249
	 *        'offset' => 0
250
	 *        'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
251
	 *        'order' => 'ASC'.
252
	 *
253
	 * @param string $return_format OBJECT, ARRAY_A, or ids.
254
	 * @return array
255
	 */
256
	public function search( $args = array(), $return_format = OBJECT ) {
257
		return as_get_scheduled_actions( $args, $return_format );
258
	}
259
}
260