1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Used to dispatch an event based on its type. |
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 PinkCrab\Queue\Event\Async_Event; |
31
|
|
|
use PinkCrab\Queue\Event\Delayed_Event; |
32
|
|
|
use PinkCrab\Queue\Event\Recurring_Event; |
33
|
|
|
|
34
|
|
|
class Action_Scheduler_Dispatcher { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Dispatch Event |
38
|
|
|
* |
39
|
|
|
* @param Event $event |
40
|
|
|
* @return integer|null Returns the event id. |
41
|
|
|
*/ |
42
|
|
|
public function dispatch( Event $event ): ?int { |
43
|
|
|
// @phpstan-ignore-next-line |
44
|
|
|
$queue_id = \call_user_func( array( $this, $this->get_dispatch_type( $event ) ), $event ); |
45
|
|
|
return \is_int( $queue_id ) ? $queue_id : null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the dispatch type based on event values. |
50
|
|
|
* |
51
|
|
|
* @param Event $event |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
private function get_dispatch_type( Event $event ): string { |
55
|
|
|
// If the event is async or there is no delay or interval. |
56
|
|
|
if ( $event instanceof Async_Event |
57
|
|
|
|| ( is_null( $event->delayed_until() ) && is_null( $event->interval() ) ) |
58
|
|
|
) { |
59
|
|
|
// Return async. |
60
|
|
|
return 'dispatch_async'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// If the event is delayed or has no interval. |
64
|
|
|
if ( $event instanceof Delayed_Event |
65
|
|
|
|| ( $event->delayed_until() instanceof DateTimeImmutable && is_null( $event->interval() ) ) |
66
|
|
|
) { |
67
|
|
|
// Return delayed. |
68
|
|
|
return 'dispatch_at'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// If the event is a recurring event and has an interval. |
72
|
|
|
if ( $event instanceof Recurring_Event |
73
|
|
|
|| ( is_int( $event->interval() ) && $event->interval() > 0 ) |
74
|
|
|
) { |
75
|
|
|
// Return recurring. |
76
|
|
|
return 'dispatch_recurring'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// If we get to the end and none has passed, throw exception. |
80
|
|
|
throw new \Exception( 'Unable to determine dispatch type.' ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Dispatches the event to the queue immediately. |
85
|
|
|
* |
86
|
|
|
* @param Event $event |
87
|
|
|
* @return integer Returns the event id. |
88
|
|
|
*/ |
89
|
|
|
private function dispatch_async( Event $event ): int { |
90
|
|
|
return \as_enqueue_async_action( |
91
|
|
|
$event->get_hook(), |
92
|
|
|
$event->get_data() ?? array(), |
93
|
|
|
$event->get_group(), |
94
|
|
|
$event->is_unique() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Dispatches the event to the queue at a specific time. |
100
|
|
|
* |
101
|
|
|
* @param Event $event |
102
|
|
|
* @return integer Returns the event id. |
103
|
|
|
*/ |
104
|
|
|
private function dispatch_at( Event $event ): int { |
105
|
|
|
if ( ! $event->delayed_until() instanceof DateTimeImmutable ) { |
106
|
|
|
throw new \InvalidArgumentException( 'Event must have a delayed_until date/time.' ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var DateTimeImmutable |
111
|
|
|
*/ |
112
|
|
|
$delayed_until = $event->delayed_until(); |
113
|
|
|
|
114
|
|
|
return as_schedule_single_action( |
115
|
|
|
(int) $delayed_until->setTimezone( wp_timezone() )->format( 'U' ), |
116
|
|
|
$event->get_hook(), |
117
|
|
|
$event->get_data() ?? array(), |
118
|
|
|
$event->get_group(), |
119
|
|
|
$event->is_unique() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Dispatches a recurring event to the queue. |
125
|
|
|
* |
126
|
|
|
* @param Event $event |
127
|
|
|
* @return integer Returns the event id. |
128
|
|
|
*/ |
129
|
|
|
private function dispatch_recurring( Event $event ): int { |
130
|
|
|
if ( $event->interval() === null ) { |
131
|
|
|
throw new \InvalidArgumentException( 'Event must have an for it to be recurring.' ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @var DateTimeImmutable |
136
|
|
|
*/ |
137
|
|
|
$delayed_until = $event->delayed_until() instanceof \DateTimeImmutable |
138
|
|
|
? $event->delayed_until()->setTimezone( wp_timezone() ) |
139
|
|
|
: DateTimeImmutable::createFromFormat( 'U', '0', wp_timezone() ); |
140
|
|
|
|
141
|
|
|
return as_schedule_recurring_action( |
142
|
|
|
(int) $delayed_until->format( 'U' ), |
143
|
|
|
$event->interval(), |
144
|
|
|
$event->get_hook(), |
145
|
|
|
$event->get_data() ?? array(), |
146
|
|
|
$event->get_group(), |
147
|
|
|
$event->is_unique() |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|