Places   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 29.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 53
c 1
b 0
f 0
dl 0
loc 229
ccs 17
cts 57
cp 0.2982
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A textSearch() 0 8 1
A details() 0 8 1
A findPlaceByText() 0 12 1
A makeApiCall() 0 4 1
A findPlace() 0 13 3
C findNearbyPlace() 0 38 12
A findPlaceByPhoneNumber() 0 13 1
A findNearbyPlaceByRadius() 0 12 1
A findNearbyPlaceByDistance() 0 9 1
1
<?php
2
/**
3
 * Copyright (c) 2019 - present
4
 * Google Maps PHP - Places.phpp
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 28/3/2019
8
 * MIT license: https://github.com/biscolab/google-maps-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\GoogleMaps\Api;
12
13
use Biscolab\GoogleMaps\Abstracts\Api;
14
use Biscolab\GoogleMaps\Enum\PlaceServicesEndpoints;
15
use Biscolab\GoogleMaps\Exception\InvalidArgumentException;
16
use Biscolab\GoogleMaps\Fields\GoogleMapsRequestFields;
17
use Biscolab\GoogleMaps\Fields\GoogleMapsResultFields;
18
use Biscolab\GoogleMaps\Http\GoogleMapsResult;
19
use Biscolab\GoogleMaps\Http\GoogleMapsResultsCollection;
20
use Biscolab\GoogleMaps\Http\Result\PlaceResultsCollection;
21
use Biscolab\GoogleMaps\Http\Result\PlacesResult;
22
use Biscolab\GoogleMaps\Object\Location;
23
use Biscolab\GoogleMaps\Utils\Config;
24
use Biscolab\GoogleMaps\Values\PlaceInputTypeValues;
25
use Biscolab\GoogleMaps\Values\RankByValues;
26
27
/**
28
 * Class Places
29
 * @package Biscolab\GoogleMaps\Api
30
 *
31
 * @since   0.5.0
32
 * @see     https://developers.google.com/places/web-service/intro
33
 */
34
class Places extends Api
35
{
36
37
	/**
38
	 * @var string
39
	 */
40
	const SERVICE_ENDPOINT = 'place';
41
42
	/**
43
	 * @var string
44
	 */
45
	protected $result_type = PlacesResult::class;
46
47
	/**
48
	 * @var string
49
	 */
50
	protected $result_collection_type = PlaceResultsCollection::class;
51
52
	/**
53
	 * @param string     $query
54
	 * @param array|null $params
55
	 * @param array|null $fields
56
	 *
57
	 * @return GoogleMapsResultsCollection
58
	 * @see https://developers.google.com/places/web-service/search#FindPlaceRequests
59
	 */
60
	public function findPlaceByText(
61
		string $query,
62
		?array $params = [],
63
		?array $fields = []
64
	): GoogleMapsResultsCollection {
65
66
		$params = array_merge($params, [
67
			GoogleMapsRequestFields::INPUT  => $query,
68
			GoogleMapsRequestFields::FIELDS => implode(',', $fields)
69
		]);
70
71
		return $this->findPlace($params);
72
73
	}
74
75
	/**
76
	 * Find Places requests
77
	 *
78
	 * @param array $params
79
	 *        GoogleMapsRequestFields::INPUT required
80
	 *
81
	 * @see     https://developers.google.com/places/web-service/search#FindPlaceRequests
82
	 * @return GoogleMapsResultsCollection
83
	 * @throws InvalidArgumentException
84
	 * @since   0.5.0
85
	 */
86
	public function findPlace(array $params): GoogleMapsResultsCollection
87
	{
88
89
		// see \Biscolab\GoogleMaps\Values\PlaceInputTypeValues
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
		if (empty($params[GoogleMapsRequestFields::INPUTTYPE])) {
91
			$params[GoogleMapsRequestFields::INPUTTYPE] = PlaceInputTypeValues::TEXTQUERY;
92
		}
93
94
		if (empty($params[GoogleMapsRequestFields::INPUT])) {
95
			throw new InvalidArgumentException(GoogleMapsRequestFields::INPUT . " field is required");
96
		}
97
98
		return $this->makeApiCall($params, PlaceServicesEndpoints::FINDPLACEFROMTEXT);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->makeApiCal...nts::FINDPLACEFROMTEXT) could return the type Biscolab\GoogleMaps\Http\GoogleMapsResult which is incompatible with the type-hinted return Biscolab\GoogleMaps\Http...leMapsResultsCollection. Consider adding an additional type-check to rule them out.
Loading history...
99
	}
