CircleParser::parse()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.1669

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 14
cts 19
cp 0.7368
rs 7.9128
c 0
b 0
f 0
cc 8
nc 128
nop 1
crap 9.1669
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 3
	public function __construct( $geocoder = null ) {
29 3
		$this->geocoder = $geocoder instanceof Geocoder ? $geocoder : MapsFactory::globalInstance()->getGeocoder();
30 3
	}
31
32
	/**
33
	 * @see StringValueParser::stringParse
34
	 *
35
	 * @since 3.0
36
	 *
37
	 * @param string $value
38
	 *
39
	 * @return Circle
40
	 */
41 3
	public function parse( $value ) {
42 3
		$metaData = explode( $this->metaDataSeparator, $value );
43 3
		$circleData = explode( ':', array_shift( $metaData ) );
44
45 3
		$circle = new Circle( $this->stringToLatLongValue( $circleData[0] ), (float)$circleData[1] );
46
47 3
		if ( $metaData !== [] ) {
48 2
			$circle->setTitle( array_shift( $metaData ) );
49
		}
50
51 3
		if ( $metaData !== [] ) {
52 1
			$circle->setText( array_shift( $metaData ) );
53
		}
54
55 3
		if ( $metaData !== [] ) {
56
			$circle->setStrokeColor( array_shift( $metaData ) );
57
		}
58
59 3
		if ( $metaData !== [] ) {
60
			$circle->setStrokeOpacity( array_shift( $metaData ) );
61
		}
62
63 3
		if ( $metaData !== [] ) {
64
			$circle->setStrokeWeight( array_shift( $metaData ) );
65
		}
66
67 3
		if ( $metaData !== [] ) {
68
			$circle->setFillColor( array_shift( $metaData ) );
69
		}
70
71 3
		if ( $metaData !== [] ) {
72
			$circle->setFillOpacity( array_shift( $metaData ) );
73
		}
74
75 3
		return $circle;
76
	}
77
78 3
	private function stringToLatLongValue( string $location ): LatLongValue {
79 3
		$latLong = $this->geocoder->geocode( $location );
80
81 3
		if ( $latLong === null ) {
82
			throw new ParseException( 'Failed to parse or geocode' );
83
		}
84
85 3
		return $latLong;
86
	}
87
88
}
89