Passed
Pull Request — master (#195)
by Jonathan
03:37
created

Object_Sync_Sf_Queue   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 48
c 1
b 0
f 0
dl 0
loc 198
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 2 1
A schedule_cron() 0 2 1
A get_next() 0 9 2
A search() 0 2 1
A get_frequency() 0 25 4
A cancel() 0 2 1
A schedule_single() 0 2 1
A schedule_recurring() 0 2 1
A get_frequencies() 0 17 3
A __construct() 0 5 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 $schedulable_classes;
22
23
	public function __construct( $wpdb, $version, $slug, $schedulable_classes ) {
24
		$this->wpdb                = $wpdb;
25
		$this->version             = $version;
26
		$this->slug                = $slug;
27
		$this->schedulable_classes = $schedulable_classes;
28
	}
29
30
	/**
31
	 * Get all the schedules with their frequencies, sorted
32
	 *
33
	 * @param string  $unit The unit of time
34
	 * @param string  $sort Which direction to sort
35
	 * @return array $this->schedulable_classes
36
	 */
37
	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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
	public function get_frequencies( /** @scrutinizer ignore-unused */ $unit = 'seconds', $sort = 'asc' ) {

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

Loading history...
38
39
		foreach ( $this->schedulable_classes as $key => $schedule ) {
40
			$this->schedulable_classes[ $key ]['frequency'] = $this->get_frequency( $key, 'seconds' );
41
		}
42
43
		if ( 'asc' === $sort ) {
44
			uasort( $this->schedulable_classes, function( $a, $b ) {
45
				return $a['frequency'] - $b['frequency'];
46
			});
47
		} else {
48
			uasort( $this->schedulable_classes, function( $a, $b ) {
49
				return $b['frequency'] - $a['frequency'];
50
			});
51
		}
52
53
		return $this->schedulable_classes;
54
55
	}
56
57
	/**
58
	 * Get a single schedule item's frequency
59
	 *
60
	 * @param string $name The name of the schedule
61
	 * @param string  $unit The unit of time
62
	 * @return int How often it runs in that unit of time
63
	 */
64
	public function get_frequency( $name, $unit ) {
65
		$schedule_number = get_option( 'object_sync_for_salesforce_' . $name . '_schedule_number', '' );
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
		$schedule_number = /** @scrutinizer ignore-call */ get_option( 'object_sync_for_salesforce_' . $name . '_schedule_number', '' );
Loading history...
66
		$schedule_unit   = get_option( 'object_sync_for_salesforce_' . $name . '_schedule_unit', '' );
67
68
		switch ( $schedule_unit ) {
69
			case 'minutes':
70
				$seconds = 60;
71
				$minutes = 1;
72
				break;
73
			case 'hours':
74
				$seconds = 3600;
75
				$minutes = 60;
76
				break;
77
			case 'days':
78
				$seconds = 86400;
79
				$minutes = 1440;
80
				break;
81
			default:
82
				$seconds = 0;
83
				$minutes = 0;
84
		}
85
86
		$total = ${$unit} * $schedule_number;
87
88
		return $total;
89
	}
90
91
	/**
92
	 * Enqueue an action to run one time, as soon as possible
93
	 *
94
	 * @param string $hook The hook to trigger.
95
	 * @param array  $args Arguments to pass when the hook triggers.
96
	 * @param string $group The group to assign this job to.
97
	 * @return string The action ID.
98
	 */
99
	public function add( $hook, $args = array(), $group = '' ) {
100
		return $this->schedule_single( time(), $hook, $args, $group );
101
	}
102
103
	/**
104
	 * Schedule an action to run once at some time in the future
105
	 *
106
	 * @param int    $timestamp When the job will run.
107
	 * @param string $hook The hook to trigger.
108
	 * @param array  $args Arguments to pass when the hook triggers.
109
	 * @param string $group The group to assign this job to.
110
	 * @return string The action ID.
111
	 */
112
	public function schedule_single( $timestamp, $hook, $args = array(), $group = '' ) {
113
		return as_schedule_single_action( $timestamp, $hook, $args, $group );
114
	}
115
116
	/**
117
	 * Schedule a recurring action
118
	 *
119
	 * @param int    $timestamp When the first instance of the job will run.
120
	 * @param int    $interval_in_seconds How long to wait between runs.
121
	 * @param string $hook The hook to trigger.
122
	 * @param array  $args Arguments to pass when the hook triggers.
123
	 * @param string $group The group to assign this job to.
124
	 * @return string The action ID.
125
	 */
126
	public function schedule_recurring( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
127
		return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $group );
128
	}
