Passed
Pull Request — master (#28)
by Valentin
03:35
created

GetScheduleCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 2
b 0
f 0
dl 0
loc 77
ccs 25
cts 25
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAction() 0 3 1
A getFilters() 0 4 1
A __construct() 0 3 1
A getGroup() 0 3 1
A getResponseParser() 0 3 1
A parseResponse() 0 5 1
A createScheduleFromArray() 0 12 1
1
<?php
2
3
namespace Pheanstalk\Command;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Pheanstalk\ResponseParser;
7
use Pheanstalk\Structure\Job;
8
use Pheanstalk\Structure\Schedule;
9
use Pheanstalk\Structure\Task;
10
use Pheanstalk\Structure\TimeSchedule;
11
use Pheanstalk\Structure\Workflow;
12
13
/**
14
 * The 'GetSchedule' command.
15
 *
16
 * Retrieve a scheduled workflow by its id, if there is no workflow for the id given in the construct, returns false
17
 *
18
 * @author  Valentin Corre
19
 * @package Pheanstalk
20
 * @license http://www.opensource.org/licenses/mit-license.php
21
 */
22
class GetScheduleCommand extends AbstractCommand implements ResponseParser
23
{
24
25
    /** @var int $scheduleId */
26
    protected $scheduleId;
27
28
    /**
29
     * GetWorkflowCommand constructor.
30
     *
31
     * @param int $scheduleId
32
     */
33 1
    public function __construct(int $scheduleId)
34
    {
35 1
        $this->scheduleId = $scheduleId;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 1
    public function getGroup(): string
42
    {
43 1
        return 'workflow_schedule';
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49 1
    public function getAction(): string
50
    {
51 1
        return 'get';
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 1
    public function getFilters(): array
58
    {
59
        return [
60 1
            'id' => $this->scheduleId
61
        ];
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 1
        $scheduleDatas = $responseData['workflow_schedule'];
78 1
        $scheduleDatas = $scheduleDatas['@attributes'] ?? $scheduleDatas;
79 1
        return $this->createScheduleFromArray($scheduleDatas);
80
    }
81
82
    /**
83
     * @param array $scheduleDatas
84
     *
85
     * @return Schedule
86
     */
87 1
    protected function createScheduleFromArray($scheduleDatas)
88
    {
89 1
        return (new Schedule(
90 1
            (int) $scheduleDatas['workflow_id'],
91 1
            (new TimeSchedule())->__fromString($scheduleDatas['schedule']),
92 1
            $scheduleDatas['onfailure'],
93 1
            $scheduleDatas['active'],
94 1
            $scheduleDatas['comment'],
95 1
            $scheduleDatas['user'] ?? '',
96 1
            $scheduleDatas['host'] ?? 'localhost',
97 1
            $scheduleDatas['node']
98 1
        ))->setId($this->scheduleId);
99
    }
100
}
101