Response   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 0
dl 0
loc 62
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process_meta() 0 6 2
A process_body() 0 7 2
A handle_errors() 0 7 3
A __get() 0 8 3
A get() 0 4 1
A __toString() 0 4 1
A __debugInfo() 0 4 1
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