Completed
Pull Request — master (#7)
by Louis
54:05 queued 33:43
created

Response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 64
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getCode() 0 4 1
A getMessage() 0 4 1
A getData() 0 4 1
A getResponse() 0 4 1
A getPropertyFromData() 0 4 2
1
<?php
2
3
namespace EtherpadLite;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
class Response
8
{
9
    const CODE_OK = 0;
10
    const CODE_WRONG_PARAMETERS = 1;
11
    const CODE_INTERNAL_ERROR = 2;
12
    const CODE_NO_SUCH_FUNCTION = 3;
13
    const CODE_NO_OR_WRONG_API_KEY = 4;
14
15
    /** @var \stdClass */
16
    private $data;
17
18
    /**
19
     * @param ResponseInterface $response
20
     */
21 4
    public function __construct(ResponseInterface $response)
22
    {
23 4
        if ($response->getStatusCode() === 200) {
24 4
            $this->data = \GuzzleHttp\json_decode($response->getBody(), true);
25 4
        } else {
26 1
            $this->data = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<stdClass> of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
        }
28 4
    }
29
30
    /**
31
     * @return string|null
32
     */
33 4
    public function getCode()
34
    {
35 4
        return $this->getPropertyFromData('code');
36
    }
37
38
    /**
39
     * @return string|null
40
     */
41 4
    public function getMessage()
42
    {
43 4
        return $this->getPropertyFromData('message');
44
    }
45
46
    /**
47
     * @return string|null
48
     */
49 4
    public function getData()
50
    {
51 4
        return $this->getPropertyFromData('data');
52
    }
53
54
    /**
55
     * @return array
56
     */
57 4
    public function getResponse()
58
    {
59 4
        return $this->data;
60
    }
61
62
    /**
63
     * @param string $key
64
     * @return mixed
65
     */
66 4
    private function getPropertyFromData($key)
67
    {
68 4
        return isset($this->data[$key]) ? $this->data[$key] : null;
69
    }
70
}
71