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

DirectionsService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 56.1%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 5
dl 0
loc 218
ccs 23
cts 41
cp 0.561
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrigin() 0 6 1
A from() 0 4 1
A fromPosition() 0 7 1
A fromCoordinates() 0 4 1
A fromPlace() 0 4 1
A setDestination() 0 6 1
A to() 0 4 1
A toPosition() 0 7 1
A toCoordinates() 0 4 1
A toPlace() 0 4 1
A directions() 0 9 1
A getDefaultQueryParams() 0 6 1
A prepareResponse() 0 6 1
1
<?php namespace Arcanedev\GeoLocation\Google\Directions;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Position;
4
use Arcanedev\GeoLocation\Google\AbstractService;
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class     DirectionsService
9
 *
10
 * @package  Arcanedev\GeoLocation\Google\Directions
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @link     https://developers.google.com/maps/documentation/directions/intro
14
 */
15
class DirectionsService extends AbstractService
16
{
17
    /* -----------------------------------------------------------------
18
     |  Constants
19
     | -----------------------------------------------------------------
20
     */
21
22
    const BASE_URL = 'https://maps.googleapis.com/maps/api/directions/json';
23
24
    /* -----------------------------------------------------------------
25
     |  Properties
26
     | -----------------------------------------------------------------
27
     */
28
29
    /**
30
     * Start point.
31
     *
32
     * @var string
33
     */
34
    protected $origin;
35
36
    /**
37
     * End point.
38
     *
39
     * @var string
40
     */
41
    protected $destination;
42
43
    /* -----------------------------------------------------------------
44
     |  Getters & Setters
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Set the start point.
50
     *
51
     * @param  string  $origin
52
     *
53
     * @return self
54
     */
55 3
    public function setOrigin($origin)
56
    {
57 3
        $this->origin = $origin;
58
59 3
        return $this;
60
    }
61
62
    /**
63
     * Set the origin point with an address (alias).
64
     *
65
     * @param  string  $address
66
     *
67
     * @return self
68
     */
69 3
    public function from($address)
70
    {
71 3
        return $this->setOrigin($address);
72
    }
73
74
    /**
75
     * Set the origin point with position object.
76
     *
77
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $position
78
     *
79
     * @return self
80
     */
81
    public function fromPosition(Position $position)
82
    {
83
        return $this->fromCoordinates(
84
            $position->lat()->value(),
85
            $position->lng()->value()
86
        );
87
    }
88
89
    /**
90
     * Set the origin point with coordinates.
91
     *
92
     * @param  float  $lat
93
     * @param  float  $long
94
     *
95
     * @return self
96
     */
97
    public function fromCoordinates($lat, $long)
98
    {
99
        return $this->setOrigin("{$lat},{$long}");
100
    }
101
102
    /**
103
     * Set the origin point with place id.
104
     *
105
     * @param  string  $placeId
106
     *
107
     * @return self
108
     */
109
    public function fromPlace($placeId)
110
    {
111
        return $this->setOrigin("place_id:{$placeId}");
112
    }
113
114
    /**
115
     * Set the end point.
116
     *
117
     * @param  string  $destination
118
     *
119
     * @return self
120
     */
121 3
    public function setDestination($destination)
122
    {
123 3
        $this->destination = $destination;
124
125 3
        return $this;
126
    }
127
128
    /**
129
     * Set the destination point with an address.
130
     *
131
     * @param  string  $address
132
     *
133
     * @return self
134
     */
135 3
    public function to($address)
136
    {
137 3
        return $this->setDestination($address);
138
    }
139
140
    /**
141
     * Set the destination point with position object.
142
     *
143
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $position
144
     *
145
     * @return self
146
     */
147
    public function toPosition(Position $position)
148
    {
149
        return $this->toCoordinates(
150
            $position->lat()->value(),
151
            $position->lng()->value()
152
        );
153
    }
154
155
    /**
156
     * Set the destination point with coordinates.
157
     *
158
     * @param  float  $lat
159
     * @param  float  $long
160
     *
161
     * @return self
162
     */
163
    public function toCoordinates($lat, $long)
164
    {
165
        return $this->setDestination("{$lat},{$long}");
166
    }
167
168
    /**
169
     * Set the destination point with place id.
170
     *
171
     * @param  string  $placeId
172
     *
173
     * @return self
174
     */
175
    public function toPlace($placeId)
176
    {
177
        return $this->setDestination("place_id:{$placeId}");
178
    }
179
180
    /* -----------------------------------------------------------------
181
     |  Main Methods
182
     | -----------------------------------------------------------------
183
     */
184
185
    /**
186
     * Get the directions.
187
     *
188
     * @param  array  $options
189
     *
190
     * @return \Arcanedev\GeoLocation\Google\Directions\DirectionsResponse
191
     */
192 3
    public function directions(array $options = [])
193
    {
194 3
        $url = static::BASE_URL.$this->prepareQuery([
195 3
            'origin'      => $this->origin,
196 3
            'destination' => $this->destination,
197 1
        ]);
198
199 3
        return $this->get($url, $options);
200
    }
201
202
    /* -----------------------------------------------------------------
203
     |  Other Methods
204
     | -----------------------------------------------------------------
205
     */
206
207
    /**
208
     * Get the default query params.
209
     *
210
     * @return array
211
     */
212 3
    protected function getDefaultQueryParams()
213
    {
214
        return [
215 3
            'key' => $this->key,
216 1
        ];
217
    }
218
219
    /**
220
     * Prepare the response.
221
     *
222
     * @param  \Psr\Http\Message\ResponseInterface $response
223
     *
224
     * @return \Arcanedev\GeoLocation\Google\Directions\DirectionsResponse
225
     */
226 3
    protected function prepareResponse(ResponseInterface $response)
227
    {
228 3
        return new DirectionsResponse(
229 3
            json_decode($response->getBody(), true)
230 1
        );
231
    }
232
}
233