Completed
Pull Request — master (#4)
by ARCANEDEV
06:24
created

GeocodingResponse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 109
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormattedAddress() 0 4 1
A getAddressComponents() 0 4 1
A getLocationPosition() 0 6 1
A getLocationType() 0 4 1
A getViewport() 0 7 1
A getPlaceId() 0 4 1
A toArray() 0 11 1
A createPosition() 0 7 1
1
<?php namespace Arcanedev\GeoLocation\Google\Geocoding;
2
3
use Arcanedev\GeoLocation\Entities\Position;
4
use Arcanedev\GeoLocation\Entities\Viewport;
5
use Arcanedev\GeoLocation\Google\AbstractResponse;
6
use Illuminate\Support\Arr;
7
8
/**
9
 * Class     GeocodingResponse
10
 *
11
 * @package  Arcanedev\GeoLocation\Google\Geocoding
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class GeocodingResponse extends AbstractResponse
15
{
16
    /* -----------------------------------------------------------------
17
     |  Getters & Setters
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * Get the formatted address.
23
     *
24
     * @return string|null
25
     */
26 12
    public function getFormattedAddress()
27
    {
28 12
        return $this->get('results.0.formatted_address');
29
    }
30
31
    /**
32
     * Get the separate address components.
33
     *
34
     * @return array
35
     */
36 9
    public function getAddressComponents()
37
    {
38 9
        return $this->get('results.0.address_components', []);
39
    }
40
41
    /**
42
     * Get the location's position.
43
     *
44
     * @return \Arcanedev\GeoLocation\Contracts\Entities\Position
45
     */
46 12
    public function getLocationPosition()
47
    {
48 12
        return $this->createPosition(
49 12
            $this->get('results.0.geometry.location', [])
50 4
        );
51
    }
52
53
    /**
54
     * Get the location's type.
55
     *
56
     * @return string
57
     */
58 9
    public function getLocationType()
59
    {
60 9
        return $this->get('results.0.geometry.location_type');
61
    }
62
63
    /**
64
     * Get the viewport coordinates.
65
     *
66
     * @return \Arcanedev\GeoLocation\Entities\Viewport
67
     */
68 9
    public function getViewport()
69
    {
70 9
        return Viewport::create(
71 9
            $this->createPosition($this->get('results.0.geometry.viewport.northeast', [])),
72 9
            $this->createPosition($this->get('results.0.geometry.viewport.southwest', []))
73 3
        );
74
    }
75
76
    /**
77
     * Get the place id.
78
     *
79
     * @return string|null
80
     */
81 9
    public function getPlaceId()
82
    {
83 9
        return $this->get('results.0.place_id');
84
    }
85
86
    /* -----------------------------------------------------------------
87
     |  Other Methods
88
     | -----------------------------------------------------------------
89
     */
90
91
    /**
92
     * Convert the object to array.
93
     *
94
     * @return array
95
     */
96 6
    public function toArray()
97
    {
98
        return [
99 6
            'formatted_address'  => $this->getFormattedAddress(),
100 6
            'address_components' => $this->getAddressComponents(),
101 6
            'location_position'  => $this->getLocationPosition()->toArray(),
102 6
            'location_type'      => $this->getLocationType(),
103 6
            'viewport'           => $this->getViewport()->toArray(),
104 6
            'place_id'           => $this->getPlaceId(),
105 2
        ];
106
    }
107
108
    /**
109
     * Create the position instance.
110
     *
111
     * @param  array  $coordinates
112
     *
113
     * @return \Arcanedev\GeoLocation\Entities\Position
114
     */
115 15
    protected function createPosition(array $coordinates)
116
    {
117 15
        return Position::create(
118 15
            Arr::get($coordinates, 'lat', 0.0),
119 15
            Arr::get($coordinates, 'lng', 0.0)
120 5
        );
121
    }
122
}
123