1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* The Queue Driver for WC Action Scheduler. |
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 ActionScheduler_Store; |
30
|
|
|
use PinkCrab\Queue\Event\Event; |
31
|
|
|
use PinkCrab\Queue\Queue_Driver\Queue; |
32
|
|
|
use PinkCrab\Queue\Queue_Driver\Action_Scheduler\Action_Scheduler_Dispatcher; |
33
|
|
|
|
34
|
|
|
class Action_Scheduler_Driver implements Queue { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The event dispatcher |
38
|
|
|
* |
39
|
|
|
* @var Action_Scheduler_Dispatcher |
40
|
|
|
*/ |
41
|
|
|
private $dispatcher; |
42
|
|
|
|
43
|
|
|
/** @var Action_Scheduler_Queue_Manager */ |
44
|
|
|
private $queue_manager; |
45
|
|
|
|
46
|
|
|
/** @var bool */ |
47
|
|
|
private static $included = false; |
48
|
|
|
|
49
|
|
|
public function __construct( Action_Scheduler_Dispatcher $dispatcher, Action_Scheduler_Queue_Manager $queue_manager ) { |
50
|
|
|
$this->dispatcher = $dispatcher; |
51
|
|
|
$this->queue_manager = $queue_manager; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Setup the Action Scheduler queue driver. |
56
|
|
|
* |
57
|
|
|
* @return Action_Scheduler_Driver |
58
|
|
|
*/ |
59
|
|
|
public static function get_instance(): self { |
60
|
|
|
$instance = new self( |
61
|
|
|
new Action_Scheduler_Dispatcher(), |
62
|
|
|
new Action_Scheduler_Queue_Manager() |
63
|
|
|
); |
64
|
|
|
return $instance; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Is run before the driver is initialized. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function setup(): void { |
73
|
|
|
|
74
|
|
|
// If the lib has already been included, bail. |
75
|
|
|
if ( self::$included ) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Path to Action Scheduler plugin directory. |
80
|
|
|
$path = join( |
81
|
|
|
\DIRECTORY_SEPARATOR, |
82
|
|
|
array( |
83
|
|
|
dirname( __DIR__, 3 ), |
84
|
|
|
'lib', |
85
|
|
|
'action-scheduler', |
86
|
|
|
'action-scheduler.php', |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
// Filter the path to the action-scheduler path. |
91
|
|
|
$path = apply_filters( 'pinkcrab_queue_action_scheduler_path', $path ); |
92
|
|
|
|
93
|
|
|
// Check if the file exists. |
94
|
|
|
if ( ! file_exists( $path ) ) { |
95
|
|
|
throw new \RuntimeException( 'Action Scheduler is not installed.' ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Include the Action Scheduler plugin. |
99
|
|
|
require_once $path; |
100
|
|
|
|
101
|
|
|
// Mark as included. |
102
|
|
|
self::$included = true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Is run after the driver is initialized. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
* @codeCoverageIgnore |
110
|
|
|
*/ |
111
|
|
|
public function teardown(): void { |
112
|
|
|
// noOp |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Initialise the driver. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
* @codeCoverageIgnore |
120
|
|
|
*/ |
121
|
|
|
public function init(): void { |
122
|
|
|
// noOp |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Dispatch Event |
127
|
|
|
* |
128
|
|
|
* @param Event $event |
129
|
|
|
*/ |
130
|
|
|
public function dispatch( Event $event ): ?int { |
131
|
|
|
$result = $this->dispatcher->dispatch( $event ); |
132
|
|
|
return \is_int( $result ) ? $result : null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Cancel next occurrence of Event |
137
|
|
|
* |
138
|
|
|
* @param Event $event |
139
|
|
|
*/ |
140
|
|
|
public function cancel_next( Event $event ): void { |
141
|
|
|
$this->queue_manager->cancel( $event, true ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Cancel all occurrences of Event |
146
|
|
|
* |
147
|
|
|
* @param Event $event |
148
|
|
|
*/ |
149
|
|
|
public function cancel_all( Event $event ): void { |
150
|
|
|
$this->queue_manager->cancel( $event, false ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get next occurrence of Event |
155
|
|
|
* |
156
|
|
|
* @param Event $event |
157
|
|
|
* @return DateTimeImmutable|null |
158
|
|
|
*/ |
159
|
|
|
public function find_next( Event $event ): ?DateTimeImmutable { |
160
|
|
|
return $this->queue_manager->find_next( $event ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get all occurrences of Event |
165
|
|
|
* |
166
|
|
|
* @param Event $event |
167
|
|
|
* @return DateTimeImmutable[] |
168
|
|
|
*/ |
169
|
|
|
public function find_all( Event $event ): array { |
170
|
|
|
return $this->queue_manager->find_all( $event ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Checks if an event is scheduled. |
176
|
|
|
* |
177
|
|
|
* @param Event $event |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function is_scheduled( Event $event ): bool { |
181
|
|
|
return $this->queue_manager->exists( $event ); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|