Completed
Push — master ( fa09b4...cd114b )
by Bradley
02:58
created

Mapper::searchLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Cornford\Googlmapper;
2
3
use Cornford\Googlmapper\Contracts\MappingInterface;
4
use Cornford\Googlmapper\Exceptions\MapperArgumentException;
5
use Cornford\Googlmapper\Exceptions\MapperException;
6
use Cornford\Googlmapper\Exceptions\MapperSearchException;
7
use Cornford\Googlmapper\Exceptions\MapperSearchResultException;
8
use Cornford\Googlmapper\Models\Location;
9
use Cornford\Googlmapper\Models\Map;
10
use Cornford\Googlmapper\Models\Streetview;
11
12
class Mapper extends MapperBase implements MappingInterface {
13
14
	/**
15
	 * Renders and returns Google Map code.
16
	 *
17
	 * @param integer $item
18
	 *
19
	 * @return string
20
	 */
21
	public function render($item = -1)
22
	{
23
		if (!$this->isEnabled()) {
24
			return;
25
		}
26
27
		return $this->view->make('googlmapper::mapper')
28
			->withView($this->view)
29
			->withOptions($this->getOptions())
30
			->withItems($item > -1 ? [$item => $this->getItem($item)] : $this->getItems())->render();
31
	}
32
33
	/**
34
	 * Search for a location against Google Maps Api.
35
	 *
36
	 * @param string $location
37
	 *
38
	 * @return mixed
39
	 */
40
	protected function searchLocation($location)
41
	{
42
		$location = urlencode($location);
43
		$request = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$location}&sensor=false");
44
45
		return json_decode($request);
46
	}
47
48
	/**
49
	 * Locate a location and return a Location instance.
50
	 *
51
	 * @param string $location
52
	 *
53
	 * @throws MapperArgumentException
54
	 * @throws MapperSearchException
55
	 * @throws MapperSearchResultException
56
	 * @throws MapperException
57
	 *
58
	 * @return Location
59
	 */
60
	public function location($location)
61
	{
62
		if (empty($location)) {
63
			throw new MapperArgumentException('Invalid location search term provided.');
64
		}
65
66
		try {
67
			$resultObject = $this->searchLocation($location);
68
		} catch (Exception $exception) {
0 ignored issues
show
Bug introduced by
The class Cornford\Googlmapper\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
69
			throw new MapperSearchException('Unable to perform location search, the error was: "' . $exception->getMessage() .  '".');
70
		}
71
72
		if (!isset($resultObject->results) || count($resultObject->results) == 0) {
73
			throw new MapperSearchResultException('No results found for the location search.');
74
		}
75
76
		if (!isset($resultObject->results[0]->formatted_address) ||
77
			!isset($resultObject->results[0]->address_components[0]->types[0]) ||
78
			!isset($resultObject->results[0]->geometry->location->lat) ||
79
			!isset($resultObject->results[0]->geometry->location->lng) ||
80
			!isset($resultObject->results[0]->place_id)
81
		) {
82
			throw new MapperException('The location search return invalid result data.');
83
		}
84
85
		return new Location([
86
			'mapper' => $this,
87
			'search' => $location,
88
			'address' => $resultObject->results[0]->formatted_address,
89
			'type' => $resultObject->results[0]->address_components[0]->types[0],
90
			'latitude' => $resultObject->results[0]->geometry->location->lat,
91
			'longitude' => $resultObject->results[0]->geometry->location->lng,
92
			'placeId' => $resultObject->results[0]->place_id,
93
		]);
94
	}
95
96
	/**
97
	 * Add a new map.
98
	 *
99
	 * @param float $latitude
100
	 * @param float $longitude
101
	 * @param array $options
102
	 *
103
	 * @return self
104
	 */
105
	public function map($latitude, $longitude, array $options = [])
106
	{
107
		$parameters = array_replace_recursive(
108
			$this->getOptions(),
109
			[
110
				'latitude' => $latitude,
111
				'longitude' => $longitude,
112
				'map' => 'map_' . count($this->getItems())
113
			],
114
			$options
115
		);
116
117
		$item = new Map($parameters);
118
		$this->addItem($item);
119
120
		return $this;
121
	}
122
123
	/**
124
	 * Add a new street view map.
125
	 *
126
	 * @param float   $latitude
127
	 * @param float   $longitude
128
	 * @param integer $heading
129
	 * @param integer $pitch
130
	 * @param array   $options
131
	 *
132
	 * @return self
133
	 */
134
	public function streetview($latitude, $longitude, $heading, $pitch, array $options = [])
135
	{
136
		$parameters = array_replace_recursive(
137
			$this->getOptions(),
138
			[
139
				'latitude' => $latitude,
140
				'longitude' => $longitude,
141
				'heading' => $heading,
142
				'pitch' => $pitch,
143
				'map' => 'map_' . count($this->getItems())
144
			],
145
			$options
146
		);
147
148
		$item = new Streetview($parameters);
149
		$this->addItem($item);
150
151
		return $this;
152
	}
153
154
	/**
155
	 * Add a new map marker.
156
	 *
157
	 * @param float $latitude
158
	 * @param float $longitude
159
	 * @param array $options
160
	 *
161
	 * @throws MapperException
162
	 *
163
	 * @return self
164
	 */
