Completed
Branch master (450f26)
by Valentin
01:46
created

GetWorkflowInstancesCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 45
c 1
b 0
f 0
dl 0
loc 109
ccs 41
cts 41
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseParser() 0 3 1
B parseResponse() 0 30 9
A __construct() 0 5 1
A getGroup() 0 3 2
A getFilters() 0 15 2
A getAction() 0 3 2
1
<?php
2
3
namespace Pheanstalk\Command;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Pheanstalk\Structure\Workflow;
7
use Pheanstalk\Structure\WorkflowInstance;
8
9
/**
10
 * The 'GetWorkflow' command.
11
 *
12
 * Retrieve a workflow by its id, if there is no workflow for the id given in the construct, returns false
13
 *
14
 * @author  Valentin Corre
15
 * @package Pheanstalk
16
 * @license http://www.opensource.org/licenses/mit-license.php
17
 */
18
class GetWorkflowInstancesCommand extends AbstractCommand implements \Pheanstalk\ResponseParser
19
{
20
21
    const FILTER_EXECUTING = 'EXECUTING';
22
    const FILTER_TERMINATED = 'TERMINATED';
23
24
    const FILTERS = [
25
        self::FILTER_EXECUTING,
26
        self::FILTER_TERMINATED
27
    ];
28
29
    /** @var Workflow $workflow */
30
    private $workflow;
31
32
    /** @var string $status */
33
    private $status;
34
35
    /** @var int $page */
36
    private $page;
37
38
    /**
39
     * GetWorkflowCommand constructor.
40
     *
41
     * @param Workflow $workflow
42
     */
43 1
    public function __construct(?Workflow $workflow, $status = self::FILTER_EXECUTING, int $page = 1)
44
    {
45 1
        $this->workflow = $workflow;
46 1
        $this->status = $status;
47 1
        $this->page = $page;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 1
    public function getGroup(): string
54
    {
55 1
        return ($this->status === self::FILTER_EXECUTING) ? 'status' : 'instances';
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 1
    public function getAction(): string
62
    {
63 1
        return ($this->status === self::FILTER_EXECUTING) ? 'query' : 'list';
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 1
    public function getFilters(): array
70
    {
71 1
        $limit = 30;
72 1
        $offset = ($this->page - 1) * $limit;
73
        $filters = [
74 1
            'type' => "workflows",
75 1
            'filter_status' => $this->status,
76 1
            'offset' => $offset,
77 1
            'limit' => $limit
78
        ];
79 1
        if (!empty($this->workflow)) {
80 1
            $filters['filter_workflow'] = $this->workflow->getName();
81
        }
82
83 1
        return $filters;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 1
    public function getResponseParser()
90
    {
91 1
        return $this;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97 1
    public function parseResponse($responseLine, $responseData)
98
    {
99
100 1
        if (!(isset($responseData['workflow']))) {
101 1
            return new ArrayCollection([]);
102
        }
103
104 1
        $instances = $responseData['workflow'];
105 1
        $instances = isset($instances['tags']) ? [$instances['@attributes']] : $instances;
106 1
        $workflowInstances = new ArrayCollection([]);
107 1
        foreach ($instances as $instance) {
108 1
            $instance = $instance['@attributes'] ?? $instance;
109 1
            if (isset($instance['start_time'])) {
110 1
                $instance['start_time'] = new \DateTime($instance['start_time']);
111
            }
112 1
            if (isset($instance['end_time'])) {
113 1
                $instance['end_time'] = new \DateTime($instance['end_time']);
114
            }
115 1
            foreach ($instance as $key => $val) {
116 1
                if (ctype_digit($val)) {
117 1
                    $instance[$key] = (int) $instance[$key];
118
                }
119
            }
120 1
            $workflowInstances[] = new WorkflowInstance($instance);
121
        }
122 1
        $collection['rows'] = (int) (isset($responseData['@attributes']['rows'])) ?
0 ignored issues
show
Comprehensibility Best Practice introduced by
$collection was never initialized. Although not strictly required by PHP, it is generally a good practice to add $collection = array(); before regardless.
Loading history...
123 1
            (int) $responseData['@attributes']['rows'] : $workflowInstances->count();
124 1
        $collection['page'] = $this->page;
125 1
        $collection['workflow_instances'] = $workflowInstances;
126 1
        return new ArrayCollection($collection);
127
    }
128
}
129