Completed
Push — cargo ( 1a3d18 )
by Jeroen De
03:25
created

DisplayMapRenderer::getLocationJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Map\DisplayMap;
6
7
use Maps\DataAccess\MediaWikiFileUrlFinder;
8
use Maps\Elements\Location;
9
use Maps\Map\MapData;
10
use Maps\Map\MapOutput;
11
use Maps\Map\MapOutputBuilder;
12
use Maps\MappingService;
13
use Maps\Presentation\ElementJsonSerializer;
14
use Maps\Map\MapHtmlBuilder;
15
use Maps\Presentation\WikitextParser;
16
use Maps\Presentation\WikitextParsers\LocationParser;
17
use Parser;
18
19
/**
20
 * Class handling the #display_map rendering.
21
 *
22
 * @licence GNU GPL v2+
23
 * @author Jeroen De Dauw < [email protected] >
24
 * @author Kim Eik
25
 */
26
class DisplayMapRenderer {
27
28
	public $service;
29
30
	/**
31
	 * @var LocationParser
32
	 */
33
	private $locationParser;
34
35
	/**
36
	 * @var MediaWikiFileUrlFinder
37
	 */
38
	private $fileUrlFinder;
39
40
	/**
41
	 * @var WikitextParser
42
	 */
43
	private $wikitextParser;
44
	/**
45
	 * @var ElementJsonSerializer
46
	 */
47
	private $elementSerializer;
48
49 27
	public function __construct( MappingService $service = null ) {
50 27
		$this->service = $service;
51 27
	}
52
53
	/**
54
	 * Handles the request from the parser hook by doing the work that's common for all
55
	 * mapping services, calling the specific methods and finally returning the resulting output.
56
	 *
57
	 * @param MapData $mapData
58
	 * @param Parser $parser
59
	 *
60
	 * @return MapOutput
61
	 */
62 27
	public final function renderMap( MapData $mapData, Parser $parser ): MapOutput {
63 27
		$factory = \Maps\MapsFactory::globalInstance();
64
65 27
		$this->locationParser = $factory->newLocationParser();
66 27
		$this->fileUrlFinder = $factory->getFileUrlFinder();
67
68 27
		$this->wikitextParser = new WikitextParser( clone $parser );
69 27
		$this->elementSerializer = new ElementJsonSerializer( $this->wikitextParser );
70
71 27
		$mapData->setParameters( $this->handleMarkerData( $mapData->getParameters() ) );
72
73 27
		$outputBuilder = new MapOutputBuilder();
74
75 27
		return $outputBuilder->buildOutput( $this->service, $mapData );
0 ignored issues
show
Bug introduced by
It seems like $this->service can be null; however, buildOutput() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
76
	}
77
78
	/**
79
	 * Converts the data in the coordinates parameter to JSON-ready objects.
80
	 * These get stored in the locations parameter, and the coordinates on gets deleted.
81
	 */
82 27
	private function handleMarkerData( array $params ) {
83 27
		$params['centre'] = $this->getCenter( $params['centre'] );
84
85
		// FIXME: this parameter is google maps service specific
86 27
		if ( array_key_exists( 'wmsoverlay', $params ) && is_object( $params['wmsoverlay'] ) ) {
87
			$params['wmsoverlay'] = $params['wmsoverlay']->getJSONObject();
88
		}
89
90 27
		$params['locations'] = $this->getLocationJson( $params );
91
92 27
		unset( $params['coordinates'] );
93
94 27
		$this->handleShapeData( $params );
95
96 27
		return $params;
97
	}
98
99 27
	private function getCenter( $coordinatesOrAddress ) {
100 27
		if ( $coordinatesOrAddress === false ) {
101 27
			return false;
102
		}
103
104
		try {
105
			// FIXME: a Location makes no sense here, since the non-coordinate data is not used
106
			$location = $this->locationParser->parse( $coordinatesOrAddress );
107
		}
108
		catch ( \Exception $ex ) {
109
			// TODO: somehow report this to the user
110
			return false;
111
		}
112
113
		return $location->getJSONObject();
114
	}
115
116 27
	private function getLocationJson( array $params ) {
117 27
		$iconUrl = $this->fileUrlFinder->getUrlForFileName( $params['icon'] );
118 27
		$visitedIconUrl = $this->fileUrlFinder->getUrlForFileName( $params['visitedicon'] ?? '' );
119
120 27
		$locationJsonObjects = [];
121
122 27
		foreach ( $params['coordinates'] as $coordinatesOrAddress ) {
123
			try {
124 16
				$location = $this->locationParser->parse( $coordinatesOrAddress );
125
			}
126 1
			catch ( \Exception $ex ) {
127
				// TODO: somehow report this to the user
128 1
				continue;
129
			}
130
131 15
			$locationJsonObjects[] = $this->getLocationJsonObject(
132 15
				$location,
133
				$params,
134
				$iconUrl,
135
				$visitedIconUrl
136
			);
137
		}
138
139 27
		return $locationJsonObjects;
140
	}
141
142 15
	private function getLocationJsonObject( Location $location, array $params, $iconUrl, $visitedIconUrl ) {
143 15
		$jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl, '', '', $visitedIconUrl );
144
145 15
		$this->elementSerializer->titleAndText( $jsonObj );
146
147 15
		if ( isset( $jsonObj['inlineLabel'] ) ) {
148 1
			$jsonObj['inlineLabel'] = strip_tags(
149 1
				$this->wikitextParser->wikitextToHtml( $jsonObj['inlineLabel'] ),
150 1
				'<a><img>'
151
			);
152
		}
153
154 15
		return $jsonObj;
155
	}
156
157 27
	private function handleShapeData( array &$params ) {
158
		$textContainers = [
159 27
			&$params['lines'],
160 27
			&$params['polygons'],
161 27
			&$params['circles'],
162 27
			&$params['rectangles'],
163 27
			&$params['imageoverlays'], // FIXME: this is Google Maps specific!!
164
		];
165
166 27
		foreach ( $textContainers as &$textContainer ) {
167 27
			if ( is_array( $textContainer ) ) {
168 27
				foreach ( $textContainer as &$obj ) {
169 5
					$obj = $this->elementSerializer->elementToJson( $obj );
170
				}
171
			}
172
		}
173 27
	}
174
175
}
176