Passed
Push — main ( 5be1cc...a3fefa )
by Dylan
01:51
created

CurlResponse::getJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Lifeboat\SDK;
4
5
/**
6
 * Class CurlResponse
7
 * 
8
 * Wrapper class for responses from the API / Oauth service
9
 * 
10
 * @package Lifeboat\SDK
11
 */
12
class CurlResponse
13
{
14
15
    private $http_code  = 0;
16
    private $result     = '';
17
18
    public function __construct($http_code, $result)
19
    {
20
        $this->http_code    = $http_code;
21
        $this->result       = $result;
22
    }
23
24
    public function isValid()
25
    {
26
        return $this->http_code > 199 && $this->http_code < 300;
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    public function getHTTPCode()
33
    {
34
        return $this->http_code;
35
    }
36
37
    /**
38
     * @return array|null
39
     */
40
    public function getJSON(): ?array
41
    {
42
        $data = json_decode($this->result, true);
43
        if (!is_array($data)) return null;
44
        
45
        return $data;
46
    }
47
48
    /**
49
     * @return bool
50
     */
51
    public function isJSON(): bool
52
    {
53
        return !is_null($this->getJSON());
54
    }
55
56
    public function getError()
57
    {
58
        return (count($this->getErrors())) ? $this->getErrors()[0]['error'] : $this->getRaw();
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getErrors(): array
65
    {
66
        return ($this->isJSON()) ? $this->getJSON()->errors : [['error' => $this->getRaw(), 'field' => '']];
67
    }
68
69
    public function getRaw()
70
    {
71
        return $this->result;
72
    }
73
}