Completed
Push — cln ( 863127...4377d0 )
by Jeroen De
09:58
created

GoogleMapsService   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 322
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 322
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B addParameterInfo() 0 190 2
A getDefaultZoom() 0 4 1
A getTypeNames() 0 3 1
A getMapId() 0 9 2
A getResourceModules() 0 6 1
A getDependencies() 0 8 2
A getApiScript() 0 16 3
A getMappedLanguageCode() 0 13 2
1
<?php
2
3
namespace Maps;
4
5
use Html;
6
7
/**
8
 * Class holding information and functionality specific to Google Maps v3.
9
 * This information and features can be used by any mapping feature.
10
 *
11
 * @since 0.7
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Peter Grassberger < [email protected] >
16
 */
17
class GoogleMapsService extends MappingService {
18
19
	/**
20
	 * Maps user input map types to the Google Maps names for the map types.
21
	 */
22
	private static $mapTypes = [
23
		'normal' => 'ROADMAP',
24
		'roadmap' => 'ROADMAP',
25
		'satellite' => 'SATELLITE',
26
		'hybrid' => 'HYBRID',
27
		'terrain' => 'TERRAIN',
28
		'physical' => 'TERRAIN',
29
		'earth' => 'earth'
30
	];
31
32
	private static $typeControlStyles = [
33
		'default' => 'DEFAULT',
34
		'horizontal' => 'HORIZONTAL_BAR',
35
		'dropdown' => 'DROPDOWN_MENU'
36
	];
37
38
	public function __construct( $serviceName ) {
39
		parent::__construct(
40
			$serviceName,
41
			[ 'googlemaps', 'google' ]
42
		);
43
	}
44
45
	/**
46
	 * @see MappingService::addParameterInfo
47
	 *
48
	 * @since 0.7
49
	 */
50
	public function addParameterInfo( array &$params ) {
0 ignored issues
show
Coding Style introduced by
addParameterInfo uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
51
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
52
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
53
		global $egMapsResizableByDefault, $egMapsGMaps3DefaultTilt;
54
55
		$params['zoom'] = [
56
			'type' => 'integer',
57
			'range' => [ 0, 20 ],
58
			'default' => self::getDefaultZoom(),
59
			'message' => 'maps-par-zoom',
60
		];
61
62
		$params['type'] = [
63
			'default' => $egMapsGMaps3Type,
64
			'values' => self::getTypeNames(),
65
			'message' => 'maps-googlemaps3-par-type',
66
			'post-format' => function ( $value ) {
67
				return GoogleMapsService::$mapTypes[strtolower( $value )];
68
			},
69
		];
70
71
		$params['types'] = [
72
			'dependencies' => 'type',
73
			'default' => $egMapsGMaps3Types,
74
			'values' => self::getTypeNames(),
75
			'message' => 'maps-googlemaps3-par-types',
76
			'islist' => true,
77
			'post-format' => function ( array $value ) {
78
				foreach ( $value as &$part ) {
79
					$part = self::$mapTypes[strtolower( $part )];
80
				}
81
82
				return $value;
83
			},
84
		];
85
86
		$params['layers'] = [
87
			'default' => $egMapsGMaps3Layers,
88
			'values' => [
89
				'traffic',
90
				'bicycling'
91
			],
92
			'message' => 'maps-googlemaps3-par-layers',
93
			'islist' => true,
94
		];
95
96
		$params['controls'] = [
97
			'default' => $egMapsGMaps3Controls,
98
			'values' => [
99
				'pan',
100
				'zoom',
101
				'type',
102
				'scale',
103
				'streetview',
104
				'rotate'
105
			],
106
			'message' => 'maps-googlemaps3-par-controls',
107
			'islist' => true,
108
			'post-format' => function ( $value ) {
109
				return array_map( 'strtolower', $value );
110
			},
111
		];
112
113
		$params['zoomstyle'] = [
114
			'default' => $egMapsGMaps3DefZoomStyle,
115
			'values' => [ 'default', 'small', 'large' ],
116
			'message' => 'maps-googlemaps3-par-zoomstyle',
117
			'post-format' => 'strtoupper',
118
		];
119
120
		$params['typestyle'] = [
121
			'default' => $egMapsGMaps3DefTypeStyle,
122
			'values' => array_keys( self::$typeControlStyles ),
123
			'message' => 'maps-googlemaps3-par-typestyle',
124
			'post-format' => function ( $value ) {
125
				return self::$typeControlStyles[strtolower( $value )];
126
			},
127
		];
128
129
		$params['autoinfowindows'] = [
130
			'type' => 'boolean',
131
			'default' => $egMapsGMaps3AutoInfoWindows,
132
			'message' => 'maps-googlemaps3-par-autoinfowindows',
133
		];
134
135
		$params['resizable'] = [
136
			'type' => 'boolean',
137
			'default' => $egMapsResizableByDefault,
138
			'message' => 'maps-par-resizable',
139
		];
140
141
		$params['kmlrezoom'] = [
142
			'type' => 'boolean',
143
			'default' => $GLOBALS['egMapsRezoomForKML'],
144
			'message' => 'maps-googlemaps3-par-kmlrezoom',
145
		];
146
147
		$params['poi'] = [
148
			'type' => 'boolean',
149
			'default' => $GLOBALS['egMapsShowPOI'],
150
			'message' => 'maps-googlemaps3-par-poi',
151
		];
152
153
		$params['markercluster'] = [
154
			'type' => 'boolean',
155
			'default' => false,
156
			'message' => 'maps-par-markercluster',
157
		];
158
159
		$params['clustergridsize'] = [
160
			'type' => 'integer',
161
			'default' => 60,
162
			'message' => 'maps-googlemaps3-par-clustergridsize',
163
		];
164
165
		$params['clustermaxzoom'] = [
166
			'type' => 'integer',
167
			'default' => 20,
168
			'message' => 'maps-par-clustermaxzoom',
169
		];
170
171
		$params['clusterzoomonclick'] = [
172
			'type' => 'boolean',
173
			'default' => true,
174
			'message' => 'maps-par-clusterzoomonclick',
175
		];
176
177
		$params['clusteraveragecenter'] = [
178
			'type' => 'boolean',
179
			'default' => true,
180
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
181
		];
182
183
		$params['clusterminsize'] = [
184
			'type' => 'integer',
185
			'default' => 2,
186
			'message' => 'maps-googlemaps3-par-clusterminsize',
187
		];
188
189
		$params['tilt'] = [
190
			'type' => 'integer',
191
			'default' => $egMapsGMaps3DefaultTilt,
192
			'message' => 'maps-googlemaps3-par-tilt',
193
		];
194
195
		$params['imageoverlays'] = [
196
			'type' => 'mapsimageoverlay',
197
			'default' => [],
198
			'delimiter' => ';',
199
			'islist' => true,
200
			'message' => 'maps-googlemaps3-par-imageoverlays',
201
		];
202
203
		$params['kml'] = [
204
			'default' => [],
205
			'message' => 'maps-par-kml',
206
			'islist' => true,
207
			// new MapsParamFile() FIXME
208
		];
209
210
		$params['gkml'] = [
211
			'default' => [],
212
			'message' => 'maps-googlemaps3-par-gkml',
213
			'islist' => true,
214
		];
215
216
		$params['fusiontables'] = [
217
			'default' => [],
218
			'message' => 'maps-googlemaps3-par-fusiontables',
219
			'islist' => true,
220
		];
221
222
		$params['searchmarkers'] = [
223
			'default' => '',
224
			'message' => 'maps-par-searchmarkers',
225
			// new CriterionSearchMarkers() FIXME
226
		];
227
228
		$params['enablefullscreen'] = [
229
			'type' => 'boolean',
230
			'default' => false,
231
			'message' => 'maps-par-enable-fullscreen',
232
		];
233
234
		$params['scrollwheelzoom'] = [
235
			'type' => 'boolean',
236
			'default' => false,
237
			'message' => 'maps-par-scrollwheelzoom',
238
		];
239
	}
240
241
	/**
242
	 * @since 0.6.5
243
	 */
244
	public function getDefaultZoom() {
245
		global $egMapsGMaps3Zoom;
246
		return $egMapsGMaps3Zoom;
247
	}
248
249
	/**
250
	 * Returns the names of all supported map types.
251
	 *
252
	 * @return array
253
	 */
254
	public static function getTypeNames() {
255
		return array_keys( self::$mapTypes );
256
	}
257
258
	/**
259
	 * @see MappingService::getMapId
260
	 *
261
	 * @since 0.6.5
262
	 */
263
	public function getMapId( $increment = true ) {
264
		static $mapsOnThisPage = 0;
265
266
		if ( $increment ) {
267
			$mapsOnThisPage++;
268
		}
269
270
		return 'map_google3_' . $mapsOnThisPage;
271
	}
272
273
	/**
274
	 * @see MappingService::getResourceModules
275
	 *
276
	 * @since 1.0
277
	 *
278
	 * @return array of string
279
	 */
280
	public function getResourceModules() {
281
		return array_merge(
282
			parent::getResourceModules(),
283
			[ 'ext.maps.googlemaps3' ]
284
		);
285
	}
286
287
	/**
288
	 * @see MappingService::getDependencies
289
	 *
290
	 * @return array
291
	 */
292
	protected function getDependencies() {
0 ignored issues
show
Coding Style introduced by
getDependencies uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
293
		return [
294
			self::getApiScript(
295
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
296
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
297
			)
298
		];
299
	}
300
301
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
0 ignored issues
show
Coding Style introduced by
getApiScript uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
302
		$urlArgs = array_merge(
303
			[
304
				'language' => self::getMappedLanguageCode( $langCode )
305
			],
306
			$urlArgs
307
		);
308
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
309
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
310
		}
311
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
312
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
313
		}
314
315
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
316
	}
317
318
	/**
319
	 * Maps language codes to Google Maps API v3 compatible values.
320
	 *
321
	 * @param string $code
322
	 *
323
	 * @return string The mapped code
324
	 */
325
	protected static function getMappedLanguageCode( $code ) {
326
		$mappings = [
327
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
328
			'he' => 'iw',      // iw is googlish for hebrew
329
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
330
		];
331
332
		if ( array_key_exists( $code, $mappings ) ) {
333
			$code = $mappings[$code];
334
		}
335
336
		return $code;
337
	}
338
}
339