Completed
Push — master ( ced0af...903d70 )
by Fede
03:07
created

Response::isError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MercadoPago\Http;
4
5
use ArrayAccess;
6
7
class Response
8
{
9
    /**
10
     * @var int
11
     */
12
    private $statusCode = 0;
13
14
    /**
15
     * @var array
16
     */
17
    private $data = [];
18
19
    /**
20
     * Response constructor.
21
     * @param int $statusCode
22
     * @param string $json
23
     */
24 66
    public function __construct($statusCode, $json = '{}')
25
    {
26 66
        $this->data = (array) json_decode($json, true);
27 66
        $this->statusCode = (int) $statusCode;
28 66
    }
29
30
    /**
31
     * @return null|string
32
     */
33 24
    public function getError()
34
    {
35 24
        if ($this->statusCode < 400) {
36 3
            return null;
37
        }
38
39 21
        $messagePieces = [$this->get('message')];
40 21
        if ($this->get('cause')) {
41 6
            if ($this->get('cause.code') && $this->get('cause.description')) {
42 3
                $messagePieces[] = $this->get('cause.code') . ': ' . $this->get('cause.description');
43 5
            } elseif (is_array($this->get('cause'))) {
44 3
                foreach ($this->get('cause') as $causes) {
45 3
                    if (is_array($causes)) {
46 3
                        foreach ($causes as $cause) {
47 3
                            $messagePieces[] = $cause['code'] . ': ' . $cause['description'];
48 2
                        }
49 2
                    } else {
50 1
                        $messagePieces[] = $causes['code'] . ': ' . $causes['description'];
51
                    }
52 2
                }
53 2
            }
54 4
        }
55
56 21
        return join(' - ', $messagePieces);
57
    }
58
59
    /**
60
     * @return int
61
     */
62 33
    public function getStatusCode()
63
    {
64 33
        return $this->statusCode;
65
    }
66
67
    /**
68
     * @return array
69
     */
70 30
    public function getData()
71
    {
72 30
        return $this->data;
73
    }
74
75
    /**
76
     * @return bool
77
     */
78 51
    public function isError()
79
    {
80 51
        return $this->statusCode >= 400;
81
    }
82
83
    /**
84
     * @param $key
85
     * @param null $default
86
     * @return array|mixed|null
87
     */
88 36
    public function get($key, $default = null)
89
    {
90 36
        $keys = explode('.', (string)$key);
91 36
        $array = &$this->data;
92 36
        foreach ($keys as $key) {
93 36
            if (!$this->exists($array, $key)) {
94 6
                return $default;
95
            }
96 36
            $array = &$array[$key];
97 24
        }
98
99 36
        return $array;
100
    }
101
102
    /**
103
     * @param $array
104
     * @param $key
105
     * @return bool
106
     */
107 36
    public function exists($array, $key)
108
    {
109 36
        if ($array instanceof ArrayAccess) {
110
            return isset($array[$key]);
111 36
        } elseif (!is_array($array)) {
112
            return false;
113
        }
114
115 36
        return array_key_exists($key, $array);
116
    }
117
}
118