Completed
Push — master ( f143cd...4c4bcb )
by Bradley
02:39
created

Mapper::render()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
cc 8
eloc 12
nc 5
nop 1
rs 7.1428
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
use Exception;
12
13
class Mapper extends MapperBase implements MappingInterface {
14
15
	/**
16
	 * Renders and returns Google Map code.
17
	 *
18
	 * @param integer $item
19
	 *
20
	 * @return string
21
	 */
22
	public function render($item = -1)
23
	{
24
		if (!$this->isEnabled()) {
25
			return;
26
		}
27
28
		$options = $this->getOptions();
29
30
		foreach (($item > -1 ? [$this->getItem($item)] : $this->getItems()) as $model) {
31
			foreach ($model->getOptions() as $key => $option) {
32
				if (array_key_exists($key, $this->getOptions()) && $this->getOptions()[$key] !== $option) {
33
					$options[$key] = $option;
34
				}
35
			}
36
		}
37
38
		return $this->view->make('googlmapper::mapper')
0 ignored issues
show
Bug introduced by
The method withView() does not seem to exist on object<Illuminate\Contracts\View\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
			->withView($this->view)
40
			->withOptions($options)
41
			->withItems($item > -1 ? [$item => $this->getItem($item)] : $this->getItems())->render();
42
	}
43
44
	/**
45
	 * Search for a location against Google Maps Api.
46
	 *
47
	 * @param string $location
48
	 *
49
	 * @return mixed
50
	 */
51
	protected function searchLocation($location)
52
	{
53
		$location = urlencode($location);
54
		$request = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$location}&sensor=false");
55
56
		return json_decode($request);
57
	}
58
59
	/**
60
	 * Locate a location and return a Location instance.
61
	 *
62
	 * @param string $location
63
	 *
64
	 * @throws MapperArgumentException
65
	 * @throws MapperSearchException
66
	 * @throws MapperSearchResultException
67
	 * @throws MapperException
68
	 *
69
	 * @return Location
70
	 */
71
	public function location($location)
72
	{
73
		if (empty($location)) {
74
			throw new MapperArgumentException('Invalid location search term provided.');
75
		}
76
77
		try {
78
			$resultObject = $this->searchLocation($location);
79
		} catch (Exception $exception) {
80
			throw new MapperSearchException('Unable to perform location search, the error was: "' . $exception->getMessage() .  '".');
81
		}
82
83
		if (!isset($resultObject->results) || count($resultObject->results) == 0) {
84
			throw new MapperSearchResultException('No results found for the location search.');
85
		}
86
87
		if (!isset($resultObject->results[0]->formatted_address) ||
88
			!isset($resultObject->results[0]->address_components[0]->types[0]) ||
89
			!isset($resultObject->results[0]->geometry->location->lat) ||
90
			!isset($resultObject->results[0]->geometry->location->lng) ||
91
			!isset($resultObject->results[0]->place_id)
92
		) {
93
			throw new MapperException('The location search return invalid result data.');
94
		}
95
96
		return new Location([
97
			'mapper' => $this,
98
			'search' => $location,
99
			'address' => $resultObject->results[0]->formatted_address,
100
			'type' => $resultObject->results[0]->address_components[0]->types[0],
101
			'latitude' => $resultObject->results[0]->geometry->location->lat,
102
			'longitude' => $resultObject->results[0]->geometry->location->lng,
103
			'placeId' => $resultObject->results[0]->place_id,
104
		]);
105
	}
106
107
	/**
108
	 * Add a new map.
109
	 *
110
	 * @param float $latitude
111
	 * @param float $longitude
112
	 * @param array $options
113
	 *
114
	 * @return self
115
	 */
116
	public function map($latitude, $longitude, array $options = [])
117
	{
118
		$parameters = array_replace_recursive(
119
			$this->getOptions(),
120
			[
121
				'latitude' => $latitude,
122
				'longitude' => $longitude,
123
				'map' => 'map_' . count($this->getItems())
124
			],
125
			$options
126
		);
127
128
		$item = new Map($parameters);
129
		$this->addItem($item);
130
131
		return $this;
132
	}
133
134
	/**
135
	 * Add a new street view map.
136
	 *
137
	 * @param float   $latitude
138
	 * @param float   $longitude
139
	 * @param integer $heading
140
	 * @param integer $pitch
141
	 * @param array   $options
142
	 *
143
	 * @return self
144
	 */
145
	public function streetview($latitude, $longitude, $heading, $pitch, array $options = [])
146
	{
147
		$parameters = array_replace_recursive(
148
			$this->getOptions(),
149
			[
150
				'latitude' => $latitude,
151
				'longitude' => $longitude,
152
				'heading' => $heading,
153
				'pitch' => $pitch,
154
				'map' => 'map_' . count($this->getItems())
155
			],
156
			$options
157
		);
158
159
		$item = new Streetview($parameters);
160
		$this->addItem($item);
161
162
		return $this;
163
	}
164
165
	/**
166
	 * Add a new map marker.
167
	 *
168
	 * @param float $latitude
169
	 * @param float $longitude
170
	 * @param array $options
171
	 *
172
	 * @throws MapperException
173
	 *
174
	 * @return self
175
	 */
