JsonResponse::get()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 2
dl 0
loc 21
rs 9.2222
c 0
b 0
f 0
1
<?php
2
namespace CodeCloud\Bundle\ShopifyBundle\Api\Response;
3
4
use Psr\Http\Message\ResponseInterface as PsrResponse;
5
6
class JsonResponse implements ResponseInterface
7
{
8
    /**
9
     * @var array
10
     */
11
    private $decoded;
12
13
    /**
14
     * @var PsrResponse
15
     */
16
    private $response;
17
18
    /**
19
     * @param PsrResponse $response
20
     */
21
    public function __construct(PsrResponse $response)
22
    {
23
        $this->response = $response;
24
    }
25
26
    /**
27
     * @return PsrResponse
28
     */
29
    public function getHttpResponse()
30
    {
31
        return $this->response;
32
    }
33
34
    /**
35
     * Access elements of the JSON response using dot notation
36
     * @param null $item
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $item is correct as it would always require null to be passed?
Loading history...
37
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
38
     * @return mixed
39
     */
40
    public function get($item = null, $default = null)
41
    {
42
        if (is_null($item)) {
0 ignored issues
show
introduced by
The condition is_null($item) is always true.
Loading history...
43
            return $default;
44
        }
45
46
        $decoded = $this->getDecodedJson();
47
48
        if (array_key_exists($item, $decoded)) {
49
            return $decoded[$item];
50
        }
51
52
        foreach (explode('.', $item) as $segment) {
53
            if (! is_array($decoded) || ! array_key_exists($segment, $decoded)) {
54
                return $default;
55
            }
56
57
            $decoded = $decoded[$segment];
58
        }
59
60
        return $decoded;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function successful()
67
    {
68
        return preg_match('/^2[\d]{2,}/', $this->getHttpResponse()->getStatusCode());
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^2[\...nse()->getStatusCode()) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    private function getDecodedJson()
75
    {
76
        if (!is_null($this->decoded)) {
0 ignored issues
show
introduced by
The condition is_null($this->decoded) is always false.
Loading history...
77
            return $this->decoded;
78
        }
79
80
        try {
81
            return $this->decoded = \GuzzleHttp\json_decode(
82
                (string) $this->getHttpResponse()->getBody(),
83
                true
84
            );
85
        } catch (\InvalidArgumentException $e) {
86
            return $this->decoded = array();
87
        }
88
    }
89
}
90