Completed
Push — LocationParsers ( afa02e )
by Jeroen De
04:46
created

CircleParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 74.07%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 69
ccs 20
cts 27
cp 0.7407
rs 10
c 0
b 0
f 0

2 Methods

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