Completed
Push — master ( 06d8dc...4a3b8a )
by Valentin
04:16
created

GetWorkflowInstancesDetailCommand::parseResponse()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0084

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 29
ccs 17
cts 18
cp 0.9444
rs 8.8333
cc 7
nc 9
nop 2
crap 7.0084
1
<?php
2
3
namespace Pheanstalk\Command;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Pheanstalk\Structure\JobInstance;
7
use Pheanstalk\Structure\TaskInstance;
8
use Pheanstalk\Structure\Workflow;
9
use Pheanstalk\Structure\WorkflowInstance;
10
11
/**
12
 * The 'GetWorkflow' command.
13
 *
14
 * Retrieve a workflow by its id, if there is no workflow for the id given in the construct, returns false
15
 *
16
 * @author  Valentin Corre
17
 * @package Pheanstalk
18
 * @license http://www.opensource.org/licenses/mit-license.php
19
 */
20
class GetWorkflowInstancesDetailCommand extends GetWorkflowInstancesCommand
21
{
22
    /** @var WorkflowInstance $workflowInstance */
23
    private $workflowInstance;
24
25
    /**
26
     * GetWorkflowCommand constructor.
27
     *
28
     * @param Workflow $workflow
29
     */
30 3
    public function __construct(WorkflowInstance $workflowInstance)
31
    {
32
//        dump($workflowInstance);
33 3
        $this->workflowInstance = $workflowInstance;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 3
    public function getGroup(): string
40
    {
41 3
        return 'instance';
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 3
    public function getAction(): string
48
    {
49 3
        return 'query';
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55 3
    public function getFilters(): array
56
    {
57
        $filters = [
58 3
            'id' => $this->workflowInstance->getId(),
59
        ];
60
61 3
        return $filters;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 3
    public function getResponseParser()
68
    {
69 3
        return $this;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 3
    public function parseResponse($responseLine, $responseData)
76
    {
77
78 3
        if (!(isset($responseData['workflow']))) {
79
            return false;
80
        }
81
82 3
        $subjobs = $responseData['workflow']['subjobs'];
83 3
        $jobInstances = new ArrayCollection([]);
84 3
        foreach ($subjobs as $subjob) {
85 3
            $taskInstances = new ArrayCollection([]);
86 3
            foreach ($subjob['tasks'] as $tasks) {
87 3
                $task = $tasks['@attributes'];
88
89 3
                if (isset($task['execution_time'])) {
90 3
                    $task['execution_time'] = new \DateTime($task['execution_time']);
91
                }
92 3
                foreach ($task as $key => $val) {
93 3
                    if (ctype_digit($val)) {
94 3
                        $task[$key] = (int) $task[$key];
95
                    }
96
                }
97 3
                $taskInstances[] = new TaskInstance($task);
98
            }
99 3
            $jobInstances[] = new JobInstance($taskInstances);
100
        }
101 3
        $this->workflowInstance->setJobInstances($jobInstances);
102
103 3
        return $this->workflowInstance;
104
    }
105
}
106