Completed
Push — cleanz ( f1882e...25c139 )
by Jeroen De
27:32 queued 25:40
created

MapsGoogleMaps3::getApiScript()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

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