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

PeekCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 25
c 1
b 0
f 0
dl 0
loc 75
ccs 18
cts 28
cp 0.6429
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A constructResponse() 0 7 1
A getFilters() 0 4 1
A getGroup() 0 3 1
B parseResponse() 0 22 7
A getAction() 0 3 1
A getResponseParser() 0 3 1
1
<?php
2
3
namespace Pheanstalk\Command;
4
5
use Pheanstalk\Exception;
6
use Pheanstalk\Response;
7
use Pheanstalk\ResponseParser;
8
9
/**
10
 * The 'peek', 'peek-ready', 'peek-delayed' and 'peek-buried' commands.
11
 *
12
 * The peek commands let the client inspect a job in the system. There are four
13
 * variations. All but the first (peek) operate only on the currently used tube.
14
 *
15
 * @author  Paul Annesley
16
 * @package Pheanstalk
17
 * @license http://www.opensource.org/licenses/mit-license.php
18
 */
19
class PeekCommand extends AbstractCommand implements ResponseParser
20
{
21
22
    /**
23
     * @inheritDoc
24
     */
25 1
    public function getGroup(): string
26
    {
27 1
        return 'status';
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33 1
    public function getAction(): string
34
    {
35 1
        return 'query';
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41 1
    public function getFilters(): array
42
    {
43
        return [
44 1
            'type' => "workflows"
45
        ];
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 1
    public function getResponseParser()
52
    {
53 1
        return $this;
54
    }
55
56
    /**
57
     * @param string $responseLine
58
     * @param string $responseData
59
     *
60
     * @return array
61
     * @throws Exception\ServerException
62
     */
63 1
    public function parseResponse($responseLine, $responseData)
64
    {
65 1
        unset($responseData['@attributes']);
66 1
        if (!isset($responseData['workflow'])) {
67
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
68
        }
69 1
        $responseData = $responseData['workflow'];
70 1
        if (isset($responseData['@attributes'])) {
71 1
            return $this->constructResponse($responseData['@attributes']);
72
        }
73
        $responseData = array_column($responseData, '@attributes');
74
        $mostRecent = [];
75
        foreach ($responseData as $date) {
76
            $curDate = strtotime($date['start_time']);
77
            if (!isset($mostRecent['start_time']) || $curDate < strtotime($mostRecent['start_time'])) {
78
                $mostRecent = $date;
79
            }
80
        }
81
        if (empty($responseData)) {
82
            return $this->parseResponse($responseLine, $responseData);
83
        }
84
        return $this->constructResponse($mostRecent);
85
    }
86
87 1
    protected function constructResponse($response)
88
    {
89
        return [
90 1
            Response::RESPONSE_FOUND,
91
            [
92 1
                'id'      => (int) $response['id'],
93 1
                'jobdata' => $response,
94
            ]
95
        ];
96
    }
97
}
98