HelloResponse::setBody()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 20
1
<?php
2
namespace Disque\Command\Response;
3
4
use Disque\Command\Argument\ArrayChecker;
5
6
class HelloResponse extends BaseResponse implements ResponseInterface
7
{
8
    /**
9
     * Array keys
10
     */
11
    const NODE_ID = 'id';
12
    const NODE_HOST = 'host';
13
    const NODE_PORT = 'port';
14
    const NODE_VERSION = 'version';
15
    const NODE_PRIORITY = 'priority';
16
    const NODES = 'nodes';
17
18
    /**
19
     * Position indexes in the Disque response
20
     */
21
    const POS_VERSION = 0;
22
    const POS_ID = 1;
23
    const POS_NODES_START = 2;
24
25
    const POS_NODE_ID = 0;
26
    const POS_NODE_HOST = 1;
27
    const POS_NODE_PORT = 2;
28
    const POS_NODE_PRIORITY = 3;
29
30
    use ArrayChecker;
31
32
    /**
33
     * Set response body
34
     *
35
     * @param mixed $body Response body
36
     * @return void
37
     * @throws InvalidResponseException
38
     */
39
    public function setBody($body)
40
    {
41
        if (!$this->checkFixedArray($body, 3, true)) {
42
            throw new InvalidResponseException($this->command, $body);
43
        }
44
        foreach (array_slice($body, 2) as $node) {
45
            if (!$this->checkFixedArray($node, 4)) {
46
                throw new InvalidResponseException($this->command, $body);
47
            }
48
        }
49
        parent::setBody($body);
50
    }
51
52
    /**
53
     * Parse response
54
     *
55
     * @return array Parsed response
56
     */
57
    public function parse()
58
    {
59
        $nodes = [];
60
        foreach (array_slice($this->body, self::POS_NODES_START) as $node) {
61
            $nodes[] = [
62
                self::NODE_ID => $node[self::POS_NODE_ID],
63
                self::NODE_HOST => $node[self::POS_NODE_HOST],
64
                self::NODE_PORT => $node[self::POS_NODE_PORT],
65
                self::NODE_PRIORITY => $node[self::POS_NODE_PRIORITY]
66
            ];
67
        }
68
69
        return [
70
            self::NODE_VERSION => $this->body[self::POS_VERSION],
71
            self::NODE_ID => $this->body[self::POS_ID],
72
            self::NODES => $nodes
73
        ];
74
    }
75
76
}
77