Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
BatchQueue | |
0.00% |
0 / 51 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
push | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
pull | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
remove | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
reserve | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
release | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
count | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\EventQueue\Queues; |
4 | |
5 | use NewfoldLabs\WP\Module\Data\Event; |
6 | use NewfoldLabs\WP\Module\Data\EventQueue\Queryable; |
7 | use NewfoldLabs\WP\ModuleLoader\Container; |
8 | |
9 | class BatchQueue implements BatchQueueInterface { |
10 | |
11 | use Queryable; |
12 | |
13 | /** |
14 | * Dependency injection container |
15 | * |
16 | * @var Container $container |
17 | */ |
18 | protected $container; |
19 | |
20 | /** |
21 | * Constructor |
22 | * |
23 | * @param Container $container |
24 | */ |
25 | public function __construct( Container $container ) { |
26 | $this->container = $container; |
27 | } |
28 | |
29 | /** |
30 | * Push events onto the queue |
31 | * |
32 | * @param non-empty-array<Event> $events |
33 | * |
34 | * @return bool |
35 | */ |
36 | public function push( array $events ) { |
37 | |
38 | $time = current_time( 'mysql' ); |
39 | |
40 | $inserts = array(); |
41 | foreach ( $events as $event ) { |
42 | $inserts[] = array( |
43 | 'event' => serialize( $event ), |
44 | 'available_at' => current_time( 'mysql' ), |
45 | 'created_at' => $time, |
46 | ); |
47 | } |
48 | |
49 | return (bool) $this->bulkInsert( $this->table(), $inserts ); |
50 | } |
51 | |
52 | /** |
53 | * Pull events from the queue |
54 | * |
55 | * @return Event[] |
56 | */ |
57 | public function pull( int $count ) { |
58 | |
59 | $events = array(); |
60 | |
61 | $rawEvents = $this |
62 | ->query() |
63 | ->select( '*' ) |
64 | ->from( $this->table(), false ) |
65 | ->whereNull( 'reserved_at' ) |
66 | ->where( 'available_at', '<=', current_time( 'mysql' ) ) |
67 | ->order_by( 'available_at' ) |
68 | ->limit( $count ) |
69 | ->get(); |
70 | |
71 | if ( ! is_array( $rawEvents ) ) { |
72 | return $events; |
73 | } |
74 | |
75 | foreach ( $rawEvents as $rawEvent ) { |
76 | if ( property_exists( $rawEvent, 'id' ) && property_exists( $rawEvent, 'event' ) ) { |
77 | $eventData = maybe_unserialize( $rawEvent->event ); |
78 | if ( is_array( $eventData ) && property_exists( $rawEvent, 'created_at' ) ) { |
79 | $eventData['created_at'] = $rawEvent->created_at; |
80 | } |
81 | $events[ $rawEvent->id ] = $eventData; |
82 | } |
83 | } |
84 | |
85 | return $events; |
86 | } |
87 | |
88 | /** |
89 | * Remove events from the queue |
90 | * |
91 | * @param int[] $ids |
92 | * |
93 | * @return bool |
94 | */ |
95 | public function remove( array $ids ) { |
96 | return (bool) $this |
97 | ->query() |
98 | ->table( $this->table(), false ) |
99 | ->whereIn( 'id', $ids ) |
100 | ->delete(); |
101 | } |
102 | |
103 | /** |
104 | * Reserve events in the queue |
105 | * |
106 | * @param int[] $ids |
107 | * |
108 | * @return bool |
109 | */ |
110 | public function reserve( array $ids ) { |
111 | return (bool) $this |
112 | ->query() |
113 | ->table( $this->table(), false ) |
114 | ->whereIn( 'id', $ids ) |
115 | ->update( array( 'reserved_at' => current_time( 'mysql' ) ) ); |
116 | } |
117 | |
118 | /** |
119 | * Release events back onto the queue |
120 | * |
121 | * @param int[] $ids |
122 | * |
123 | * @return bool |
124 | */ |
125 | public function release( array $ids ) { |
126 | return (bool) $this |
127 | ->query() |
128 | ->table( $this->table(), false ) |
129 | ->whereIn( 'id', $ids ) |
130 | ->update( array( 'reserved_at' => null ) ); |
131 | } |
132 | |
133 | /** |
134 | * Count the number of events in the queue |
135 | * |
136 | * @return int |
137 | */ |
138 | public function count() { |
139 | return $this |
140 | ->query() |
141 | ->select( '*' ) |
142 | ->from( $this->table(), false ) |
143 | ->whereNull( 'reserved_at' ) |
144 | ->where( 'available_at', '<=', current_time( 'mysql' ) ) |
145 | ->count(); |
146 | } |
147 | } |