KeyValueResponse   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 0
loc 42
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setBody() 0 7 5
A parse() 0 19 4
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
}