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

GetWorkflowInstancesDetailCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 1
b 0
f 0
dl 0
loc 84
ccs 27
cts 29
cp 0.931
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroup() 0 3 1
A getFilters() 0 7 1
A __construct() 0 4 1
A getResponseParser() 0 3 1
B parseResponse() 0 29 7
A getAction() 0 3 1
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 1
    public function __construct(WorkflowInstance $workflowInstance)
31
    {
32
//        dump($workflowInstance);
33 1
        $this->workflowInstance = $workflowInstance;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 1
    public function getGroup(): string
40
    {
41 1
        return 'instance';
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 1
    public function getAction(): string
48
    {
49 1
        return 'query';
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55 1
    public function getFilters(): array
56
    {
57
        $filters = [
58 1
            'id' => $this->workflowInstance->getId(),
59
        ];
60
61 1
        return $filters;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1
    public function getResponseParser()
68
    {
69 1
        return $this;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 1
    public function parseResponse($responseLine, $responseData)
76
    {
77
78 1
        if (!(isset($responseData['workflow']))) {
79
            return false;
80
        }
81
82 1
        $subjobs = $responseData['workflow']['subjobs'];
83 1
        $jobInstances = new ArrayCollection([]);
84 1
        foreach ($subjobs as $subjob) {
85 1
            $taskInstances = new ArrayCollection([]);
86 1
            foreach ($subjob['tasks'] as $tasks) {
87 1
                $task = $tasks['@attributes'];
88
89 1
                if (isset($task['execution_time'])) {
90
                    $task['execution_time'] = new \DateTime($task['execution_time']);
91
                }
92 1
                foreach ($task as $key => $val) {
93 1
                    if (ctype_digit($val)) {
94 1
                        $task[$key] = (int) $task[$key];
95
                    }
96
                }
97 1
                $taskInstances[] = new TaskInstance($task);
98
            }
99 1
            $jobInstances[] = new JobInstance($taskInstances);
100
        }
101 1
        $this->workflowInstance->setJobInstances($jobInstances);
102
103 1
        return $this->workflowInstance;
104
    }
105
}
106