Response::process_body()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DrewM\Drip;
4
5
class Response
6
{
7
    public $status  = null;
8
    public $error   = null;
9
    public $message = null;
10
11
    protected $data = [];
12
13
    public function __construct($meta, $body)
14
    {
15
        $this->process_meta($meta);
16
        $this->process_body($body);
17
        $this->handle_errors();
18
    }
19
20
    protected function process_meta($meta)
21
    {
22
        if (isset($meta['http_code'])) {
23
            $this->status = (int) $meta['http_code'];
24
        }
25
    }
26
27
    protected function process_body($body)
28
    {
29
        $decoded_body = json_decode($body, true);
30
        if (is_array($decoded_body)) {
31
            $this->data = $decoded_body;
32
        }
33
    }
34
35
    protected function handle_errors()
36
    {
37
        if (is_array($this->data) && isset($this->data['errors'])) {
38
            $this->error   = $this->data['errors'][0]['code'];
39
            $this->message = $this->data['errors'][0]['message'];
40
        }
41
    }
42
43
    public function __get($name)
44
    {
45
        if (is_array($this->data) && isset($this->data[$name])) {
46
            return $this->data[$name];
47
        }
48
49
        return false;
50
    }
51
52
    public function get()
53
    {
54
        return $this->data;
55
    }
56
57
    public function __toString()
58
    {
59
        return print_r($this->data, true);
60
    }
61
62
    public function __debugInfo()
63
    {
64
        return $this->data;
65
    }
66
}
67