Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
30 / 30
CRAP
100.00% covered (success)
100.00%
1 / 1
Worker
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
30 / 30
45
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getApplication
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 application
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasApplication
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addQueue
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addQueues
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getQueues
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQueue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasQueue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 work
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 workAll
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 run
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 runAll
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 clear
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clearFailed
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clearTasks
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clearAll
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clearAllFailed
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 clearAllTasks
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 offsetSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetExists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetUnset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 count
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Queue;
15
16use ArrayIterator;
17use Pop\Application;
18use Pop\Queue\Process\AbstractJob;
19
20/**
21 * Queue worker class
22 *
23 * @category   Pop
24 * @package    Pop\Queue
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    2.0.0
29 */
30class Worker implements \ArrayAccess, \Countable, \IteratorAggregate
31{
32
33    /**
34     * Queues
35     * @var array
36     */
37    protected array $queues = [];
38
39    /**
40     * Application object
41     * @var ?Application
42     */
43    protected ?Application $application = null;
44
45    /**
46     * Constructor
47     *
48     * Instantiate the queue worker object.
49     *
50     * @param mixed $queues
51     * @param ?Application $application
52     */
53    public function __construct(mixed $queues = null, ?Application $application = null)
54    {
55        if (!empty($queues)) {
56            if (is_array($queues)) {
57                $this->addQueues($queues);
58            } else if ($queues instanceof Queue) {
59                $this->addQueue($queues);
60            }
61        }
62
63        $this->application = $application;
64    }
65
66    /**
67     * Create queue worker worker
68     *
69     * @param  mixed $queues
70     * @param  ?Application $application
71     * @return Worker
72     */
73    public static function create(mixed $queues = null, ?Application $application = null): Worker
74    {
75        return new self($queues, $application);
76    }
77
78    /**
79     * Get the application
80     *
81     * @return ?Application
82     */
83    public function getApplication(): ?Application
84    {
85        return $this->application;
86    }
87
88    /**
89     * Get the application (alias)
90     *
91     * @return ?Application
92     */
93    public function application(): ?Application
94    {
95        return $this->application;
96    }
97
98    /**
99     * Has application
100     *
101     * @return bool
102     */
103    public function hasApplication(): bool
104    {
105        return ($this->application !== null);
106    }
107
108    /**
109     * Add queue
110     *
111     * @param  Queue $queue
112     * @return Worker
113     */
114    public function addQueue(Queue $queue): Worker
115    {
116        $this->queues[$queue->getName()] = $queue;
117        return $this;
118    }
119
120    /**
121     * Add queues
122     *
123     * @param  array $queues
124     * @return Worker
125     */
126    public function addQueues(array $queues): Worker
127    {
128        foreach ($queues as $queue) {
129            $this->addQueue($queue);
130        }
131        return $this;
132    }
133
134    /**
135     * Get queues
136     *
137     * @return array
138     */
139    public function getQueues(): array
140    {
141        return $this->queues;
142    }
143
144    /**
145     * Get queue
146     *
147     * @param  string $queue
148     * @return ?Queue
149     */
150    public function getQueue(string $queue): ?Queue
151    {
152        return $this->queues[$queue] ?? null;
153    }
154
155    /**
156     * Has queue
157     *
158     * @param  string $queue
159     * @return bool
160     */
161    public function hasQueue(string $queue): bool
162    {
163        return (isset($this->queues[$queue]));
164    }
165
166    /**
167     * Work next job in queue
168     *
169     * @param  string $queueName
170     * @return ?AbstractJob
171     */
172    public function work(string $queueName): ?AbstractJob
173    {
174        $job = null;
175
176        if (isset($this->queues[$queueName])) {
177            $job = $this->queues[$queueName]->work($this->application);
178        }
179
180        return $job;
181    }
182
183    /**
184     * Work next job across in all queues
185     *
186     * @return array
187     */
188    public function workAll(): array
189    {
190        $jobs = [];
191        foreach ($this->queues as $queueName => $queue) {
192            $jobs[$queueName] = $queue->work($this->application);
193        }
194        return $jobs;
195    }
196
197    /**
198     * Run next scheduled task in queue
199     *
200     * @param  string $queueName
201     * @return array
202     */
203    public function run(string $queueName): array
204    {
205        $tasks = [];
206        if (isset($this->queues[$queueName])) {
207            $tasks[$queueName] = $this->queues[$queueName]->run($this->application);
208        }
209        return $tasks;
210    }
211
212    /**
213     * Run next scheduled task across in all queues
214     *
215     * @return array
216     */
217    public function runAll(): array
218    {
219        $tasks = [];
220        foreach ($this->queues as $queueName => $queue) {
221            $tasks[$queueName] = $queue->run($this->application);
222        }
223        return $tasks;
224    }
225
226    /**
227     * Clear jobs from queue
228     *
229     * @param  string $queueName
230     * @return Worker
231     */
232    public function clear(string $queueName): Worker
233    {
234        if (isset($this->queues[$queueName])) {
235            $this->queues[$queueName]->clear();
236        }
237        return $this;
238    }
239
240    /**
241     * Clear failed jobs from queue
242     *
243     * @param  string $queueName
244     * @return Worker
245     */
246    public function clearFailed(string $queueName): Worker
247    {
248        if (isset($this->queues[$queueName])) {
249            $this->queues[$queueName]->clearFailed();
250        }
251        return $this;
252    }
253
254    /**
255     * Clear tasks from queue
256     *
257     * @param  string $queueName
258     * @return Worker
259     */
260    public function clearTasks(string $queueName): Worker
261    {
262        if (isset($this->queues[$queueName])) {
263            $this->queues[$queueName]->clearTasks();
264        }
265        return $this;
266    }
267
268    /**
269     * Clear all jobs from queues
270     *
271     * @return Worker
272     */
273    public function clearAll(): Worker
274    {
275        foreach ($this->queues as $queue) {
276            $queue->clear();
277        }
278        return $this;
279    }
280
281    /**
282     * Clear all failed jobs from queues
283     *
284     * @return Worker
285     */
286    public function clearAllFailed(): Worker
287    {
288        foreach ($this->queues as $queue) {
289            $queue->clearFailed();
290        }
291        return $this;
292    }
293
294    /**
295     * Clear all tasks from queues
296     *
297     * @return Worker
298     */
299    public function clearAllTasks(): Worker
300    {
301        foreach ($this->queues as $queue) {
302            $queue->clearTasks();
303        }
304        return $this;
305    }
306
307    /**
308     * Register a queue with the worker
309     *
310     * @param  string $name
311     * @param  mixed $value
312     * @return void
313     */
314    public function __set(string $name, mixed $value): void
315    {
316        $this->addQueue($value);
317    }
318
319    /**
320     * Get a queue
321     *
322     * @param  string $name
323     * @return ?Queue
324     */
325    public function __get(string $name): ?Queue
326    {
327        return $this->getQueue($name);
328    }
329
330    /**
331     * Determine if a queue is registered with the worker object
332     *
333     * @param  string $name
334     * @return bool
335     */
336    public function __isset(string $name): bool
337    {
338        return isset($this->queues[$name]);
339    }
340
341    /**
342     * Unset a queue with the worker
343     *
344     * @param  string $name
345     * @return void
346     */
347    public function __unset(string $name): void
348    {
349        if (isset($this->queues[$name])) {
350            unset($this->queues[$name]);
351        }
352    }
353
354    /**
355     * Set a queue with the worker
356     *
357     * @param  mixed $offset
358     * @param  mixed $value
359     * @return void
360     */
361    public function offsetSet(mixed $offset, mixed $value): void
362    {
363        $this->__set($offset, $value);
364    }
365
366    /**
367     * Get a queue
368     *
369     * @param  mixed $offset
370     * @return ?Queue
371     */
372    public function offsetGet(mixed $offset): ?Queue
373    {
374        return $this->__get($offset);
375    }
376
377    /**
378     * Determine if a queue is registered with the worker object
379     *
380     * @param  mixed $offset
381     * @return bool
382     */
383    public function offsetExists(mixed $offset): bool
384    {
385        return $this->__isset($offset);
386    }
387
388    /**
389     * Unset a queue from the worker
390     *
391     * @param  string $offset
392     * @return void
393     */
394    public function offsetUnset(mixed $offset): void
395    {
396        $this->__unset($offset);
397    }
398
399    /**
400     * Return count
401     *
402     * @return int
403     */
404    public function count(): int
405    {
406        return count($this->queues);
407    }
408
409    /**
410     * Get iterator
411     *
412     * @return ArrayIterator
413     */
414    public function getIterator(): ArrayIterator
415    {
416        return new ArrayIterator($this->queues);
417    }
418
419}