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