Completed
Push — master ( 5ad0f5...f795f0 )
by ARCANEDEV
11s
created

DirectionsService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\GeoLocation\Google\Directions;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\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 12
    public function setOrigin($origin)
56
    {
57 12
        $this->origin = $origin;
58
59 12
        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\Coordinates\Position  $position
78
     *
79
     * @return self
80
     */
81 3
    public function fromPosition(Position $position)
82
    {
83 3
        return $this->fromCoordinates(
84 3
            $position->lat()->value(),
85 3
            $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 6
    public function fromCoordinates($lat, $long)
98
    {
99 6
        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 3
    public function fromPlace($placeId)
110
    {
111 3
        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 12
    public function setDestination($destination)
122
    {
123 12
        $this->destination = $destination;
124
125 12
        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\Coordinates\Position  $position
144
     *
145
     * @return self
146
     */
147 3
    public function toPosition(Position $position)
148
    {
149 3
        return $this->toCoordinates(
150 3
            $position->lat()->value(),
151 3
            $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 6
    public function toCoordinates($lat, $long)
164
    {
165 6
        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 3
    public function toPlace($placeId)
176
    {
177 3
        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 12
    public function directions(array $options = [])
193
    {
194 12
        $url = static::BASE_URL.$this->prepareQuery([
195 12
            'origin'      => $this->origin,
196 12
            'destination' => $this->destination,
197
        ]);
198
199 12
        return $this->get($url, $options);
200
    }
201
202
    /* -----------------------------------------------------------------
203
     |  Other Methods
204
     | -----------------------------------------------------------------
205
     */
206
207
    /**
208
     * Prepare the response.
209
     *
210
     * @param  \Psr\Http\Message\ResponseInterface $response
211
     *
212
     * @return \Arcanedev\GeoLocation\Google\Directions\DirectionsResponse
213
     */
214 12
    protected function prepareResponse(ResponseInterface $response)
215
    {
216 12
        return new DirectionsResponse(
217 12
            json_decode($response->getBody(), true)
218
        );
219
    }
220
}
221