Action_Scheduler_Queue_Manager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 41
c 2
b 0
f 0
dl 0
loc 92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cancel() 0 14 2
A exists() 0 5 1
A find_next() 0 10 3
A find_all() 0 28 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Used to managed events within queue.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\Queue
23
 * @since 0.1.0
24
 */
25
26
namespace PinkCrab\Queue\Queue_Driver\Action_Scheduler;
27
28
use DateTimeImmutable;
29
use PinkCrab\Queue\Event\Event;
30
use ActionScheduler_SimpleSchedule;
31
32
class Action_Scheduler_Queue_Manager {
33
34
	/**
35
	 * Cancel queued events.
36
	 *
37
	 * @param Event $event
38
	 * @param bool $next_only
39
	 * @return void
40
	 */
41
	public function cancel( Event $event, bool $next_only = false ): void {
42
		if ( $next_only ) {
43
			\as_unschedule_action(
44
				$event->get_hook(),
45
				$event->get_data() ?? array(),
46
				$event->get_group()
47
			);
48
			return;
49
		}
50
51
		\as_unschedule_all_actions(
52
			$event->get_hook(),
53
			$event->get_data() ?? array(),
54
			$event->get_group()
55
		);
56
	}
57
58
	/**
59
	 * Finds all or the next instance of an event.
60
	 *
61
	 * @param Event $event
62
	 * @return DateTimeImmutable|null
63
	 */
64
	public function find_next( Event $event ): ?DateTimeImmutable {
65
		$next = as_next_scheduled_action(
66
			$event->get_hook(),
67
			$event->get_data() ?? array(),
68
			$event->get_group()
69
		);
70
71
		return is_int( $next )
72
			? ( DateTimeImmutable::createFromFormat( 'U', \strval( $next ), wp_timezone() ) ?: null ) //phpcs:ignore
73
			: null;
74
	}
75
76
	/**
77
	 * Finds all or the next instance of an event.
78
	 *
79
	 * @param Event $event
80
	 * @return array<int, DateTimeImmutable>
81
	 */
82
	public function find_all( Event $event ): array {
83
		$events = as_get_scheduled_actions(
84
			array(
85
				'hook'         => $event->get_hook(),
86
				'args'         => $event->get_data() ?? array(),
87
				'group'        => $event->get_group(),
88
				'status'       => \ActionScheduler_Store::STATUS_PENDING,
89
				'orderby'      => 'date',
90
				'per_page'     => -1,
91
				'date'         => DateTimeImmutable::createFromFormat( 'U', 'now', wp_timezone() ),
92
				'date_compare' => '>',
93
			)
94
		);
95
96
		return array_reduce(
97
			$events,
98
			function ( array $carry, \ActionScheduler_Action $event ): array {
99
				/** @var ActionScheduler_SimpleSchedule */
100
				$schedule = $event->get_schedule();
101
102
				// Get the date from schedule, if valid add to array.
103
				$date = $schedule->get_date();
0 ignored issues
show
Bug introduced by
The method get_date() does not exist on ActionScheduler_Schedule. It seems like you code against a sub-type of said class. However, the method does not exist in ActionScheduler_Schedule_Deprecated. Are you sure you never get one of those? ( Ignorable by Annotation )

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

103
				/** @scrutinizer ignore-call */ 
104
    $date = $schedule->get_date();
Loading history...
104
				if ( $date instanceof \DateTime ) {
105
					$carry[] = DateTimeImmutable::createFromMutable( $date );
106
				}
107
				return $carry;
108
			},
109
			array()
110
		);
111
	}
112
113
	/**
114
	 * Checks if an event is queued.
115
	 *
116
	 * @param \PinkCrab\Queue\Event\Event $event
117
	 * @return bool
118
	 */
119
	public function exists( Event $event ): bool {
120
		return as_has_scheduled_action(
121
			$event->get_hook(),
122
			$event->get_data() ?? array(),
123
			$event->get_group()
124
		);
125
	}
126
}
127