Completed
Pull Request — master (#3)
by ARCANEDEV
05:27
created

GeocodingService::reversePosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php namespace Arcanedev\GeoLocation\Google\Geocoding;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Position as PositionContract;
4
use Arcanedev\GeoLocation\Entities\Position;
5
use Arcanedev\GeoLocation\Google\AbstractWebService;
6
use GuzzleHttp\ClientInterface;
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 AbstractWebService
17
{
18
    /* -----------------------------------------------------------------
19
     |  Constants
20
     | -----------------------------------------------------------------
21
     */
22
23
    const BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
24
25
    /* -----------------------------------------------------------------
26
     |  Constructor
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * GoogleDistanceMatrix constructor.
32
     *
33
     * @param  \GuzzleHttp\ClientInterface  $client
34
     */
35 9
    public function __construct(ClientInterface $client)
36
    {
37 9
        parent::__construct($client);
38
39 9
        $this->setKey(getenv('GOOGLE_MAPS_GEOCODING_KEY'));
40 9
    }
41
42
    /* -----------------------------------------------------------------
43
     |  Main Methods
44
     | -----------------------------------------------------------------
45
     */
46
47
    /**
48
     * Get the geocoding response.
49
     *
50
     * @param  string  $address
51
     * @param  array   $options
52
     *
53
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
54
     */
55 3
    public function geocode($address, array $options = [])
56
    {
57 3
        $url = static::BASE_URL.'?'.$this->prepareQuery([
58 3
            'address' => urlencode($address)
59 1
        ]);
60
61 3
        $response = $this->client->request('GET', $url, $options);
62
63 3
        return $this->prepareResponse($response);
64
    }
65
66
    /**
67
     * Reverse geocoding (address lookup).
68
     *
69
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $position
70
     * @param  array                                               $options
71
     *
72
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
73
     */
74 3
    public function reverse(PositionContract $position, array $options = [])
75
    {
76 3
        $url = static::BASE_URL.'?'.$this->prepareQuery([
77 3
            'latlng' => $this->parsePosition($position),
78 1
        ]);
79
80 3
        $response = $this->client->request('GET', $url, $options);
81
82 3
        return $this->prepareResponse($response);
83
    }
84
85
    /**
86
     * Reverse geocoding (address lookup & simplified).
87
     *
88
     * @param  float  $lat
89
     * @param  float  $long
90
     * @param  array  $options
91
     *
92
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
93
     */
94 3
    public function reversePosition($lat, $long, array $options = [])
95
    {
96 3
        return $this->reverse(Position::create($lat, $long), $options);
97
    }
98
99
    /* -----------------------------------------------------------------
100
     |  Other Methods
101
     | -----------------------------------------------------------------
102
     */
103
104
    /**
105
     * Get the default query params.
106
     *
107
     * @return array
108
     */
109 6
    protected function getDefaultQueryParams()
110
    {
111
        return [
112 6
            'key' => $this->key,
113 2
        ];
114
    }
115
116
    /**
117
     * Prepare the response.
118
     *
119
     * @param  \Psr\Http\Message\ResponseInterface  $response
120
     *
121
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
122
     */
123 6
    private function prepareResponse($response)
124
    {
125 6
        return new GeocodingResponse(
126 6
            json_decode($response->getBody(), true)
127 2
        );
128
    }
129
}
130