Passed
Push — master ( 18af30...0e6a78 )
by Mathias
45:31 queued 28:39
created

MongoQueueListController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 28
dl 0
loc 66
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A listAction() 0 42 5
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2019 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Queue\Controller;
12
13
use Core\Queue\MongoQueue;
14
use SlmQueue\Queue\QueuePluginManager;
15
use Zend\Mvc\Console\Controller\AbstractConsoleController;
16
17
/**
18
 * Console controller for list queue jobs.
19
 * 
20
 * @author Mathias Gelhausen <[email protected]>
21
 */
22
class MongoQueueListController extends AbstractConsoleController
23
{
24
    /**
25
     * The queue plugin manager
26
     *
27
     * @var QueuePluginManager
28
     */
29
    private $queuePluginManager;
30
31
    /**
32
     * MongoQueueListController constructor.
33
     *
34
     * @param QueuePluginManager $queuePluginManager
35
     */
36 3
    public function __construct(QueuePluginManager $queuePluginManager)
37
    {
38 3
        $this->queuePluginManager = $queuePluginManager;
39 3
    }
40
41
    /**
42
     * List jobs from a queue
43
     *
44
     * @return string
45
     */
46 3
    public function listAction()
47
    {
48 3
        $queue   = $this->params()->fromRoute('queue');
49 3
        $queue   = $this->queuePluginManager->get($queue);
50
51 3
        if (!$queue instanceOf MongoQueue) {
52 1
            return 'Unsupported queue type.';
53
        }
54
55 2
        $statusMap = function($stat) {
56 2
            static $map = [
57
                'pending' => MongoQueue::STATUS_PENDING,
58
                'running' => MongoQueue::STATUS_RUNNING,
59
                'failed'  => MongoQueue::STATUS_FAILED,
60
            ];
61
62 2
            return $map[$stat] ?? $map['pending'];
63 2
        };
64
65
        $console = $this->getConsole();
66
        $jobs    = $queue->listing([
67
            'limit'  => (int) $this->params()->fromRoute('limit', 0),
68
            'status' => $statusMap($this->params()->fromRoute('status')),
69
        ]);
70
71
        if (!$jobs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $jobs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
72
            return 'Queue is empty.';
73
        }
74
75
        $lineTmpl = '%-20s %s';
76
        foreach ($jobs as $job) {
77
            $console->writeLine(get_class($job['job']) . ' [ ' . $job['job']->getId() . ' ]');
78
79
            foreach (['created', 'executed', 'scheduled'] as $key) {
80
                $console->writeLine(sprintf($lineTmpl, ucFirst($key), $job[$key]->toDateTime()->format('Y-m-d H:i:s')));
81
            }
82
            $console->writeLine(sprintf($lineTmpl, 'Tries', $job['tried']));
83
            $console->writeLine();
84
            $console->writeLine();
85
        }
86
87
        return '';
88
    }
89
90
}
91