Completed
Push — master ( 247b53...16b162 )
by Jeroen De
03:24
created

LeafletService::getParameterInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 108

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 1

Importance

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