Completed
Push — master ( 4427e6...cf958e )
by Jeroen De
03:32 queued 11s
created

ParserHookSetup::registerDisplayMap()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.568
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use Maps\Map\DisplayMap\DisplayMapFunction;
8
use Parser;
9
use PPFrame;
10
11
class ParserHookSetup {
12
13
	private $parser;
14
15 1
	public function __construct( Parser $parser ) {
16 1
		$this->parser = $parser;
17 1
	}
18
19 1
	public function registerParserHooks() {
20 1
		 $this->registerServiceSpecificFunction( 'leaflet', 'leaflet' );
21 1
		 $this->registerServiceSpecificFunction( 'google_maps', 'googlemaps3' );
22
23 1
		$this->registerDisplayMap();
24 1
		$this->registerNonMapHooks();
25 1
	}
26
27 8
	private function registerServiceSpecificFunction( string $functionName, string $serviceName ) {
28 1
		$this->parser->setFunctionHook(
29 1
			$functionName,
30 8
			function ( Parser $parser, PPFrame $frame, array $arguments ) use ( $serviceName ) {
31 8
				return $this->handleFunctionHook( $parser, $frame, $arguments, [ 'service=' . $serviceName ] );
32 1
			},
33 1
			Parser::SFH_OBJECT_ARGS
34
		);
35 1
	}
36
37 17
	private function registerDisplayMap() {
38 1
		foreach ( [ 'display_map', 'display_point', 'display_points', 'display_line' ] as $hookName ) {
39 1
			$this->parser->setFunctionHook(
40 1
				$hookName,
41 17
				function ( Parser $parser, PPFrame $frame, array $arguments ) {
42 17
					return $this->handleFunctionHook( $parser, $frame, $arguments );
43 1
				},
44 1
				Parser::SFH_OBJECT_ARGS
45
			);
46
47 1
			$this->parser->setHook(
48 1
				$hookName,
49 2
				function ( $text, array $arguments, Parser $parser ) {
50 2
					if ( $text !== null ) {
51 2
						$arguments[DisplayMapFunction::getDefaultParameters()[0]] = $text;
52
					}
53
54 2
					return $this->getFactory()->getDisplayMapFunction()->getMapHtmlForParameterList( $parser, $arguments );
55 1
				}
56
			);
57
		}
58 1
	}
59
60 25
	private function handleFunctionHook( Parser $parser, PPFrame $frame, array $arguments, $fixedArguments = [] ): array {
61 25
		$mapHtml = $this->getFactory()->getDisplayMapFunction()->getMapHtmlForKeyValueStrings(
62 25
			$parser,
63 25
			array_merge(
64 25
				array_map(
65 25
					function ( $argument ) use ( $frame ) {
66 25
						return $frame->expand( $argument );
67 25
					},
68
					$arguments
69
				),
70
				$fixedArguments
71
			)
72
		);
73
74
		return [
75 25
			$mapHtml,
76
			'noparse' => true,
77
			'isHTML' => true,
78
		];
79
	}
80
81 28
	private function getFactory(): MapsFactory {
82
		// Not injected into this class since that execution would happen before TestFactory setup
83 28
		return MapsFactory::globalInstance();
84
	}
85
86 1
	private function registerNonMapHooks() {
87 1
		foreach ( $this->getFactory()->newNonMapParserHooks() as $hook ) {
88 1
			$hook->init( $this->parser );
89
		}
90 1
	}
91
92
}
93