Completed
Push — master ( ceb9a8...527314 )
by ARCANEDEV
14s
created

AbstractResponse   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 100
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRaw() 0 4 1
A getStatus() 0 4 1
A get() 0 4 1
A isOk() 0 7 1
1
<?php namespace Arcanedev\GeoLocation\Google;
2
3
use Arcanedev\GeoLocation\Traits\CanBeJsonable;
4
use Illuminate\Contracts\Support\Arrayable;
5
use Illuminate\Contracts\Support\Jsonable;
6
use Illuminate\Support\Arr;
7
use JsonSerializable;
8
9
/**
10
 * Class     AbstractResponse
11
 *
12
 * @package  Arcanedev\GeoLocation\Google
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class AbstractResponse implements Arrayable, Jsonable, JsonSerializable
16
{
17
    /* -----------------------------------------------------------------
18
     |  Constants
19
     | -----------------------------------------------------------------
20
     */
21
22
    const STATUS_OK           = 'OK';
23
    const STATUS_ZERO_RESULTS = 'ZERO_RESULTS';
24
25
    /* -----------------------------------------------------------------
26
     |  Traits
27
     | -----------------------------------------------------------------
28
     */
29
30
    use CanBeJsonable;
31
32
    /* -----------------------------------------------------------------
33
     |  Properties
34
     | -----------------------------------------------------------------
35
     */
36
37
    /**
38
     * The response's data.
39
     *
40
     * @var  array
41
     */
42
    protected $data = [];
43
44
    /* -----------------------------------------------------------------
45
     |  Constructor
46
     | -----------------------------------------------------------------
47
     */
48
49
    /**
50
     * AbstractResponse constructor.
51
     *
52
     * @param  array  $data
53
     */
54 111
    public function __construct(array $data = [])
55
    {
56 111
        $this->data = $data;
57 111
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Getters & Setters
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Get the raw response.
66
     *
67
     * @return array
68
     */
69 105
    public function getRaw()
70
    {
71 105
        return $this->data;
72
    }
73
74
    /**
75
     * Get the status.
76
     *
77
     * @return string
78
     */
79 42
    public function getStatus()
80
    {
81 42
        return $this->get('status');
82
    }
83
84
    /* -----------------------------------------------------------------
85
     |  Main Methods
86
     | -----------------------------------------------------------------
87
     */
88
89
    /**
90
     * Get a data with a given key.
91
     *
92
     * @param  string      $key
93
     * @param  mixed|null  $default
94
     *
95
     * @return mixed
96
     */
97 99
    public function get($key, $default = null)
98
    {
99 99
        return Arr::get($this->getRaw(), $key, $default);
100
    }
101
102
    /**
103
     * Check if the response's status is OK.
104
     *
105
     * @return bool
106
     */
107 42
    public function isOk()
108
    {
109 42
        return in_array($this->getStatus(), [
110 42
            static::STATUS_OK,
111 42
            static::STATUS_ZERO_RESULTS,
112 14
        ]);
113
    }
114
}
115