KeyValueResponse::setBody()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 4
nc 2
nop 1
crap 30
1
<?php
2
namespace Disque\Command\Response;
3
4
class KeyValueResponse extends BaseResponse implements ResponseInterface
5
{
6
    /**
7
     * Set response body
8
     *
9
     * @param mixed $body Response body
10
     * @return void
11
     * @throws InvalidResponseException
12
     */
13
    public function setBody($body)
14
    {
15
        if ($body !== false && (empty($body) || !is_array($body) || (count($body) % 2) !== 0)) {
16
            throw new InvalidResponseException($this->command, $body);
17
        }
18
        parent::setBody($body);
19
    }
20
21
    /**
22
     * Parse response
23
     *
24
     * @return array Parsed response
25
     */
26
    public function parse()
27
    {
28
        if ($this->body === false) {
29
            return null;
30
        }
31
32
        $result = [];
33
        $key = null;
34
        foreach ($this->body as $value) {
35
            if (!is_null($key)) {
36
                $result[$key] = $value;
37
                $key = null;
38
            } else {
39
                $key = $value;
40
            }
41
        }
42
43
        return $result;
44
    }
45
}