Completed
Pull Request — master (#4)
by ARCANEDEV
01:52
created

GeocodingService::setKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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