Completed
Push — cargo ( d35b5e...08c93e )
by Jeroen De
05:35
created

CargoOutputBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildOutputFromCargoData() 0 15 1
A processParameters() 0 8 1
A getParameterDefinitions() 0 3 1
A cargoValuesToMarkers() 0 12 3
A getCoordinateFieldNames() 0 11 3
A newMarker() 0 15 1
A getPropertyValuesToDisplay() 0 11 3
A shouldDisplayValue() 0 11 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Map\CargoFormat;
6
7
use CargoFieldDescription;
8
use DataValues\Geo\Values\LatLongValue;
9
use Maps\Map\MapOutput;
10
use Maps\Map\MapOutputBuilder;
11
use Maps\Map\Marker;
12
use Maps\MappingService;
13
use Maps\MappingServices;
14
use ParamProcessor\ParamDefinitionFactory;
15
use ParamProcessor\Processor;
16
17
class CargoOutputBuilder {
18
19
	private $outputBuilder;
20
	private $services;
21
	private $paramDefinitionFactory;
22
23
	public function __construct( MapOutputBuilder $outputBuilder, MappingServices $services, ParamDefinitionFactory $paramDefinitionFactory ) {
24
		$this->outputBuilder = $outputBuilder;
25
		$this->services = $services;
26
		$this->paramDefinitionFactory = $paramDefinitionFactory;
27
	}
28
29
	public function buildOutputFromCargoData( array $values, array $formattedValues, array $fieldDescriptions, array $displayParams ): MapOutput {
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
		$service = $this->services->getDefaultService();
31
32
		$mapData = $service->newMapDataFromProcessingResult( $this->processParameters( $service, $displayParams ) );
33
34
		$mapData->setMarkers( $this->cargoValuesToMarkers(
35
			$formattedValues,
36
			$fieldDescriptions
37
		) );
38
39
		return $this->outputBuilder->buildOutput(
40
			$service,
41
			$mapData
42
		);
43
	}
44
45
	private function processParameters( MappingService $service, array $displayParams ) {
46
		$processor = Processor::newDefault();
47
48
		$processor->setParameters( $displayParams );
49
		$processor->setParameterDefinitions( $this->getParameterDefinitions( $service ) );
50
51
		return $processor->processParameters();
52
	}
53
54
	private function getParameterDefinitions( MappingService $service ): array {
55
		return $this->paramDefinitionFactory->newDefinitionsFromArrays( $service->getParameterInfo() ) ;
56
	}
57
58
	/**
59
	 * @return Marker[]
60
	 */
61
	private function cargoValuesToMarkers( array $formattedValues, array $fieldDescriptions ): array {
62
		$coordinateFields = $this->getCoordinateFieldNames( $fieldDescriptions );
63
		$markers = [];
64
65
		foreach ( $formattedValues as $valuesRow ) {
66
			foreach ( $coordinateFields as $coordinateField ) {
67
				$markers[] = $this->newMarker( $valuesRow, $coordinateField, $coordinateFields );
68
			}
69
		}
70
71
		return $markers;
72
	}
73
74
	/**
75
	 * @param CargoFieldDescription[] $fieldDescriptions
76
	 * @return string[]
77
	 */
78
	private function getCoordinateFieldNames( array $fieldDescriptions ): array {
79
		$names = [];
80
81
		foreach ( $fieldDescriptions as $fieldName => $field ) {
82
			if ( $field->mType === 'Coordinates' ) {
83
				$names[] = str_replace( ' ', '_', $fieldName );
84
			}
85
		}
86
87
		return $names;
88
	}
89
90
	private function newMarker( array $valuesRow, string $coordinateField, array $coordinateFields ): Marker {
91
		$marker = new Marker( new LatLongValue(
92
			(float)$valuesRow[$coordinateField . '  lat'],
93
			(float)$valuesRow[$coordinateField . '  lon']
94
		) );
95
96
		$marker->setText(
97
			( new PopupContent(
98
				array_shift( $valuesRow ) ?? '',
99
				$this->getPropertyValuesToDisplay( $valuesRow, $coordinateFields )
100
			) )->getHtml()
101
		);
102
103
		return $marker;
104
	}
105
106
	private function getPropertyValuesToDisplay( array $valuesRow, array $coordinateFields ): array {
107
		$propertyValues = [];
108
109
		foreach ( $valuesRow as $name => $value ) {
110
			if ( $this->shouldDisplayValue( $name, $coordinateFields ) ) {
111
				$propertyValues[$name] = $value;
112
			}
113
		}
114
115
		return $propertyValues;
116
	}
117
118
	private function shouldDisplayValue( string $name, array $coordinateFields ): bool {
119
		$hiddenFields = [];
120
121
		foreach ( $coordinateFields as $field ) {
122
			$hiddenFields[] = $field;
123
			$hiddenFields[] = $field . '  lat';
124
			$hiddenFields[] = $field . '  lon';
125
		}
126
127
		return !in_array( $name, $hiddenFields );
128
	}
129
130
}
131