GetWorkflowInstancesCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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