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

LeafletService::getParameterInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 100

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 32
cts 32
cp 1
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1

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\ParameterTypes;
7
use ParamProcessor\ProcessingResult;
8
9
/**
10
 * @licence GNU GPL v2+
11
 */
12
class LeafletService implements MappingService {
13
14
	private $addedDependencies = [];
15
16 26
	public function getName(): string {
17 26
		return 'leaflet';
18
	}
19
20 24
	public function getAliases(): array {
21 24
		return [ 'leafletmaps', 'leaflet' ]; // TODO: main name should not be in here?
22
	}
23
24 18
	public function getParameterInfo(): array {
25 18
		$params = MapsFunctions::getCommonParameters();
26
27 18
		$params['zoom'] = [
28
			'type' => ParameterTypes::INTEGER,
29
			'range' => [ 0, 20 ],
30
			'default' => false,
31
			'message' => 'maps-par-zoom'
32
		];
33
34 18
		$params['defzoom'] = [
35 18
			'type' => ParameterTypes::INTEGER,
36
			'range' => [ 0, 20 ],
37 18
			'default' => self::getDefaultZoom(),
38 18
			'message' => 'maps-leaflet-par-defzoom'
39
		];
40
41 18
		$params['layers'] = [
42 18
			'aliases' => 'layer',
43 18
			'type' => 'string',
44 18
			'values' => array_keys( $GLOBALS['egMapsLeafletAvailableLayers'], true, true ),
45 18
			'default' => $GLOBALS['egMapsLeafletLayers'],
46 18
			'message' => 'maps-leaflet-par-layers',
47
			'islist' => true,
48
		];
49
50 18
		$params['overlays'] = [
51 18
			'aliases' => [ 'overlaylayers' ],
52
			'type' => ParameterTypes::STRING,
53 18
			'values' => array_keys( $GLOBALS['egMapsLeafletAvailableOverlayLayers'], true, true ),
54 18
			'default' => $GLOBALS['egMapsLeafletOverlayLayers'],
55 18
			'message' => 'maps-leaflet-par-overlaylayers',
56
			'islist' => true,
57
		];
58
59 18
		$params['resizable'] = [
60 18
			'type' => ParameterTypes::BOOLEAN,
61 18
			'default' => $GLOBALS['egMapsResizableByDefault'],
62 18
			'message' => 'maps-par-resizable'
63
		];
64
65 18
		$params['fullscreen'] = [
66
			'aliases' => [ 'enablefullscreen' ],
67
			'type' => ParameterTypes::BOOLEAN,
68
			'default' => false,
69
			'message' => 'maps-par-enable-fullscreen',
70
		];
71
72 18
		$params['scrollwheelzoom'] = [
73
			'aliases' => [ 'scrollzoom' ],
74
			'type' => ParameterTypes::BOOLEAN,
75
			'default' => true,
76
			'message' => 'maps-par-scrollwheelzoom',
77
		];
78
79 18
		$params['cluster'] = [
80
			'aliases' => [ 'markercluster' ],
81
			'type' => ParameterTypes::BOOLEAN,
82
			'default' => false,
83
			'message' => 'maps-par-markercluster',
84
		];
85
86 18
		$params['clustermaxzoom'] = [
87
			'type' => ParameterTypes::INTEGER,
88
			'default' => 20,
89
			'message' => 'maps-par-clustermaxzoom',
90
		];
91
92 18
		$params['clusterzoomonclick'] = [
93
			'type' => ParameterTypes::BOOLEAN,
94
			'default' => true,
95
			'message' => 'maps-par-clusterzoomonclick',
96
		];
97
98 18
		$params['clustermaxradius'] = [
99
			'type' => ParameterTypes::INTEGER,
100
			'default' => 80,
101
			'message' => 'maps-par-maxclusterradius',
102
		];
103
104 18
		$params['clusterspiderfy'] = [
105
			'type' => ParameterTypes::BOOLEAN,
106
			'default' => true,
107
			'message' => 'maps-leaflet-par-clusterspiderfy',
108
		];
109
110 18
		$params['geojson'] = [
111
			'type' => ParameterTypes::STRING,
112
			'default' => '',
113
			'message' => 'maps-displaymap-par-geojson',
114
		];
115
116 18
		$params['clicktarget'] = [
117
			'type' => ParameterTypes::STRING,
118
			'default' => '',
119
			'message' => 'maps-leaflet-par-clicktarget',
120
		];
121
122 18
		return $params;
123
	}
124
125
	/**
126
	 * @since 3.0
127
	 */
128 18
	public function getDefaultZoom() {
129 18
		return $GLOBALS['egMapsLeafletZoom'];
130
	}
131
132 18
	public function newMapId(): string {
133 18
		static $mapsOnThisPage = 0;
134
135 18
		$mapsOnThisPage++;
136
137 18
		return 'map_leaflet_' . $mapsOnThisPage;
138
	}
139
140 18
	public function getResourceModules( array $params ): array {
141 18
		$modules = [];
142
143 18
		$modules[] = 'ext.maps.leaflet.loader';
144
145 18
		if ( $params['resizable'] ) {
146
			$modules[] = 'ext.maps.resizable';
147
		}
148
149 18
		if ( $params['cluster'] ) {
150
			$modules[] = 'ext.maps.leaflet.markercluster';
151
		}
152
153 18
		if ( $params['fullscreen'] ) {
154
			$modules[] = 'ext.maps.leaflet.fullscreen';
155
		}
156
157 18
		if ( array_key_exists( 'geojson', $params ) ) {
158 18
			$modules[] = 'ext.maps.leaflet.editor';
159
		}
160
161 18
		if ( array_key_exists( 'ajaxquery', $params ) && $params['ajaxquery'] !== '' ) {
162
			$modules[] = 'ext.maps.leaflet.leafletajax';
163
		}
164
165 18
		return $modules;
166
	}
167
168 18
	public function getDependencyHtml( array $params ): string {
169 18
		$dependencies = [];
170
171
		// Only add dependencies that have not yet been added.
172 18
		foreach ( $this->getDependencies( $params ) as $dependency ) {
173 18
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
174 17
				$dependencies[] = $dependency;
175 17
				$this->addedDependencies[] = $dependency;
176
			}
177
		}
178
179
		// If there are dependencies, put them all together in a string, otherwise return false.
180 18
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
181
	}
