Completed
Push — master ( d34761...111ee6 )
by Jeroen De
03:32
created

GoogleMapsService   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 359
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.4%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 3
dl 0
loc 359
ccs 134
cts 139
cp 0.964
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getMappedLanguageCode() 0 13 2
A getName() 0 3 1
A getAliases() 0 3 1
A hasAlias() 0 3 1
B getParameterInfo() 0 212 2
A getTypeNames() 0 3 1
A newMapId() 0 7 1
A getApiScript() 0 16 3
A getDependencyHtml() 0 14 4
A getDependencies() 0 8 2
A processingResultToMapParams() 0 17 5
A getResourceModules() 0 3 1
A getParameterWithValue() 0 9 1
A processedParamsToMapParams() 0 3 1
1
<?php
2
3
namespace Maps;
4
5
use Html;
6
use ParamProcessor\ProcessedParam;
7
use ParamProcessor\ProcessingResult;
8
9
/**
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 * @author Peter Grassberger < [email protected] >
13
 */
14
class GoogleMapsService implements MappingService {
15
16
	/**
17
	 * Maps user input map types to the Google Maps names for the map types.
18
	 */
19
	private const MAP_TYPES = [
20
		'normal' => 'ROADMAP',
21
		'roadmap' => 'ROADMAP',
22
		'satellite' => 'SATELLITE',
23
		'hybrid' => 'HYBRID',
24
		'terrain' => 'TERRAIN',
25
		'physical' => 'TERRAIN',
26
		'earth' => 'earth'
27
	];
28
29
	private const TYPE_CONTROL_STYLES = [
30
		'default' => 'DEFAULT',
31
		'horizontal' => 'HORIZONTAL_BAR',
32
		'dropdown' => 'DROPDOWN_MENU'
33
	];
34
35
	private $addedDependencies = [];
36
37 24
	public function getName(): string {
38 24
		return 'googlemaps3';
39
	}
40
41 24
	public function getAliases(): array {
42 24
		return [ 'googlemaps', 'google' ];
43
	}
44
45
	public function hasAlias( string $alias ): bool {
46
		return in_array( $alias, [ 'googlemaps', 'google' ] );
47
	}
48
49 8
	public function getParameterInfo(): array {
50 8
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
51 8
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
52 8
		global $egMapsResizableByDefault;
53
54 8
		$params = MapsFunctions::getCommonParameters();
55
56 8
		$params['visitedicon'] = [
57
			'default' => '',
58
			'message' => 'maps-displaymap-par-visitedicon',
59
		];
60
61 8
		$params['wmsoverlay'] = [
62
			'type' => 'wmsoverlay',
63
			'default' => false,
64
			'delimiter' => ' ',
65
			'message' => 'maps-displaymap-par-wmsoverlay',
66
		];
67
68 8
		$params['zoom'] = [
69 8
			'type' => 'integer',
70
			'range' => [ 0, 20 ],
71 8
			'default' => $GLOBALS['egMapsGMaps3Zoom'],
72 8
			'message' => 'maps-par-zoom',
73
		];
74
75 8
		$params['type'] = [
76 8
			'default' => $egMapsGMaps3Type,
77 8
			'values' => self::getTypeNames(),
78 8
			'message' => 'maps-googlemaps3-par-type',
79
			'post-format' => function ( $value ) {
80 8
				return GoogleMapsService::MAP_TYPES[strtolower( $value )];
81 8
			},
82
		];
83
84 8
		$params['types'] = [
85 8
			'dependencies' => 'type',
86 8
			'default' => $egMapsGMaps3Types,
87 8
			'values' => self::getTypeNames(),
88 8
			'message' => 'maps-googlemaps3-par-types',
89
			'islist' => true,
90
			'post-format' => function ( array $value ) {
91 8
				foreach ( $value as &$part ) {
92 8
					$part = self::MAP_TYPES[strtolower( $part )];
93
				}
94
95 8
				return $value;
96 8
			},
97
		];
98
99 8
		$params['layers'] = [
100 8
			'default' => $egMapsGMaps3Layers,
101
			'values' => [
102
				'traffic',
103
				'bicycling',
104
				'transit'
105
			],
106 8
			'message' => 'maps-googlemaps3-par-layers',
107
			'islist' => true,
108
		];
109
110 8
		$params['controls'] = [
111 8
			'default' => $egMapsGMaps3Controls,
112
			'values' => [
113
				'pan',
114
				'zoom',
115
				'type',
116
				'scale',
117
				'streetview',
118
				'rotate'
119
			],
120 8
			'message' => 'maps-googlemaps3-par-controls',
121
			'islist' => true,
122
			'post-format' => function ( $value ) {
123 8
				return array_map( 'strtolower', $value );
124 8
			},
125
		];
126
127 8
		$params['zoomstyle'] = [
128 8
			'default' => $egMapsGMaps3DefZoomStyle,
129
			'values' => [ 'default', 'small', 'large' ],
130 8
			'message' => 'maps-googlemaps3-par-zoomstyle',
131 8
			'post-format' => 'strtoupper',
132
		];
133
134 8
		$params['typestyle'] = [
135 8
			'default' => $egMapsGMaps3DefTypeStyle,
136 8
			'values' => array_keys( self::TYPE_CONTROL_STYLES ),
137 8
			'message' => 'maps-googlemaps3-par-typestyle',
138
			'post-format' => function ( $value ) {
139 8
				return self::TYPE_CONTROL_STYLES[strtolower( $value )];
140 8
			},
141
		];
142
143 8
		$params['autoinfowindows'] = [
144 8
			'type' => 'boolean',
145 8
			'default' => $egMapsGMaps3AutoInfoWindows,
146 8
			'message' => 'maps-googlemaps3-par-autoinfowindows',
147
		];
148
149 8
		$params['resizable'] = [
150 8
			'type' => 'boolean',
151 8
			'default' => $egMapsResizableByDefault,
152 8
			'message' => 'maps-par-resizable',
153
		];
154
155 8
		$params['kmlrezoom'] = [
156 8
			'type' => 'boolean',
157 8
			'default' => $GLOBALS['egMapsRezoomForKML'],
158 8
			'message' => 'maps-googlemaps3-par-kmlrezoom',
159
		];
160
161 8
		$params['poi'] = [
162 8
			'type' => 'boolean',
163 8
			'default' => $GLOBALS['egMapsShowPOI'],
164 8
			'message' => 'maps-googlemaps3-par-poi',
165
		];
166
167 8
		$params['cluster'] = [
168
			'aliases' => [ 'markercluster' ],
169
			'type' => 'boolean',
170
			'default' => false,
171
			'message' => 'maps-par-markercluster',
172
		];
173
174 8
		$params['clustergridsize'] = [
175
			'type' => 'integer',
176
			'default' => 60,
177
			'message' => 'maps-googlemaps3-par-clustergridsize',
178
		];
179
180 8
		$params['clustermaxzoom'] = [
181
			'type' => 'integer',
182
			'default' => 20,
183
			'message' => 'maps-par-clustermaxzoom',
184
		];
185
186 8
		$params['clusterzoomonclick'] = [
187
			'type' => 'boolean',
188
			'default' => true,
189
			'message' => 'maps-par-clusterzoomonclick',
190
		];
191
192 8
		$params['clusteraveragecenter'] = [
193
			'type' => 'boolean',
194
			'default' => true,
195
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
196
		];
197
198 8
		$params['clusterminsize'] = [
199
			'type' => 'integer',
200
			'default' => 2,
201
			'message' => 'maps-googlemaps3-par-clusterminsize',
202
		];
203
204 8
		$params['imageoverlays'] = [
205
			'type' => 'mapsimageoverlay',
206
			'default' => [],
207
			'delimiter' => ';',
208
			'islist' => true,
209
			'message' => 'maps-googlemaps3-par-imageoverlays',
210
		];
211
212 8
		$params['kml'] = [
213 8
			'default' => [],
214 8
			'message' => 'maps-par-kml',
215
			'islist' => true,
216
			'post-format' => function( array $kmlFileNames ) {
217 8
				return array_values(
218 8
					array_filter(
219 8
						array_map(
220
							function( string $fileName ) {
221 1
								return wfExpandUrl( MapsFunctions::getFileUrl( $fileName ) );
0 ignored issues
show
Deprecated Code introduced by
The method Maps\MapsFunctions::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
222 8
							},
223
							$kmlFileNames
224
						),
225
						function( string $fileName ) {
226 1
							return $fileName !== '';
227 8
						}
228
					)
229
				);
230 8
			}
231
		];
232
233 8
		$params['gkml'] = [
234
			'default' => [],
235
			'message' => 'maps-googlemaps3-par-gkml',
236
			'islist' => true,
237
		];
238
239 8
		$params['searchmarkers'] = [
240
			'default' => '',
241
			'message' => 'maps-par-searchmarkers',
242
			// new CriterionSearchMarkers() FIXME
243
		];
244
245 8
		$params['fullscreen'] = [
246
			'aliases' => [ 'enablefullscreen' ],
247
			'type' => 'boolean',
248
			'default' => false,
249
			'message' => 'maps-par-enable-fullscreen',
250
		];
251
252 8
		$params['scrollwheelzoom'] = [
253
			'aliases' => [ 'scrollzoom' ],
254
			'type' => 'boolean',
255
			'default' => false,
256
			'message' => 'maps-par-scrollwheelzoom',
257
		];
258
259 8
		return $params;
260
	}
261
262
	/**
263
	 * Returns the names of all supported map types.
264
	 */
265 8
	private function getTypeNames(): array {
266 8
		return array_keys( self::MAP_TYPES );
267
	}
268
269 8
	public function newMapId(): string {
270 8
		static $mapsOnThisPage = 0;
271
272 8
		$mapsOnThisPage++;
273
274 8
		return 'map_google3_' . $mapsOnThisPage;
275
	}
276
277 8
	public function getResourceModules( array $params ): array {
278 8
		return [ 'ext.maps.googlemaps3', 'ext.maps.googlemaps3ajax' ];
279
	}
280
281 8
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
282 8
		$urlArgs = array_merge(
283
			[
284 8
				'language' => self::getMappedLanguageCode( $langCode )
285
			],
286
			$urlArgs
287
		);
288 8
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
289
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
290
		}
