Completed
Push — master ( 81538e...c2bf57 )
by Jeroen De
10:06
created

CircleParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 67
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
B parse() 0 36 8
A stringToLatLongValue() 0 9 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\WikitextParsers;
6
7
use DataValues\Geo\Values\LatLongValue;
8
use Jeroen\SimpleGeocoder\Geocoder;
9
use Maps\LegacyModel\Circle;
10
use Maps\MapsFactory;
11
use ValueParsers\ParseException;
12
use ValueParsers\StringValueParser;
13
use ValueParsers\ValueParser;
14
15
/**
16
 * @since 3.0
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Kim Eik
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class CircleParser implements ValueParser {
23
24
	private $metaDataSeparator = '~';
25
26
	private $geocoder;
27
28
	public function __construct( $geocoder = null ) {
29
		$this->geocoder = $geocoder instanceof Geocoder ? $geocoder : MapsFactory::globalInstance()->getGeocoder();
30
	}
31
32
	/**
33
	 * @see StringValueParser::stringParse
34
	 *
35
	 * @since 3.0
36
	 *
37
	 * @param string $value
38
	 *
39
	 * @return Circle
40
	 */
41
	public function parse( $value ) {
42
		$metaData = explode( $this->metaDataSeparator, $value );
43
		$circleData = explode( ':', array_shift( $metaData ) );
44
45
		$circle = new Circle( $this->stringToLatLongValue( $circleData[0] ), (float)$circleData[1] );
46
47
		if ( $metaData !== [] ) {
48
			$circle->setTitle( array_shift( $metaData ) );
49
		}
50
51
		if ( $metaData !== [] ) {
52
			$circle->setText( array_shift( $metaData ) );
53
		}
54
55
		if ( $metaData !== [] ) {
56
			$circle->setStrokeColor( array_shift( $metaData ) );
57
		}
58
59
		if ( $metaData !== [] ) {
60
			$circle->setStrokeOpacity( array_shift( $metaData ) );
61
		}
62
63
		if ( $metaData !== [] ) {
64
			$circle->setStrokeWeight( array_shift( $metaData ) );
65
		}
66
67
		if ( $metaData !== [] ) {
68
			$circle->setFillColor( array_shift( $metaData ) );
69
		}
70
71
		if ( $metaData !== [] ) {
72
			$circle->setFillOpacity( array_shift( $metaData ) );
73
		}
74
75
		return $circle;
76
	}
77
78
	private function stringToLatLongValue( string $location ): LatLongValue {
79
		$latLong = $this->geocoder->geocode( $location );
80
81
		if ( $latLong === null ) {
82
			throw new ParseException( 'Failed to parse or geocode' );
83
		}
84
85
		return $latLong;
86
	}
87
88
}
89