GeocodingService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 81
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4
ccs 13
cts 13
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A geocode() 0 8 1
A reverse() 0 8 1
A reversePosition() 0 4 1
A prepareResponse() 0 6 1
1
<?php namespace Arcanedev\GeoLocation\Google\Geocoding;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position as PositionContract;
4
use Arcanedev\GeoLocation\Entities\Coordinates\Position;
5
use Arcanedev\GeoLocation\Google\AbstractService;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Class     GeocodingService
10
 *
11
 * @package  Arcanedev\GeoLocation\Google\Geocoding
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @link     https://developers.google.com/maps/documentation/geocoding/intro
15
 */
16
class GeocodingService extends AbstractService
17
{
18
    /* -----------------------------------------------------------------
19
     |  Constants
20
     | -----------------------------------------------------------------
21
     */
22
23
    const BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
24
25
    /* -----------------------------------------------------------------
26
     |  Main Methods
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * Get the geocoding response.
32
     *
33
     * @param  string  $address
34
     * @param  array   $options
35
     *
36
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
37
     */
38 3
    public function geocode($address, array $options = [])
39
    {
40 3
        $url = static::BASE_URL.$this->prepareQuery([
41 3
            'address' => urlencode($address)
42
        ]);
43
44 3
        return $this->get($url, $options);
45
    }
46
47
    /**
48
     * Reverse geocoding (address lookup).
49
     *
50
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position  $position
51
     * @param  array                                               $options
52
     *
53
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
54
     */
55 3
    public function reverse(PositionContract $position, array $options = [])
56
    {
57 3
        $url = static::BASE_URL.$this->prepareQuery([
58 3
            'latlng' => $this->parsePosition($position),
59
        ]);
60
61 3
        return $this->get($url, $options);
62
    }
63
64
    /**
65
     * Reverse geocoding (address lookup & simplified).
66
     *
67
     * @param  float  $lat
68
     * @param  float  $long
69
     * @param  array  $options
70
     *
71
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
72
     */
73 3
    public function reversePosition($lat, $long, array $options = [])
74
    {
75 3
        return $this->reverse(Position::create($lat, $long), $options);
76
    }
77
78
    /* -----------------------------------------------------------------
79
     |  Other Methods
80
     | -----------------------------------------------------------------
81
     */
82
83
    /**
84
     * Prepare the response.
85
     *
86
     * @param  \Psr\Http\Message\ResponseInterface  $response
87
     *
88
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
89
     */
90 6
    protected function prepareResponse(ResponseInterface $response)
91
    {
92 6
        return new GeocodingResponse(
93 6
            json_decode($response->getBody(), true)
94
        );
95
    }
96
}
97