291 8
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
292
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
293
		}
294
295 8
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
296
	}
297
298
	/**
299
	 * Maps language codes to Google Maps API v3 compatible values.
300
	 */
301 8
	private static function getMappedLanguageCode( string $code ): string {
302
		$mappings = [
303 8
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
304
			'he' => 'iw',      // iw is googlish for hebrew
305
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
306
		];
307
308 8
		if ( array_key_exists( $code, $mappings ) ) {
309
			return $mappings[$code];
310
		}
311
312 8
		return $code;
313
	}
314
315 8
	public function getDependencyHtml( array $params ): string {
316 8
		$dependencies = [];
317
318
		// Only add dependencies that have not yet been added.
319 8
		foreach ( $this->getDependencies() as $dependency ) {
320 8
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
321 8
				$dependencies[] = $dependency;
322 8
				$this->addedDependencies[] = $dependency;
323
			}
324
		}
325
326
		// If there are dependencies, put them all together in a string, otherwise return false.
327 8
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
328
	}
329
330 8
	private function getDependencies(): array {
331
		return [
332 8
			self::getApiScript(
333 8
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
334 8
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
335
			)
336
		];
337
	}
338
339 8
	public function processingResultToMapParams( ProcessingResult $processingResult ): array {
340 8
		$parameters = $processingResult->getParameters();
341
342 8
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
343 7
				$parameters['coordinates']->getValue()
344 8
			) > 1 ) {
345 1
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
346
		}
347
348 8
		$mapParams = [];
349
350 8
		foreach ( $parameters as $parameter ) {
351 8
			$mapParams[$parameter->getName()] = $parameter->getValue();
352
		}
353
354 8
		return $this->processedParamsToMapParams( $mapParams );
355
	}
356
357
358 1
	private function getParameterWithValue( ProcessedParam $param, $value ) {
359 1
		return new ProcessedParam(
360 1
			$param->getName(),
361
			$value,
362 1
			$param->wasSetToDefault(),
363 1
			$param->getOriginalName(),
364 1
			$param->getOriginalValue()
365
		);
366
	}
367
368 8
	public function processedParamsToMapParams( array $params ): array {
369 8
		return $params;
370
	}
371
372
}
373