Completed
Pull Request — master (#3)
by ARCANEDEV
05:27
created

DistanceMatrixService::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\DistanceMatrix;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Position;
4
use Arcanedev\GeoLocation\Google\AbstractWebService;
5
use GuzzleHttp\ClientInterface;
6
use InvalidArgumentException;
7
8
/**
9
 * Class     DistanceMatrixService
10
 *
11
 * @package  Arcanedev\GeoLocation\Google\DistanceMatrix
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @link     https://developers.google.com/maps/documentation/distance-matrix/intro
15
 */
16
class DistanceMatrixService extends AbstractWebService
17
{
18
    /* -----------------------------------------------------------------
19
     |  Constants
20
     | -----------------------------------------------------------------
21
     */
22
23
    const BASE_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json';
24
25
    /* -----------------------------------------------------------------
26
     |  Properties
27
     | -----------------------------------------------------------------
28
     */
29
30
    /** @var string */
31
    protected $language = 'en';
32
33
    /** @var string */
34
    protected $mode = 'driving';
35
36
    /** @var string */
37
    protected $units = 'metric';
38
39
    /* -----------------------------------------------------------------
40
     |  Constructor
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * GoogleDistanceMatrix constructor.
46
     *
47
     * @param  \GuzzleHttp\ClientInterface  $client
48
     */
49 21
    public function __construct(ClientInterface $client)
50
    {
51 21
        parent::__construct($client);
52
53 21
        $this->setKey(getenv('GOOGLE_MAPS_DISTANCE_MATRIX_KEY'));
54 21
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Getters & Setters
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Set the language.
63
     *
64
     * @param  string  $language
65
     *
66
     * @return self
67
     */
68 3
    public function setLanguage($language)
69
    {
70 3
        $this->language = $language;
71
72 3
        return $this;
73
    }
74
75
    /**
76
     * Set the mode of transport.
77
     *
78
     * @param  string  $mode
79
     *
80
     * @return self
81
     */
82 6
    public function setMode($mode)
83
    {
84 6
        $this->mode = $this->checkMode($mode);
85
86 3
        return $this;
87
    }
88
89
    /**
90
     * Set the unit system.
91
     *
92
     * @param  string  $units
93
     *
94
     * @return self
95
     */
96 6
    public function setUnits($units)
97
    {
98 6
        $this->units = $this->checkUnits($units);
99
100 3
        return $this;
101
    }
102
103
    /* -----------------------------------------------------------------
104
     |  Main Methods
105
     | -----------------------------------------------------------------
106
     */
107
108
    /**
109
     * Calculate the distance.
110
     *
111
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $start
112
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $end
113
     * @param  array                                               $options
114
     *
115
     * @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse
116
     */
117 12
    public function calculate(Position $start, Position $end, array $options = [])
118
    {
119 12
        $url = static::BASE_URL.'?'.$this->prepareQuery([
120 12
            'origins'      => $this->parsePosition($start),
121 12
            'destinations' => $this->parsePosition($end),
122 4
        ]);
123
124 12
        $response = $this->client->request('GET', $url, $options);
125
126 12
        return $this->prepareResponse($response);
127
    }
128
129
    /* -----------------------------------------------------------------
130
     |  Other Methods
131
     | -----------------------------------------------------------------
132
     */
133
134
    /**
135
     * Get the default query params.
136
     *
137
     * @return array
138
     */
139 12
    protected function getDefaultQueryParams()
140
    {
141
        return [
142 12
            'mode'         => $this->mode,
143 12
            'language'     => $this->language,
144 12
            'units'        => $this->units,
145 12
            'key'          => $this->key,
146 4
        ];
147
    }
148
149
    /**
150
     * Prepare the response.
151
     *
152
     * @param  \Psr\Http\Message\ResponseInterface  $response
153
     *
154
     * @return \Arcanedev\GeoLocation\Google\DistanceMatrix\DistanceMatrixResponse
155
     */
156 12
    private function prepareResponse($response)
157
    {
158 12
        return new DistanceMatrixResponse(
159 12
            json_decode($response->getBody(), true)
160 4
        );
161
    }
162
163
    /**
164
     * Check the mode of transport.
165
     *
166
     * @param  string  $mode
167
     *
168
     * @return string
169
     */
170 6
    private function checkMode($mode)
171
    {
172 6
        $mode = strtolower($mode);
173
174 6
        if ( ! in_array($mode, ['driving', 'walking', 'bicycling', 'transit'])) {
175 3
            throw new InvalidArgumentException(
176 3
                "The given mode of transport [{$mode}] is invalid. Please check the supported travel modes: ".
177 2
                "https://developers.google.com/maps/documentation/distance-matrix/intro#travel_modes"
178 1
            );
179
        }
180
181 3
        return $mode;
182
    }
183
184
    /**
185
     * Check the unit system.
186
     *
187
     * @param  string  $units
188
     *
189
     * @return string
190
     */
191 6
    private function checkUnits($units)
192
    {
193 6
        $units = strtolower($units);
194
195 6
        if ( ! in_array($units, ['metric', 'imperial'])) {
196 3
            throw new InvalidArgumentException(
197 3
                "The given unit system [{$units}] is invalid. Please check the supported units: ".
198 2
                "https://developers.google.com/maps/documentation/distance-matrix/intro#unit_systems"
199 1
            );
200
        }
201
202 3
        return $units;
203
    }
204
}
205