182
183 18
	private function getDependencies( array $params ): array {
184 18
		$leafletPath = $GLOBALS['wgScriptPath'] . '/extensions/Maps/resources/lib/leaflet';
185
186 18
		return array_merge(
187
			[
188 18
				Html::linkedStyle( "$leafletPath/leaflet.css" ),
189
			],
190 18
			$this->getLayerDependencies( $params )
191
		);
192
	}
193
194 18
	private function getLayerDependencies( array $params ) {
195 18
		global $egMapsLeafletLayerDependencies, $egMapsLeafletAvailableLayers,
196 18
			   $egMapsLeafletLayersApiKeys;
197
198 18
		$layerDependencies = [];
199
200 18
		foreach ( $params['layers'] as $layerName ) {
201 18
			if ( array_key_exists( $layerName, $egMapsLeafletAvailableLayers )
202 18
				&& $egMapsLeafletAvailableLayers[$layerName]
203 18
				&& array_key_exists( $layerName, $egMapsLeafletLayersApiKeys )
204 18
				&& array_key_exists( $layerName, $egMapsLeafletLayerDependencies ) ) {
205
				$layerDependencies[] = '<script src="' . $egMapsLeafletLayerDependencies[$layerName] .
206
					$egMapsLeafletLayersApiKeys[$layerName] . '"></script>';
207
			}
208
		}
209
210 18
		return array_unique( $layerDependencies );
211
	}
212
213 16
	public function processingResultToMapParams( ProcessingResult $processingResult ): array {
214 16
		return $this->processedParamsToMapParams( $processingResult->getParameterArray() );
215
	}
216
217 18
	public function processedParamsToMapParams( array $params ): array {
218 18
		if ( $params['geojson'] !== '' ) {
219 3
			$fetcher = MapsFactory::newDefault()->newGeoJsonFetcher();
220
221 3
			$result = $fetcher->fetch( $params['geojson'] );
222
223 3
			$params['geojson'] = $result->getContent();
224 3
			$params['GeoJsonSource'] = $result->getTitleValue() === null ? null : $result->getTitleValue()->getText();
225 3
			$params['GeoJsonRevisionId'] = $result->getRevisionId();
226
		}
227
228 18
		return $params;
229
	}
230
231
}
232