Completed
Push — master ( 752318...b300ed )
by Jeroen De
03:51
created

GoogleMapsService   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 317
ccs 117
cts 123
cp 0.9512
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getAliases() 0 3 1
A hasAlias() 0 3 1
B getParameterInfo() 0 204 2
A getTypeNames() 0 3 1
A newMapId() 0 7 1
A getResourceModules() 0 3 1
A getApiScript() 0 16 3
A getMappedLanguageCode() 0 13 2
A getDependencyHtml() 0 14 4
A getDependencies() 0 8 2
1
<?php
2
3
namespace Maps;
4
5
use Html;
6
7
/**
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 * @author Peter Grassberger < [email protected] >
11
 */
12
class GoogleMapsService implements MappingService {
13
14
	/**
15
	 * Maps user input map types to the Google Maps names for the map types.
16
	 */
17
	private const MAP_TYPES = [
18
		'normal' => 'ROADMAP',
19
		'roadmap' => 'ROADMAP',
20
		'satellite' => 'SATELLITE',
21
		'hybrid' => 'HYBRID',
22
		'terrain' => 'TERRAIN',
23
		'physical' => 'TERRAIN',
24
		'earth' => 'earth'
25
	];
26
27
	private const TYPE_CONTROL_STYLES = [
28
		'default' => 'DEFAULT',
29
		'horizontal' => 'HORIZONTAL_BAR',
30
		'dropdown' => 'DROPDOWN_MENU'
31
	];
32
33
	private $addedDependencies = [];
34
35 21
	public function getName(): string {
36 21
		return 'googlemaps3';
37
	}
38
39 21
	public function getAliases(): array {
40 21
		return [ 'googlemaps', 'google' ];
41
	}
42
43
	public function hasAlias( string $alias ): bool {
44
		return in_array( $alias, [ 'googlemaps', 'google' ] );
45
	}
46
47 7
	public function getParameterInfo(): array {
48 7
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
49 7
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
50 7
		global $egMapsResizableByDefault;
51
52 7
		$params = MapsFunctions::getCommonParameters();
53
54 7
		$params['visitedicon'] = [
55
			'default' => '',
56
			'message' => 'maps-displaymap-par-visitedicon',
57
		];
58
59 7
		$params['wmsoverlay'] = [
60
			'type' => 'wmsoverlay',
61
			'default' => false,
62
			'delimiter' => ' ',
63
			'message' => 'maps-displaymap-par-wmsoverlay',
64
		];
65
66 7
		$params['zoom'] = [
67 7
			'type' => 'integer',
68
			'range' => [ 0, 20 ],
69 7
			'default' => $GLOBALS['egMapsGMaps3Zoom'],
70 7
			'message' => 'maps-par-zoom',
71
		];
72
73 7
		$params['type'] = [
74 7
			'default' => $egMapsGMaps3Type,
75 7
			'values' => self::getTypeNames(),
76 7
			'message' => 'maps-googlemaps3-par-type',
77 7
			'post-format' => function ( $value ) {
78 7
				return GoogleMapsService::MAP_TYPES[strtolower( $value )];
79 7
			},
80
		];
81
82 7
		$params['types'] = [
83 7
			'dependencies' => 'type',
84 7
			'default' => $egMapsGMaps3Types,
85 7
			'values' => self::getTypeNames(),
86 7
			'message' => 'maps-googlemaps3-par-types',
87
			'islist' => true,
88 7
			'post-format' => function ( array $value ) {
89 7
				foreach ( $value as &$part ) {
90 7
					$part = self::MAP_TYPES[strtolower( $part )];
91
				}
92
93 7
				return $value;
94 7
			},
95
		];
96
97 7
		$params['layers'] = [
98 7
			'default' => $egMapsGMaps3Layers,
99
			'values' => [
100
				'traffic',
101
				'bicycling',
102
				'transit'
103
			],
104 7
			'message' => 'maps-googlemaps3-par-layers',
105
			'islist' => true,
106
		];
107
108 7
		$params['controls'] = [
109 7
			'default' => $egMapsGMaps3Controls,
110
			'values' => [
111
				'pan',
112
				'zoom',
113
				'type',
114
				'scale',
115
				'streetview',
116
				'rotate'
117
			],
118 7
			'message' => 'maps-googlemaps3-par-controls',
119
			'islist' => true,
120 7
			'post-format' => function ( $value ) {
121 7
				return array_map( 'strtolower', $value );
122 7
			},
123
		];
124
125 7
		$params['zoomstyle'] = [
126 7
			'default' => $egMapsGMaps3DefZoomStyle,
127
			'values' => [ 'default', 'small', 'large' ],
128 7
			'message' => 'maps-googlemaps3-par-zoomstyle',
129 7
			'post-format' => 'strtoupper',
130
		];
131
132 7
		$params['typestyle'] = [
133 7
			'default' => $egMapsGMaps3DefTypeStyle,
134 7
			'values' => array_keys( self::TYPE_CONTROL_STYLES ),
135 7
			'message' => 'maps-googlemaps3-par-typestyle',
136 7
			'post-format' => function ( $value ) {
137 7
				return self::TYPE_CONTROL_STYLES[strtolower( $value )];
138 7
			},
139
		];
140
141 7
		$params['autoinfowindows'] = [
142 7
			'type' => 'boolean',
143 7
			'default' => $egMapsGMaps3AutoInfoWindows,
144 7
			'message' => 'maps-googlemaps3-par-autoinfowindows',
145
		];
146
147 7
		$params['resizable'] = [
148 7
			'type' => 'boolean',
149 7
			'default' => $egMapsResizableByDefault,
150 7
			'message' => 'maps-par-resizable',
151
		];
152
153 7
		$params['kmlrezoom'] = [
154 7
			'type' => 'boolean',
155 7
			'default' => $GLOBALS['egMapsRezoomForKML'],
156 7
			'message' => 'maps-googlemaps3-par-kmlrezoom',
157
		];
158
159 7
		$params['poi'] = [
160 7
			'type' => 'boolean',
161 7
			'default' => $GLOBALS['egMapsShowPOI'],
162 7
			'message' => 'maps-googlemaps3-par-poi',
163
		];
164
165 7
		$params['markercluster'] = [
166
			'type' => 'boolean',
167
			'default' => false,
168
			'message' => 'maps-par-markercluster',
169
		];
170
171 7
		$params['clustergridsize'] = [
172
			'type' => 'integer',
173
			'default' => 60,
174
			'message' => 'maps-googlemaps3-par-clustergridsize',
175
		];
176
177 7
		$params['clustermaxzoom'] = [
178
			'type' => 'integer',
179
			'default' => 20,
180
			'message' => 'maps-par-clustermaxzoom',
181
		];
182
183 7
		$params['clusterzoomonclick'] = [
184
			'type' => 'boolean',
185
			'default' => true,
186
			'message' => 'maps-par-clusterzoomonclick',
187
		];
188
189 7
		$params['clusteraveragecenter'] = [
190
			'type' => 'boolean',
191
			'default' => true,
192
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
193
		];
194
195 7
		$params['clusterminsize'] = [
196
			'type' => 'integer',
197
			'default' => 2,
198
			'message' => 'maps-googlemaps3-par-clusterminsize',
199
		];
200
201 7
		$params['imageoverlays'] = [
202
			'type' => 'mapsimageoverlay',
203
			'default' => [],
204
			'delimiter' => ';',
205
			'islist' => true,
206
			'message' => 'maps-googlemaps3-par-imageoverlays',
207
		];
208
209 7
		$params['kml'] = [
210 7
			'default' => [],
211 7
			'message' => 'maps-par-kml',
212
			'islist' => true,
213 7
			'post-format' => function( array $kmlFileNames ) {
214 7
				return array_map(
215 7
					function( string $fileName ) {
216
						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...
217 7
					},
218
					$kmlFileNames
219
				);
220 7
			}
221
		];
222
223 7
		$params['gkml'] = [
224
			'default' => [],
225
			'message' => 'maps-googlemaps3-par-gkml',
226
			'islist' => true,
227
		];
228
229 7
		$params['searchmarkers'] = [
230
			'default' => '',
231
			'message' => 'maps-par-searchmarkers',
232
			// new CriterionSearchMarkers() FIXME
233
		];
234
235 7
		$params['enablefullscreen'] = [
236
			'aliases' => [ 'fullscreen' ],
237
			'type' => 'boolean',
238
			'default' => false,
239
			'message' => 'maps-par-enable-fullscreen',
240
		];
241
242 7
		$params['scrollwheelzoom'] = [
243
			'aliases' => [ 'scrollzoom' ],
244
			'type' => 'boolean',
245
			'default' => false,
246
			'message' => 'maps-par-scrollwheelzoom',
247
		];
248
249 7
		return $params;
250
	}
251
252
	/**
253
	 * Returns the names of all supported map types.
254
	 */
255 7
	private function getTypeNames(): array {
256 7
		return array_keys( self::MAP_TYPES );
257
	}
258
259 7
	public function newMapId(): string {
260 7
		static $mapsOnThisPage = 0;
261
262 7
		$mapsOnThisPage++;
263
264 7
		return 'map_google3_' . $mapsOnThisPage;
265
	}
266
267 7
	public function getResourceModules(): array {
268 7
		return [ 'ext.maps.googlemaps3', 'ext.sm.googlemaps3ajax' ];
269
	}
270
271 7
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
272 7
		$urlArgs = array_merge(
273
			[
274 7
				'language' => self::getMappedLanguageCode( $langCode )
275
			],
276
			$urlArgs
277
		);
278 7
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
279
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
280
		}
281 7
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
282
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
283
		}
284
285 7
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
286
	}
287
288
	/**
289
	 * Maps language codes to Google Maps API v3 compatible values.
290
	 */
291 7
	private static function getMappedLanguageCode( string $code ): string {
292
		$mappings = [
293 7
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
294
			'he' => 'iw',      // iw is googlish for hebrew
295
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
296
		];
297
298 7
		if ( array_key_exists( $code, $mappings ) ) {
299
			return $mappings[$code];
300
		}
301
302 7
		return $code;
303
	}
304
305 7
	public function getDependencyHtml( array $params ): string {
306 7
		$dependencies = [];
307
308
		// Only add dependencies that have not yet been added.
309 7
		foreach ( $this->getDependencies() as $dependency ) {
310 7
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
311 7
				$dependencies[] = $dependency;
312 7
				$this->addedDependencies[] = $dependency;
313
			}
314
		}
315
316
		// If there are dependencies, put them all together in a string, otherwise return false.
317 7
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
318
	}
319
320 7
	private function getDependencies(): array {
321
		return [
322 7
			self::getApiScript(
323 7
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
324 7
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
325
			)
326
		];
327
	}
328
}
329