1
|
|
|
<?php |
2
|
|
|
namespace Disque\Command\Response; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Parse a Disque response of GETJOB with the argument WITHCOUNTERS |
6
|
|
|
*/ |
7
|
|
|
class JobsWithCountersResponse extends JobsWithQueueResponse implements ResponseInterface |
8
|
|
|
{ |
9
|
|
|
const KEY_NACKS = 'nacks'; |
10
|
|
|
const KEY_ADDITIONAL_DELIVERIES = 'additional-deliveries'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* GETJOB called with WITHCOUNTERS returns a 7-member array for each job. |
14
|
|
|
* The value on (zero-based) position #3 is always the string "nacks", the |
15
|
|
|
* value on position #5 is always the string "additional-deliveries". |
16
|
|
|
* |
17
|
|
|
* We want to remove these values from the response. |
18
|
|
|
*/ |
19
|
|
|
const DISQUE_RESPONSE_KEY_NACKS = 3; |
20
|
|
|
const DISQUE_RESPONSE_KEY_DELIVERIES = 5; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
// Note: The order of these calls is important as $jobDetails must |
25
|
|
|
// match the order of the Disque response rows. Nacks go at the end. |
26
|
|
|
parent::__construct(); |
27
|
|
|
$this->jobDetails = array_merge( |
28
|
|
|
$this->jobDetails, |
29
|
|
|
[ |
30
|
|
|
self::KEY_NACKS, |
31
|
|
|
self::KEY_ADDITIONAL_DELIVERIES |
32
|
|
|
] |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function setBody($body) |
40
|
|
|
{ |
41
|
|
|
if (is_null($body)) { |
42
|
|
|
$body = []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!is_array($body)) { |
46
|
|
|
throw new InvalidResponseException($this->command, $body); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$jobDetailCount = count($this->jobDetails) + 2; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Remove superfluous strings from the response |
53
|
|
|
* See the comment for the constants defined above |
54
|
|
|
*/ |
55
|
|
|
$filteredBody = array_map(function (array $job) use ($jobDetailCount, $body) { |
56
|
|
|
if (!$this->checkFixedArray($job, $jobDetailCount)) { |
57
|
|
|
throw new InvalidResponseException($this->command, $body); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
unset($job[self::DISQUE_RESPONSE_KEY_NACKS]); |
61
|
|
|
unset($job[self::DISQUE_RESPONSE_KEY_DELIVERIES]); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* We must reindex the array so it's dense (without holes) |
65
|
|
|
* @see Disque\Command\Argument\ArrayChecker::checkFixedArray() |
66
|
|
|
*/ |
67
|
|
|
return array_values($job); |
68
|
|
|
}, $body); |
69
|
|
|
|
70
|
|
|
parent::setBody($filteredBody); |
71
|
|
|
} |
72
|
|
|
} |