100
101
	/**
102
	 * @param array  $params
103
	 * @param string $endpoint
104
	 *
105
	 * @return GoogleMapsResult|GoogleMapsResultsCollection
106
	 * @since   0.5.0
107
	 */
108
	public function makeApiCall(array $params, string $endpoint)
109
	{
110
111
		return $this->callApi($params, $endpoint);
112
	}
113
114
	/**
115
	 * @param string     $number
116
	 * @param array|null $params
117
	 * @param array|null $fields
118
	 *
119
	 * @return GoogleMapsResultsCollection
120
	 */
121
	public function findPlaceByPhoneNumber(
122
		string $number,
123
		?array $params = [],
124
		?array $fields = []
125
	): GoogleMapsResultsCollection {
126
127
		$params = array_merge($params, [
128
			GoogleMapsRequestFields::INPUT     => $number,
129
			GoogleMapsRequestFields::INPUTTYPE => PlaceInputTypeValues::PHONENUMBER,
130
			GoogleMapsRequestFields::FIELDS    => implode(',', $fields)
131
		]);
132
133
		return $this->findPlace($params);
134
135
	}
136
137
	/**
138
	 * @param Location   $location
139
	 * @param int        $radius
140
	 * @param array|null $params
141
	 *
142
	 * @return GoogleMapsResultsCollection
143
	 */
144
	public function findNearbyPlaceByRadius(
145
		Location $location,
146
		int $radius,
147
		?array $params = []
148
	): GoogleMapsResultsCollection {
149
150
		$params = array_merge($params, [
151
			GoogleMapsRequestFields::LOCATION => $location,
152
			GoogleMapsRequestFields::RADIUS   => $radius
153
		]);
154
155
		return $this->findNearbyPlace($params);
156
	}
157
158
	/**
159
	 * Nearby Search requests
160
	 *
161
	 * @param array $params
162
	 *
163
	 * @return GoogleMapsResultsCollection
164
	 *
165
	 * @throws InvalidArgumentException
166
	 * @see     https://developers.google.com/places/web-service/search#PlaceSearchRequests
167
	 * @since   0.5.0
168
	 */
169 5
	public function findNearbyPlace(array $params): GoogleMapsResultsCollection
