Completed
Push — master ( 4dbba6...2bc122 )
by Jeroen De
03:56
created

GoogleMapsService::getParameterInfo()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 212

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 83
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 212
ccs 83
cts 83
cp 1
rs 8
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 8
	public function getParameterInfo(): array {
46 8
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
47 8
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
48 8
		global $egMapsResizableByDefault;
49
50 8
		$params = MapsFunctions::getCommonParameters();
51
52 8
		$params['visitedicon'] = [
53
			'default' => '',
54
			'message' => 'maps-displaymap-par-visitedicon',
55
		];
56
57 8
		$params['wmsoverlay'] = [
58
			'type' => 'wmsoverlay',
59
			'default' => false,
60
			'delimiter' => ' ',
61
			'message' => 'maps-displaymap-par-wmsoverlay',
62
		];
63
64 8
		$params['zoom'] = [
65 8
			'type' => 'integer',
66
			'range' => [ 0, 20 ],
67 8
			'default' => $GLOBALS['egMapsGMaps3Zoom'],
68 8
			'message' => 'maps-par-zoom',
69
		];
70
71 8
		$params['type'] = [
72 8
			'default' => $egMapsGMaps3Type,
73 8
			'values' => self::getTypeNames(),
74 8
			'message' => 'maps-googlemaps3-par-type',
75
			'post-format' => function ( $value ) {
76 8
				return GoogleMapsService::MAP_TYPES[strtolower( $value )];
77 8
			},
78
		];
79
80 8
		$params['types'] = [
81 8
			'dependencies' => 'type',
82 8
			'default' => $egMapsGMaps3Types,
83 8
			'values' => self::getTypeNames(),
84 8
			'message' => 'maps-googlemaps3-par-types',
85
			'islist' => true,
86
			'post-format' => function ( array $value ) {
87 8
				foreach ( $value as &$part ) {
88 8
					$part = self::MAP_TYPES[strtolower( $part )];
89
				}
90
91 8
				return $value;
92 8
			},
93
		];
94
95 8
		$params['layers'] = [
96 8
			'default' => $egMapsGMaps3Layers,
97
			'values' => [
98
				'traffic',
99
				'bicycling',
100
				'transit'
101
			],
102 8
			'message' => 'maps-googlemaps3-par-layers',
103
			'islist' => true,
104
		];
105
106 8
		$params['controls'] = [
107 8
			'default' => $egMapsGMaps3Controls,
108
			'values' => [
109
				'pan',
110
				'zoom',
111
				'type',
112
				'scale',
113
				'streetview',
114
				'rotate'
115
			],
116 8
			'message' => 'maps-googlemaps3-par-controls',
117
			'islist' => true,
118
			'post-format' => function ( $value ) {
119 8
				return array_map( 'strtolower', $value );
120 8
			},
121
		];
122
123 8
		$params['zoomstyle'] = [
124 8
			'default' => $egMapsGMaps3DefZoomStyle,
125
			'values' => [ 'default', 'small', 'large' ],
126 8
			'message' => 'maps-googlemaps3-par-zoomstyle',
127 8
			'post-format' => 'strtoupper',
128
		];
129
130 8
		$params['typestyle'] = [
131 8
			'default' => $egMapsGMaps3DefTypeStyle,
132 8
			'values' => array_keys( self::TYPE_CONTROL_STYLES ),
133 8
			'message' => 'maps-googlemaps3-par-typestyle',
134
			'post-format' => function ( $value ) {
135 8
				return self::TYPE_CONTROL_STYLES[strtolower( $value )];
136 8
			},
137
		];
138
139 8
		$params['autoinfowindows'] = [
140 8
			'type' => 'boolean',
141 8
			'default' => $egMapsGMaps3AutoInfoWindows,
142 8
			'message' => 'maps-googlemaps3-par-autoinfowindows',
143
		];
144
145 8
		$params['resizable'] = [
146 8
			'type' => 'boolean',
147 8
			'default' => $egMapsResizableByDefault,
148 8
			'message' => 'maps-par-resizable',
149
		];
150
151 8
		$params['kmlrezoom'] = [
152 8
			'type' => 'boolean',
153 8
			'default' => $GLOBALS['egMapsRezoomForKML'],
154 8
			'message' => 'maps-googlemaps3-par-kmlrezoom',
155
		];
156
157 8
		$params['poi'] = [
158 8
			'type' => 'boolean',
159 8
			'default' => $GLOBALS['egMapsShowPOI'],
160 8
			'message' => 'maps-googlemaps3-par-poi',
161
		];
162
163 8
		$params['cluster'] = [
164
			'aliases' => [ 'markercluster' ],
165
			'type' => 'boolean',
166
			'default' => false,
167
			'message' => 'maps-par-markercluster',
168
		];
169
170 8
		$params['clustergridsize'] = [
171
			'type' => 'integer',
172
			'default' => 60,
173
			'message' => 'maps-googlemaps3-par-clustergridsize',
174
		];
175
176 8
		$params['clustermaxzoom'] = [
177
			'type' => 'integer',
178
			'default' => 20,
179
			'message' => 'maps-par-clustermaxzoom',
180
		];
181
182 8
		$params['clusterzoomonclick'] = [
183
			'type' => 'boolean',
184
			'default' => true,
185
			'message' => 'maps-par-clusterzoomonclick',
186
		];
187
188 8
		$params['clusteraveragecenter'] = [
189
			'type' => 'boolean',
190
			'default' => true,
191
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
192
		];
193
194 8
		$params['clusterminsize'] = [
195
			'type' => 'integer',
196
			'default' => 2,
197
			'message' => 'maps-googlemaps3-par-clusterminsize',
198
		];
199
200 8
		$params['imageoverlays'] = [
201
			'type' => 'mapsimageoverlay',
202
			'default' => [],
203
			'delimiter' => ';',
204
			'islist' => true,
205
			'message' => 'maps-googlemaps3-par-imageoverlays',
206
		];
207
208 8
		$params['kml'] = [
209 8
			'default' => [],
210 8
			'message' => 'maps-par-kml',
211
			'islist' => true,
212
			'post-format' => function( array $kmlFileNames ) {
213 8
				return array_values(
214 8
					array_filter(
215 8
						array_map(
216
							function( string $fileName ) {
217 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...
218 8
							},
219
							$kmlFileNames
220
						),
221
						function( string $fileName ) {
222 1
							return $fileName !== '';
223 8
						}
224
					)
225
				);
226 8
			}
227
		];
228
229 8
		$params['gkml'] = [
230
			'default' => [],
231
			'message' => 'maps-googlemaps3-par-gkml',
232
			'islist' => true,
233
		];
234
235 8
		$params['searchmarkers'] = [
236
			'default' => '',
237
			'message' => 'maps-par-searchmarkers',
238
			// new CriterionSearchMarkers() FIXME
239
		];
240
241 8
		$params['fullscreen'] = [
242
			'aliases' => [ 'enablefullscreen' ],
243
			'type' => 'boolean',
244
			'default' => false,
245
			'message' => 'maps-par-enable-fullscreen',
246
		];
247
248 8
		$params['scrollwheelzoom'] = [
249
			'aliases' => [ 'scrollzoom' ],
250
			'type' => 'boolean',
251
			'default' => false,
252
			'message' => 'maps-par-scrollwheelzoom',
253
		];
254
255 8
		return $params;
256
	}
