Completed
Push — master ( 16c0e5...ced0af )
by Fede
16:40 queued 01:40
created

Response::getError()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9

Importance

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