JCResponse::json()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 31/05/2017
6
 * Time: 09:50
7
 */
8
9
namespace JC\HttpClient;
10
11
12
use GuzzleHttp;
13
use Psr\Http\Message\ResponseInterface;
14
15
class JCResponse implements JCResponseInterface
16
{
17
18
    /**
19
     * @var ResponseInterface
20
     */
21
    public $response;
22
23
    /**
24
     * @var string
25
     */
26
    protected $body;
27
28
    /**
29
     * JCResponse constructor.
30
     * @param $response
31
     */
32 7
    public function __construct($response)
33
    {
34 7
        if ($response) {
35 7
            $this->response = $response;
36 7
            $this->body = $this->response->getBody()->getContents();
37
        }
38
    }
39 7
40
    public function status()
41 7
    {
42
        if ($this->hasResponse()) {
43
            return $this->response->getStatusCode();
44 7
        } else {
45
            return false;
46 7
        }
47
    }
48
49 1
    public function body()
50
    {
51 1
        return $this->body;
52
    }
53
54 6
    public function headers()
55
    {
56 6
        if ($this->hasResponse()) {
57
            return $this->response->getHeaders();
58
        } else {
59
            return null;
60
        }
61
    }
62
63
    public function json()
64
    {
65
        if ($this->hasResponse()) {
66
            return GuzzleHttp\json_decode($this->body());
67
        } else {
68
            return null;
69
        }
70
    }
71
72
    public function success()
73
    {
74
        if ($this->hasResponse()) {
75
            return $this->response->getStatusCode() < 300;
76
        } else {
77
            return false;
78
        }
79
    }
80
81
    protected function hasResponse()
82
    {
83
        return !is_null($this->response);
84
    }
85
}