Completed
Pull Request — master (#603)
by Jeroen De
01:20
created

SubObjectBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 56
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSubObjectsFromGeoJson() 0 6 1
A featureCollectionToSubObjects() 0 9 3
A pointToSubobject() 0 24 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\DataAccess\GeoJsonStore;
6
7
use GeoJson\Feature\FeatureCollection;
8
use GeoJson\GeoJson;
9
use GeoJson\Geometry\Point;
10
use Title;
11
12
class SubObjectBuilder {
13
14
	private $subjectPage;
15
	private $pointCount = 0;
16
17 6
	public function __construct( Title $subjectPage ) {
18 6
		$this->subjectPage = $subjectPage;
19 6
	}
20
21
	/**
22
	 * @return SubObject[]
23
	 */
24 6
	public function getSubObjectsFromGeoJson( string $jsonString ) {
25 6
		$json = json_decode( $jsonString );
26 6
		$geoJson = GeoJson::jsonUnserialize( $json );
27
28 6
		return iterator_to_array( $this->featureCollectionToSubObjects( $geoJson ) );
29
	}
30
31 6
	private function featureCollectionToSubObjects( FeatureCollection $featureCollection ) {
32 6
		foreach ( $featureCollection->getFeatures() as $feature ) {
33 2
			$geometry = $feature->getGeometry();
34
35 2
			if ( $geometry instanceof Point ) {
36 2
				yield $this->pointToSubobject( $geometry, $feature->getProperties() ?? [] );
37
			}
38
		}
39 6
	}
40
41 2
	private function pointToSubobject( Point $point, array $properties ): SubObject {
42 2
		$subObject = new SubObject( 'Point_' . ++$this->pointCount );
43
44 2
		$subObject->addPropertyValuePair(
45 2
			'HasCoordinates',
46 2
			new \SMWDIGeoCoord( $point->getCoordinates()[1], $point->getCoordinates()[0] )
47
		);
48
49 2
		if ( array_key_exists( 'description', $properties ) ) {
50 1
			$subObject->addPropertyValuePair(
51 1
				'HasDescription',
52 1
				new \SMWDIBlob( $properties['description'] )
53
			);
54
		}
55
56 2
		if ( array_key_exists( 'title', $properties ) ) {
57 1
			$subObject->addPropertyValuePair(
58 1
				'HasTitle',
59 1
				new \SMWDIBlob( $properties['title'] )
60
			);
61
		}
62
63 2
		return $subObject;
64
	}
65
66
67
}
68