257
258
	/**
259
	 * Returns the names of all supported map types.
260
	 */
261 8
	private function getTypeNames(): array {
262 8
		return array_keys( self::MAP_TYPES );
263
	}
264
265 8
	public function newMapId(): string {
266 8
		static $mapsOnThisPage = 0;
267
268 8
		$mapsOnThisPage++;
269
270 8
		return 'map_google3_' . $mapsOnThisPage;
271
	}
272
273 8
	public function getResourceModules( array $params ): array {
274 8
		return [ 'ext.maps.googlemaps3', 'ext.maps.googlemaps3ajax' ];
275
	}
276
277 8
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
278 8
		$urlArgs = array_merge(
279
			[
280 8
				'language' => self::getMappedLanguageCode( $langCode )
281
			],
282
			$urlArgs
283
		);
284 8
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
285
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
286
		}
287 8
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
288
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
289
		}
290
291 8
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
292
	}
293
294
	/**
295
	 * Maps language codes to Google Maps API v3 compatible values.
296
	 */
297 8
	private static function getMappedLanguageCode( string $code ): string {
298
		$mappings = [
299 8
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
300
			'he' => 'iw',      // iw is googlish for hebrew
301
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
302
		];
303
304 8
		if ( array_key_exists( $code, $mappings ) ) {
305
			return $mappings[$code];
306
		}
307
308 8
		return $code;
309
	}
310
311 8
	public function getDependencyHtml( array $params ): string {
312 8
		$dependencies = [];
313
314
		// Only add dependencies that have not yet been added.
315 8
		foreach ( $this->getDependencies() as $dependency ) {
316 8
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
317 8
				$dependencies[] = $dependency;
318 8
				$this->addedDependencies[] = $dependency;
319
			}
320
		}
321
322
		// If there are dependencies, put them all together in a string, otherwise return false.
323 8
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
324
	}
325
326 8
	private function getDependencies(): array {
327
		return [
328 8
			self::getApiScript(
329 8
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
330 8
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
331
			)
332
		];
333
	}
334
335 8
	public function processingResultToMapParams( ProcessingResult $processingResult ): array {
336 8
		$parameters = $processingResult->getParameters();
337
338 8
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
339 7
				$parameters['coordinates']->getValue()
340 8
			) > 1 ) {
341 1
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
342
		}
343
344 8
		$mapParams = [];
345
346 8
		foreach ( $parameters as $parameter ) {
347 8
			$mapParams[$parameter->getName()] = $parameter->getValue();
348
		}
349
350 8
		return $this->processedParamsToMapParams( $mapParams );
351
	}
352
353
354 1
	private function getParameterWithValue( ProcessedParam $param, $value ) {
355 1
		return new ProcessedParam(
356 1
			$param->getName(),
357
			$value,
358 1
			$param->wasSetToDefault(),
359 1
			$param->getOriginalName(),
360 1
			$param->getOriginalValue()
361
		);
362
	}
363
364 8
	public function processedParamsToMapParams( array $params ): array {
365 8
		return $params;
366
	}
367
368
}
369