Completed
Push — newparam ( 8a99e5...659dc5 )
by Jeroen De
01:32
created

DisplayMapFunction   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 98.65%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 0
loc 159
ccs 73
cts 74
cp 0.9865
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMapHtmlForKeyValueStrings() 0 23 1
A getMapHtmlForParameterList() 0 14 1
A getAllParameterDefinitions() 0 27 1
A getMapHtmlFromProcessor() 0 12 1
A extractServiceName() 0 8 1
A processedParametersToKeyValueArray() 0 9 2
A getDefaultParameters() 0 3 1
A defaultMapZoom() 0 7 4
A getParameterWithValue() 0 9 1
A trackMap() 0 5 2
1
<?php
2
3
namespace Maps\MediaWiki\ParserHooks;
4
5
use Maps;
6
use Maps\MappingService;
7
use Maps\MapsFactory;
8
use Maps\MappingServices;
9
use Maps\Presentation\ParameterExtractor;
10
use MWException;
11
use ParamProcessor\ProcessedParam;
12
use ParamProcessor\Processor;
13
use Parser;
14
15
/**
16
 * Class for the 'display_map' parser hooks.
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class DisplayMapFunction {
22
23
	private $services;
24
25
	private $renderer;
26
27 21
	public function __construct( MappingServices $services ) {
28 21
		$this->services = $services;
29
30 21
		$this->renderer = new DisplayMapRenderer();
31 21
	}
32
33
	/**
34
	 * @param Parser $parser
35
	 * @param string[] $parameters Values of the array can be named parameters ("key=value") or unnamed.
36
	 * They are not normalized, so can be "key =  value "
37
	 *
38
	 * @return string
39
	 * @throws MWException
40
	 */
41 19
	public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters ): string {
42 19
		$processor = new Processor( new \ParamProcessor\Options() );
43
44 19
		$service = $this->services->getServiceOrDefault(
45 19
			$this->extractServiceName(
46 19
				Maps\Presentation\ParameterExtractor::extractFromKeyValueStrings( $parameters )
47
			)
48
		);
49
50 19
		$this->renderer->service = $service;
51
52 19
		$processor->setFunctionParams(
53 19
			$parameters,
54 19
			[],
55 19
			self::getDefaultParameters()
56
		);
57
58 19
		$processor->setParameterDefinitions(
59 19
			$this->getAllParameterDefinitions( $service, ';' )
60
		);
61
62 19
		return $this->getMapHtmlFromProcessor( $parser, $processor );
63
	}
64
65
	/**
66
	 * @param Parser $parser
67
	 * @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys.
68
	 * Both keys and values have not been normalized.
69
	 *
70
	 * @return string
71
	 * @throws MWException
72
	 */
73 2
	public function getMapHtmlForParameterList( Parser $parser, array $parameters ) {
74 2
		$processor = new Processor( new \ParamProcessor\Options() );
75
76 2
		$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) );
77
78 2
		$this->renderer->service = $service;
79
80 2
		$processor->setParameters( $parameters );
81 2
		$processor->setParameterDefinitions(
82 2
			$this->getAllParameterDefinitions( $service, "\n" )
83
		);
84
85 2
		return $this->getMapHtmlFromProcessor( $parser, $processor );
86
	}
87
88 21
	private function getAllParameterDefinitions( MappingService $service, string $locationDelimiter ) {
89 21
		$params = [];
90
91 21
		$params['mappingservice'] = [
92 21
			'type' => 'string',
93 21
			'aliases' => 'service',
94 21
			'default' => $GLOBALS['egMapsDefaultService'],
95 21
			'values' => MapsFactory::globalInstance()->getMappingServices()->getAllNames(),
96 21
			'message' => 'maps-par-mappingservice'
97
		];
98
99 21
		$params['coordinates'] = [
100 21
			'type' => 'string',
101
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
102
			'default' => [],
103
			'islist' => true,
104 21
			'delimiter' => $locationDelimiter,
105 21
			'message' => 'maps-displaymap-par-coordinates',
106
		];
107
108 21
		return MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays(
109 21
			array_merge(
110 21
				$params,
111 21
				$service->getParameterInfo()
112
			)
113
		);
114
	}
115
116 21
	private function getMapHtmlFromProcessor( Parser $parser, Processor $processor ) {
117 21
		$params = $processor->processParameters()->getParameters();
118
119 21
		$this->defaultMapZoom( $params );
120
121 21
		$this->trackMap( $parser );
122
123 21
		return $this->renderer->renderMap(
124 21
			$this->processedParametersToKeyValueArray( $params ),
125
			$parser
126
		);
127
	}
128
129 21
	private function extractServiceName( array $parameters ): string {
130 21
		$service = ( new ParameterExtractor() )->extract(
131 21
			[ 'mappingservice', 'service' ],
132
			$parameters
133
		);
134
135 21
		return $service ?? '';
136
	}
137
138 21
	private function processedParametersToKeyValueArray( array $params ): array {
139 21
		$parameters = [];
140
141 21
		foreach ( $params as $parameter ) {
142 21
			$parameters[$parameter->getName()] = $parameter->getValue();
143
		}
144
145 21
		return $parameters;
146
	}
147
148 21
	public static function getDefaultParameters(): array {
149 21
		return [ 'coordinates' ];
150
	}
151
152
	/**
153
	 * @param ProcessedParam[] $parameters
154
	 */
155 21
	private function defaultMapZoom( array &$parameters ) {
156 21
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
157 20
				$parameters['coordinates']->getValue()
158 21
			) > 1 ) {
159 2
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
160
		}
161 21
	}
162
163 2
	private function getParameterWithValue( ProcessedParam $param, $value ) {
164 2
		return new ProcessedParam(
165 2
			$param->getName(),
166
			$value,
167 2
			$param->wasSetToDefault(),
168 2
			$param->getOriginalName(),
169 2
			$param->getOriginalValue()
170
		);
171
	}
172
173 21
	private function trackMap( Parser $parser ) {
174 21
		if ( $GLOBALS['egMapsEnableCategory'] ) {
175
			$parser->addTrackingCategory( 'maps-tracking-category' );
176
		}
177 21
	}
178
179
}
180