Completed
Push — master ( 4a3b8a...43ff8f )
by Valentin
03:38
created

PeekCommand::getMostRecentFromArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 3
nop 1
crap 4
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|bool
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 1
            return false;
68
        }
69 1
        $responseData = $responseData['workflow'];
70 1
        if (isset($responseData['@attributes'])) {
71 1
            return $this->constructResponse($responseData['@attributes']);
72
        }
73 1
        $responseData = array_column($responseData, '@attributes');
74 1
        $mostRecent = $this->getMostRecentFromArray($responseData);
75 1
        return $this->constructResponse($mostRecent);
76
    }
77
78
    /**
79
     * @param $response
80
     *
81
     * @return array
82
     */
83 1
    protected function constructResponse($response)
84
    {
85
        return [
86 1
            Response::RESPONSE_FOUND,
87
            [
88 1
                'id'      => (int) $response['id'],
89 1
                'jobdata' => $response,
90
            ]
91
        ];
92
    }
93
94
    /**
95
     * @param array $datas
96
     *
97
     * @return array
98
     */
99 1
    protected function getMostRecentFromArray(array $datas)
100
    {
101 1
        $mostRecent = [];
102 1
        foreach ($datas as $data) {
103 1
            $curDate = strtotime($data['start_time']);
104 1
            if (!isset($mostRecent['start_time']) || $curDate < strtotime($mostRecent['start_time'])) {
105 1
                $mostRecent = $data;
106
            }
107
        }
108 1
        return $mostRecent;
109
    }
110
}
111