165
	public function marker($latitude, $longitude, array $options = [])
166
	{
167
		$items = $this->getItems();
168
		$parameters = $this->getOptions();
169
		$options = array_replace_recursive(['user' => $parameters['user']], $parameters['markers'], $options);
170
171
		if (empty($items)) {
172
			throw new MapperException('No map found to add a marker to.');
173
		}
174
175
		$item = end($items);
176
		$item->marker($latitude, $longitude, $options);
177
178
		return $this;
179
	}
180
181
	/**
182
	 * Add a new map information window.
183
	 *
184
	 * @param float  $latitude
185
	 * @param float  $longitude
186
	 * @param string $content
187
	 * @param array  $options
188
	 *
189
	 * @throws MapperException
190
	 *
191
	 * @return self
192
	 */
193
	public function informationWindow($latitude, $longitude, $content, array $options = [])
194
	{
195
		$items = $this->getItems();
196
		
197
		if (empty($items)) {
198
			throw new MapperException('No map found to add a information window to.');
199
		}
200
201
		$parameters = $this->getOptions();
202
		$options = array_replace_recursive(['user' => $parameters['user']], ['markers' => $parameters['markers']], $options);
203
		$item = end($items);
204
		$item->marker($latitude, $longitude, array_replace_recursive($options, ['markers' => ['content' => $content]]));
205
206
		return $this;
207
	}
208
209
	/**
210
	 * Add a new map polyline.
211
	 *
212
	 * @param array $coordinates
213
	 * @param array $options
214
	 *
215
	 * @throws MapperException
216
	 *
217
	 * @return self
218
	 */
219
	public function polyline(array $coordinates = [], array $options = [])
220
	{
221
		$items = $this->getItems();
222
		$parameters = $this->getOptions();
223
224
		$defaults = [
225
			'coordinates' => $coordinates,
226
			'geodesic' => false,
227
			'strokeColor' => '#FF0000',
228
			'strokeOpacity' => 0.8,
229
			'strokeWeight' => 2,
230
			'editable' => false
231
		];
232
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
233
234
		if (empty($items)) {
235
			throw new MapperException('No map found to add a polyline to.');
236
		}
237
238
		$item = end($items);
239
		$item->shape('polyline', $coordinates, $options);
240
241
		return $this;
242
	}
243
244
	/**
245
	 * Add a new map polygon.
246
	 *
247
	 * @param array $coordinates
248
	 * @param array $options
249
	 *
250
	 * @throws MapperException
251
	 *
252
	 * @return self
253
	 */
254 View Code Duplication
	public function polygon(array $coordinates = [], array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
255
	{
256
		$items = $this->getItems();
257
		$parameters = $this->getOptions();
258
259
		$defaults = [
260
			'coordinates' => $coordinates,
261
			'strokeColor' => '#FF0000',
262
			'strokeOpacity' => 0.8,
263
			'strokeWeight' => 2,
264
			'fillColor' => '#FF0000',
265
			'fillOpacity' => 0.35,
266
			'editable' => false
267
		];
268
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
269
270
		if (empty($items)) {
271
			throw new MapperException('No map found to add a polygon to.');
272
		}
273
274
		$item = end($items);
275
		$item->shape('polygon', $coordinates, $options);
276
277
		return $this;
278
	}
279
280
	/**
281
	 * Add a new map rectangle.
282
	 *
283
	 * @param array $coordinates
284
	 * @param array $options
285
	 *
286
	 * @throws MapperException
287
	 *
288
	 * @return self
289
	 */
290 View Code Duplication
	public function rectangle(array $coordinates = [], array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
291
	{
292
		$items = $this->getItems();
293
		$parameters = $this->getOptions();
294
295
		$defaults = [
296
			'coordinates' => $coordinates,
297
			'strokeColor' => '#FF0000',
298
			'strokeOpacity' => 0.8,
299
			'strokeWeight' => 2,
300
			'fillColor' => '#FF0000',
301
			'fillOpacity' => 0.35,
302
			'editable' => false
303
		];
304
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
305
306
		if (empty($items)) {
307
			throw new MapperException('No map found to add a rectangle to.');
308
		}
309
310
		$item = end($items);
311
		$item->shape('rectangle', $coordinates, $options);
312
313
		return $this;
314
	}
315
316
	/**
317
	 * Add a new map circle.
318
	 *
319
	 * @param array $coordinates
320
	 * @param array $options
321
	 *
322
	 * @throws MapperException
323
	 *
324
	 * @return self
325
	 */
326 View Code Duplication
	public function circle(array $coordinates = [], array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
327
	{
328
		$items = $this->getItems();
329
		$parameters = $this->getOptions();
330
331
		$defaults = [
332
			'coordinates' => $coordinates,
333
			'strokeColor' => '#FF0000',
334
			'strokeOpacity' => 0.8,
335
			'strokeWeight' => 2,
336
			'fillColor' => '#FF0000',
337
			'fillOpacity' => 0.35,
338
			'radius' => 100000,
339
			'editable' => false
340
		];
341
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
342
343
		if (empty($items)) {
344
			throw new MapperException('No map found to add a circle to.');
345
		}
346
347
		$item = end($items);
348
		$item->shape('circle', $coordinates, $options);
349
350
		return $this;
351
	}
352
353
}
354