CurlResponse   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getJSON() 0 6 2
A getHTTPCode() 0 3 1
A isValid() 0 3 2
A isJSON() 0 3 1
A __construct() 0 4 1
A getError() 0 3 2
A getErrors() 0 4 3
A getRaw() 0 3 1
1
<?php
2
3
namespace Lifeboat;
4
5
/**
6
 * Class CurlResponse
7
 *
8
 * Wrapper class for responses from the API / Oauth service
9
 *
10
 * @package Lifeboat
11
 */
12
class CurlResponse
13
{
14
15
    private $http_code;
16
    private $result;
17
18
    public function __construct(int $http_code, string $result)
19
    {
20
        $this->http_code    = $http_code;
21
        $this->result       = $result;
22
    }
23
24
    public function isValid(): bool
25
    {
26
        return $this->http_code > 199 && $this->http_code < 300;
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    public function getHTTPCode(): int
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
    /**
57
     * @return string
58
     */
59
    public function getError(): string
60
    {
61
        return (count($this->getErrors())) ? $this->getErrors()[0]['error'] : $this->getRaw();
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getErrors(): array
68
    {
69
        $errors = ($this->isJSON()) ? $this->getJSON()['errors'] : [['error' => $this->getRaw(), 'field' => '']];
70
        return is_array($errors) ? $errors : [];
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getRaw(): string
77
    {
78
        return $this->result;
79
    }
80
}
81