Completed
Push — master ( 4d0076...81538e )
by Jeroen De
26s queued 11s
created

DisplayMapFunction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.31%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 133
ccs 58
cts 59
cp 0.9831
rs 10
c 0
b 0
f 0

7 Methods

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