Completed
Pull Request — master (#4)
by ARCANEDEV
01:52
created

DistanceMatrixService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 187
rs 10
c 0
b 0
f 0
ccs 37
cts 37
cp 1

9 Methods

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