129
130
	/**
131
	 * Schedule an action that recurs on a cron-like schedule.
132
	 *
133
	 * @param int    $timestamp The schedule will start on or after this time.
134
	 * @param string $cron_schedule A cron-link schedule string.
135
	 * @see http://en.wikipedia.org/wiki/Cron
136
	 *   *    *    *    *    *    *
137
	 *   ┬    ┬    ┬    ┬    ┬    ┬
138
	 *   |    |    |    |    |    |
139
	 *   |    |    |    |    |    + year [optional]
140
	 *   |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
141
	 *   |    |    |    +---------- month (1 - 12)
142
	 *   |    |    +--------------- day of month (1 - 31)
143
	 *   |    +-------------------- hour (0 - 23)
144
	 *   +------------------------- min (0 - 59)
145
	 * @param string $hook The hook to trigger.
146
	 * @param array  $args Arguments to pass when the hook triggers.
147
	 * @param string $group The group to assign this job to.
148
	 * @return string The action ID
149
	 */
150
	public function schedule_cron( $timestamp, $cron_schedule, $hook, $args = array(), $group = '' ) {
151
		return as_schedule_cron_action( $timestamp, $cron_schedule, $hook, $args, $group );
152
	}
153
154
	/**
155
	 * Dequeue all actions with a matching hook (and optionally matching args and group) so they are not run.
156
	 *
157
	 * Any recurring actions with a matching hook will also be cancelled, not just the next scheduled action.
158
	 *
159
	 * Technically, one action in a recurring or Cron action is scheduled at any one point in time. The next
160
	 * in the sequence is scheduled after the previous one is run, so only the next scheduled action needs to
161
	 * be cancelled/dequeued to stop the sequence.
162
	 *
163
	 * @param string $hook The hook that the job will trigger.
164
	 * @param array  $args Args that would have been passed to the job.
165
	 * @param string $group Group name.
166
	 */
167
	public function cancel( $hook, $args = array(), $group = '' ) {
168
		as_unschedule_action( $hook, $args, $group );
169
	}
170
171
	/**
172
	 * Get the date and time for the next scheduled occurence of an action with a given hook
173
	 * (an optionally that matches certain args and group), if any.
174
	 *
175
	 * @param string $hook Hook name.
176
	 * @param array  $args Arguments.
177
	 * @param string $group Group name.
178
	 * @return time|null The date and time for the next occurrence, or null if there is no pending, scheduled action for the given hook.
0 ignored issues
show
Bug introduced by
The type time was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
179
	 */
180
	public function get_next( $hook, $args = null, $group = '' ) {
181
182
		$next_timestamp = as_next_scheduled_action( $hook, $args, $group );
183
184
		if ( $next_timestamp ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $next_timestamp of type integer|false is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false 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...
185
			return $next_timestamp;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $next_timestamp returns the type integer which is incompatible with the documented return type null|time.
Loading history...
186
		}
187
188
		return null;
189
	}
190
191
	/**
192
	 * Find scheduled actions
193
	 *
194
	 * @param array  $args Possible arguments, with their default values:
195
	 *        'hook' => '' - the name of the action that will be triggered
196
	 *        'args' => null - the args array that will be passed with the action
197
	 *        '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.
198
	 *        'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
199
	 *        '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.
200
	 *        'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
201
	 *        'group' => '' - the group the action belongs to
202
	 *        'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
203
	 *        'claimed' => null - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
204
	 *        'per_page' => 5 - Number of results to return
205
	 *        'offset' => 0
206
	 *        'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', or 'date'
207
	 *        'order' => 'ASC'.
208
	 *
209
	 * @param string $return_format OBJECT, ARRAY_A, or ids.
210
	 * @return array
211
	 */
212
	public function search( $args = array(), $return_format = OBJECT ) {
0 ignored issues
show
Bug introduced by
The constant OBJECT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
213
		return as_get_scheduled_actions( $args, $return_format );
214
	}
215
}
216