CursorResponse::parse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
namespace Disque\Command\Response;
3
4
use Disque\Command\Argument\ArrayChecker;
5
6
abstract class CursorResponse extends BaseResponse implements ResponseInterface
7
{
8
    use ArrayChecker;
9
10
    /**
11
     * Set response body
12
     *
13
     * @param mixed $body Response body
14
     * @return void
15
     * @throws InvalidResponseException
16
     */
17
    public function setBody($body)
18
    {
19
        if (
20
            !$this->checkFixedArray($body, 2) ||
21
            !is_numeric($body[0]) ||
22
            !is_array($body[1])
23
        ) {
24
            throw new InvalidResponseException($this->command, $body);
25
        }
26
27
        parent::setBody($body);
28
    }
29
30
    /**
31
     * Parse response
32
     *
33
     * @return array Parsed response. Indexed array with `finished', 'nextCursor`, `queues`
34
     */
35
    public function parse()
36
    {
37
        $nextCursor = (int) $this->body[0];
38
        return array_merge([
39
            'finished' => (0 === $nextCursor),
40
            'nextCursor' => $nextCursor,
41
        ], $this->parseBody((array) $this->body[1]));
42
    }
43
44
    /**
45
     * Parse main body
46
     *
47
     * @param array $body Body
48
     * @return array Parsed body
49
     */
50
    abstract protected function parseBody(array $body);
51
}