Completed
Pull Request — master (#2)
by ARCANEDEV
03:37
created

GeocodingService::prepareQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php namespace Arcanedev\GeoLocation\Google\Geocoding;
2
3
use GuzzleHttp\ClientInterface;
4
5
/**
6
 * Class     GeocodingService
7
 *
8
 * @package  Arcanedev\GeoLocation\Google\Geocoding
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @link     https://developers.google.com/maps/documentation/geocoding/intro
12
 */
13
class GeocodingService
14
{
15
    /* -----------------------------------------------------------------
16
     |  Constants
17
     | -----------------------------------------------------------------
18
     */
19
20
    const BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
21
22
    /* -----------------------------------------------------------------
23
     |  Properties
24
     | -----------------------------------------------------------------
25
     */
26
27
    /** @var  \GuzzleHttp\ClientInterface */
28
    protected $client;
29
30
    /** @var string|null */
31
    protected $key = null;
32
33
    /* -----------------------------------------------------------------
34
     |  Constructor
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * GoogleDistanceMatrix constructor.
40
     *
41
     * @param  \GuzzleHttp\ClientInterface  $client
42
     */
43
    public function __construct(ClientInterface $client)
44
    {
45
        $this->setHttpClient($client);
46
        $this->setKey(getenv('GOOGLE_MAPS_GEOCODING_KEY'));
47
    }
48
49
    /* -----------------------------------------------------------------
50
     |  Getters & Setters
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Set the HTTP Client.
56
     *
57
     * @param  \GuzzleHttp\ClientInterface  $client
58
     *
59
     * @return self
60
     */
61
    public function setHttpClient(ClientInterface $client)
62
    {
63
        $this->client = $client;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Set the API Key.
70
     *
71
     * @param  string  $key
72
     *
73
     * @return self
74
     */
75
    public function setKey($key)
76
    {
77
        $this->key = $key;
78
79
        return $this;
80
    }
81
82
    /* -----------------------------------------------------------------
83
     |  Main Methods
84
     | -----------------------------------------------------------------
85
     */
86
87
    /**
88
     * Get the geocoding response.
89
     *
90
     * @param  string  $address
91
     * @param  array   $options
92
     *
93
     * @return \Arcanedev\GeoLocation\Google\Geocoding\GeocodingResponse
94
     */
95
    public function geocode($address, array $options = [])
96
    {
97
        $url = static::BASE_URL.'?'.$this->prepareQuery($address);
98
99
        $result = $this->client->request('GET', $url, $options);
100
101
        return new GeocodingResponse(
102
            json_decode($result->getBody(), true)
103
        );
104
    }
105
106
    /**
107
     * Prepare the URL query.
108
     *
109
     * @param  string  $address
110
     *
111
     * @return string
112
     */
113
    private function prepareQuery($address)
114
    {
115
        $queryData = array_filter([
116
            'address' => urlencode($address),
117
            'key'     => $this->key,
118
        ]);
119
120
        return urldecode(http_build_query($queryData));
121
    }
122
}
123