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

ElevationService::__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
ccs 4
cts 4
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\Elevation;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position as PositionContract;
4
use Arcanedev\GeoLocation\Google\AbstractService;
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class     ElevationService
9
 *
10
 * @package  Arcanedev\GeoLocation\Google\Elevation
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class ElevationService extends AbstractService
14
{
15
    /* -----------------------------------------------------------------
16
     |  Constants
17
     | -----------------------------------------------------------------
18
     */
19
20
    const BASE_URL = 'https://maps.googleapis.com/maps/api/elevation/json';
21
22
    /* -----------------------------------------------------------------
23
     |  Main Methods
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * Get the geocoding response.
29
     *
30
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position  $position
31
     * @param  array   $options
32
     *
33
     * @return \Arcanedev\GeoLocation\Google\Elevation\ElevationResponse
34
     */
35 6
    public function location(PositionContract $position, array $options = [])
36
    {
37 6
        $url = static::BASE_URL.$this->prepareQuery([
38 6
            'locations' => $this->parsePosition($position),
39
        ]);
40
41 6
        return $this->get($url, $options);
42
    }
43
44
    /**
45
     * Reverse geocoding (address lookup).
46
     *
47
     * @param  array  $positions
48
     * @param  array  $options
49
     *
50
     * @return \Arcanedev\GeoLocation\Google\Elevation\ElevationResponse
51
     */
52
    public function path(array $positions, array $options = [])
53
    {
54 3
        $positions = array_map(function (PositionContract $position) {
55 3
            return $this->parsePosition($position);
56 3
        }, $positions);
57
58 3
        $url = static::BASE_URL.$this->prepareQuery([
59 3
            'locations' => implode('|', $positions),
60
        ]);
61
62 3
        return $this->get($url, $options);
63
    }
64
65
    /* -----------------------------------------------------------------
66
     |  Other Methods
67
     | -----------------------------------------------------------------
68
     */
69
70
    /**
71
     * Prepare the response.
72
     *
73
     * @param  \Psr\Http\Message\ResponseInterface $response
74
     *
75
     * @return \Arcanedev\GeoLocation\Google\AbstractResponse
76
     */
77 9
    protected function prepareResponse(ResponseInterface $response)
78
    {
79 9
        return new ElevationResponse(
80 9
            json_decode($response->getBody(), true)
81
        );
82
    }
83
}
84