Completed
Push — master ( c1b3bb...db4538 )
by Jared
03:22
created

JCResponse   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 71
c 0
b 0
f 0
ccs 12
cts 14
cp 0.8571
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A status() 0 8 2
A body() 0 4 1
A headers() 0 8 2
A json() 0 8 2
A success() 0 8 2
A hasResponse() 0 4 1
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
}