170
	{
171
172 5
		if (!empty($params[GoogleMapsRequestFields::LOCATION])) {//-33.8670522,151.1957362
173 4
			$location = $params[GoogleMapsRequestFields::LOCATION];
174 4
			if (!$location instanceof Location) {
175 1
				throw new InvalidArgumentException(GoogleMapsRequestFields::LOCATION . ' field must be instance of ' . Location::class . ' class');
176
			}
177 3
			$params[GoogleMapsRequestFields::LOCATION] = (string)$params[GoogleMapsRequestFields::LOCATION];
178
		} else {
179 1
			throw new InvalidArgumentException(GoogleMapsResultFields::LOCATION . ' field is required');
180
		}
181
182 3
		if (!empty($params[GoogleMapsRequestFields::RANKBY]) &&
183 3
			$params[GoogleMapsRequestFields::RANKBY] === RankByValues::DISTANCE
184
		) {
185 1
			if (empty($params[GoogleMapsRequestFields::KEYWORD]) &&
186 1
				empty($params[GoogleMapsRequestFields::NAME]) &&
187 1
				empty($params[GoogleMapsRequestFields::TYPE])) {
188
//				If rankby=distance (described under Optional parameters below) is specified,
189
// 				then one or more of keyword, name, or type is required.
190 1
				throw new InvalidArgumentException('If ' . GoogleMapsRequestFields::RANKBY . ' is set as "' . RankByValues::DISTANCE . '" one or more of ' . GoogleMapsRequestFields::KEYWORD . ', ' . GoogleMapsRequestFields::NAME . ', ' . GoogleMapsRequestFields::TYPE . ' fields are required');
191
			}
192
			if (!empty($params[GoogleMapsRequestFields::RADIUS])) {
193
// 				Note that radius must not be included if rankby=distance (described under Optional parameters below) is specified.
194
				throw new InvalidArgumentException(GoogleMapsRequestFields::RADIUS . ' must not be included if ' . GoogleMapsRequestFields::RANKBY . ' = ' . RankByValues::DISTANCE);
195
			}
196 2
		} elseif (empty($params[GoogleMapsRequestFields::RADIUS])) {
197
//			radius — Defines the distance (in meters) within which to return place results.
198 1
			throw new InvalidArgumentException(GoogleMapsRequestFields::RADIUS . ' field is required');
199
		}
200
201 1
		if (!empty($params[GoogleMapsRequestFields::RADIUS]) && floatval($params[GoogleMapsRequestFields::RADIUS]) > Config::MAX_PLACE_RADIUS_VALUE) {
202
// 			The maximum allowed radius is 50 000 meters.
203 1
			throw new InvalidArgumentException(GoogleMapsRequestFields::RADIUS . ' must be lower than ' . Config::MAX_PLACE_RADIUS_VALUE);
204
		}
205
206
		return $this->makeApiCall($params, PlaceServicesEndpoints::NEARBYSEARCH);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->makeApiCal...ndpoints::NEARBYSEARCH) could return the type Biscolab\GoogleMaps\Http\GoogleMapsResult which is incompatible with the type-hinted return Biscolab\GoogleMaps\Http...leMapsResultsCollection. Consider adding an additional type-check to rule them out.
Loading history...
207
	}
208
209
	/**
210
	 * @param Location $location
211
	 * @param array    $params
212
	 *
213
	 * @return GoogleMapsResultsCollection
214
	 */
215
	public function findNearbyPlaceByDistance(Location $location, array $params): GoogleMapsResultsCollection
216
	{
217
218
		$params = array_merge($params, [
219
			GoogleMapsRequestFields::LOCATION => $location,
220
			GoogleMapsRequestFields::RANKBY   => RankByValues::DISTANCE
221
		]);
222
223
		return $this->findNearbyPlace($params);
224
	}
225
226
	/**
227
	 * Nearby Search requests
228
	 *
229
	 * @param string     $query
230
	 * @param array|null $params
231
	 *
232
	 * @see     https://developers.google.com/places/web-service/search#TextSearchRequests
233
	 * @return GoogleMapsResultsCollection
234
	 * @throws InvalidArgumentException
235
	 * @since   0.5.0
236
	 */
237
	public function textSearch(string $query, ?array $params = []): GoogleMapsResultsCollection
238
	{
239
240
		$params = array_merge($params, [
241
			GoogleMapsRequestFields::QUERY => $query
242
		]);
243
244
		return $this->makeApiCall($params, PlaceServicesEndpoints::TEXTSEARCH);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->makeApiCal...sEndpoints::TEXTSEARCH) could return the type Biscolab\GoogleMaps\Http\GoogleMapsResult which is incompatible with the type-hinted return Biscolab\GoogleMaps\Http...leMapsResultsCollection. Consider adding an additional type-check to rule them out.
Loading history...
245
	}
246
247
	/**
248
	 * @param string     $place_id
249
	 * @param array|null $params
250
	 *
251
	 * @return GoogleMapsResult
252
	 * @see https://developers.google.com/places/web-service/details
253
	 * @since v0.6.0
254
	 */
255
	public function details(string $place_id, ?array $params = []): GoogleMapsResult
256
	{
257
258
		$params = array_merge($params, [
259
			GoogleMapsRequestFields::PLACE_ID => $place_id
260
		]);
261
262
		return $this->makeApiCall($params, PlaceServicesEndpoints::DETAILS);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->makeApiCal...icesEndpoints::DETAILS) could return the type Biscolab\GoogleMaps\Http...leMapsResultsCollection which is incompatible with the type-hinted return Biscolab\GoogleMaps\Http\GoogleMapsResult. Consider adding an additional type-check to rule them out.
Loading history...
263
264
	}
265
266
}