Completed
Push — master ( ceb9a8...527314 )
by ARCANEDEV
14s
created

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