176
	public function marker($latitude, $longitude, array $options = [])
177
	{
178
		$items = $this->getItems();
179
		$parameters = $this->getOptions();
180
		$options = array_replace_recursive(['user' => $parameters['user']], $parameters['markers'], $options);
181
182
		if (empty($items)) {
183
			throw new MapperException('No map found to add a marker to.');
184
		}
185
186
		$item = end($items);
187
		$item->marker($latitude, $longitude, $options);
188
189
		return $this;
190
	}
191
192
	/**
193
	 * Add a new map information window.
194
	 *
195
	 * @param float  $latitude
196
	 * @param float  $longitude
197
	 * @param string $content
198
	 * @param array  $options
199
	 *
200
	 * @throws MapperException
201
	 *
202
	 * @return self
203
	 */
204
	public function informationWindow($latitude, $longitude, $content, array $options = [])
205
	{
206
		$items = $this->getItems();
207
		
208
		if (empty($items)) {
209
			throw new MapperException('No map found to add a information window to.');
210
		}
211
212
		$parameters = $this->getOptions();
213
		$options = array_replace_recursive(['user' => $parameters['user']], ['markers' => $parameters['markers']], $options);
214
		$item = end($items);
215
		$item->marker($latitude, $longitude, array_replace_recursive($options, ['markers' => ['content' => $content]]));
216
217
		return $this;
218
	}
219
220
	/**
221
	 * Add a new map polyline.
222
	 *
223
	 * @param array $coordinates
224
	 * @param array $options
225
	 *
226
	 * @throws MapperException
227
	 *
228
	 * @return self
229
	 */
230
	public function polyline(array $coordinates = [], array $options = [])
231
	{
232
		$items = $this->getItems();
233
		$parameters = $this->getOptions();
234
235
		$defaults = [
236
			'coordinates' => $coordinates,
237
			'geodesic' => false,
238
			'strokeColor' => '#FF0000',
239
			'strokeOpacity' => 0.8,
240
			'strokeWeight' => 2,
241
			'editable' => false
242
		];
243
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
244
245
		if (empty($items)) {
246
			throw new MapperException('No map found to add a polyline to.');
247
		}
248
249
		$item = end($items);
250
		$item->shape('polyline', $coordinates, $options);
251
252
		return $this;
253
	}
254
255
	/**
256
	 * Add a new map polygon.
257
	 *
258
	 * @param array $coordinates
259
	 * @param array $options
260
	 *
261
	 * @throws MapperException
262
	 *
263
	 * @return self
264
	 */
265 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...
266
	{
267
		$items = $this->getItems();
268
		$parameters = $this->getOptions();
269
270
		$defaults = [
271
			'coordinates' => $coordinates,
272
			'strokeColor' => '#FF0000',
273
			'strokeOpacity' => 0.8,
274
			'strokeWeight' => 2,
275
			'fillColor' => '#FF0000',
276
			'fillOpacity' => 0.35,
277
			'editable' => false
278
		];
279
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
280
281
		if (empty($items)) {
282
			throw new MapperException('No map found to add a polygon to.');
283
		}
284
285
		$item = end($items);
286
		$item->shape('polygon', $coordinates, $options);
287
288
		return $this;
289
	}
290
291
	/**
292
	 * Add a new map rectangle.
293
	 *
294
	 * @param array $coordinates
295
	 * @param array $options
296
	 *
297
	 * @throws MapperException
298
	 *
299
	 * @return self
300
	 */
301 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...
302
	{
303
		$items = $this->getItems();
304
		$parameters = $this->getOptions();
305
306
		$defaults = [
307
			'coordinates' => $coordinates,
308
			'strokeColor' => '#FF0000',
309
			'strokeOpacity' => 0.8,
310
			'strokeWeight' => 2,
311
			'fillColor' => '#FF0000',
312
			'fillOpacity' => 0.35,
313
			'editable' => false
314
		];
315
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
316
317
		if (empty($items)) {
318
			throw new MapperException('No map found to add a rectangle to.');
319
		}
320
321
		$item = end($items);
322
		$item->shape('rectangle', $coordinates, $options);
323
324
		return $this;
325
	}
326
327
	/**
328
	 * Add a new map circle.
329
	 *
330
	 * @param array $coordinates
331
	 * @param array $options
332
	 *
333
	 * @throws MapperException
334
	 *
335
	 * @return self
336
	 */
337 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...
338
	{
339
		$items = $this->getItems();
340
		$parameters = $this->getOptions();
341
342
		$defaults = [
343
			'coordinates' => $coordinates,
344
			'strokeColor' => '#FF0000',
345
			'strokeOpacity' => 0.8,
346
			'strokeWeight' => 2,
347
			'fillColor' => '#FF0000',
348
			'fillOpacity' => 0.35,
349
			'radius' => 100000,
350
			'editable' => false
351
		];
352
		$options = array_replace_recursive(['user' => $parameters['user']], $defaults, $options);
353
354
		if (empty($items)) {
355
			throw new MapperException('No map found to add a circle to.');
356
		}
357
358
		$item = end($items);
359
		$item->shape('circle', $coordinates, $options);
360
361
		return $this;
362
	}
363
364
}
365