Completed
Push — killgeocoders ( 08fbcb )
by Jeroen De
06:57
created

LineParser   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A constructShapeFromLatLongValues() 0 3 1
B handleCommonParams() 0 26 5
A setBubbleDataFromParameter() 0 8 3
A setLinkFromParameter() 0 8 2
A isLinkParameter() 0 7 2
A __construct() 0 3 1
A setGeocoder() 0 3 1
A parse() 0 11 1
A parseCoordinates() 0 16 3
1
<?php
2
3
namespace Maps;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use Maps\Elements\Line;
7
use Maps\Geocoders\Geocoder;
8
use ValueParsers\StringValueParser;
9
use ValueParsers\ValueParser;
10
11
/**
12
 * ValueParser that parses the string representation of a line.
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 LineParser implements ValueParser {
21
22
	private $metaDataSeparator = '~';
23
24
	private $geocoder;
25
26
	public function __construct() {
27
		$this->geocoder = MapsFactory::newDefault()->newGeocoder();
28
	}
29
30
	public function setGeocoder( Geocoder $geocoder ) {
31
		$this->geocoder = $geocoder;
32
	}
33
34
	/**
35
	 * @see StringValueParser::stringParse
36
	 *
37
	 * @since 3.0
38
	 *
39
	 * @param string $value
40
	 *
41
	 * @return Line
42
	 */
43
	public function parse( $value ) {
44
		$parts = explode( $this->metaDataSeparator , $value );
45
46
		$line = $this->constructShapeFromLatLongValues( $this->parseCoordinates(
47
			explode( ':' , array_shift( $parts ) )
48
		) );
49
50
		$this->handleCommonParams( $parts, $line );
51
52
		return $line;
53
	}
54
55
	/**
56
	 * @since 3.0
57
	 *
58
	 * @param string[] $coordinateStrings
59
	 *
60
	 * @return LatLongValue[]
61
	 */
62
	protected function parseCoordinates( array $coordinateStrings ) {
63
		$coordinates = [];
64
65
		foreach ( $coordinateStrings as $coordinateString ) {
66
			$coordinate = $this->geocoder->geocode( $coordinateString );
67
68
			if ( $coordinate === null ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
69
				// TODO: good if the user knows something has been omitted
70
			}
71
			else {
72
				$coordinates[] = $coordinate;
73
			}
74
		}
75
76
		return $coordinates;
77
	}
78
79
	protected function constructShapeFromLatLongValues( array $locations ) {
80
		return new Line( $locations );
81
	}
82
83
	/**
84
	 * This method requires that parameters are positionally correct,
85
	 * 1. Link (one parameter) or bubble data (two parameters)
86
	 * 2. Stroke data (three parameters)
87
	 * 3. Fill data (two parameters)
88
	 * e.g ...title~text~strokeColor~strokeOpacity~strokeWeight~fillColor~fillOpacity
89
	 *
90
	 * @since 3.0
91
	 *
92
	 * @param array $params
93
	 * @param Line $line
94
	 */
95
	protected function handleCommonParams( array &$params, Line &$line ) {
96
		//Handle bubble and link parameters
97
98
		//create link data
99
		$linkOrTitle = array_shift( $params );
100
		if ( $link = $this->isLinkParameter( $linkOrTitle ) ) {
101
			$this->setLinkFromParameter( $line , $link );
102
		} else {
103
			//create bubble data
104
			$this->setBubbleDataFromParameter( $line , $params , $linkOrTitle );
105
		}
106
107
108
		//handle stroke parameters
109
		if ( $color = array_shift( $params ) ) {
110
			$line->setStrokeColor( $color );
111
		}
112
113
		if ( $opacity = array_shift( $params ) ) {
114
			$line->setStrokeOpacity( $opacity );
115
		}
116
117
		if ( $weight = array_shift( $params ) ) {
118
			$line->setStrokeWeight( $weight );
119
		}
120
	}
121
122
	protected function setBubbleDataFromParameter( Line &$line , &$params , $title ) {
123
		if ( $title ) {
124
			$line->setTitle( $title );
125
		}
126
		if ( $text = array_shift( $params ) ) {
127
			$line->setText( $text );
128
		}
129
	}
130
131
	protected function setLinkFromParameter( Line &$line , $link ) {
132
		if ( filter_var( $link , FILTER_VALIDATE_URL , FILTER_FLAG_SCHEME_REQUIRED ) ) {
133
			$line->setLink( $link );
134
		} else {
135
			$title = \Title::newFromText( $link );
136
			$line->setLink( $title->getFullURL() );
137
		}
138
	}
139
140
	/**
141
	 * Checks if a string is prefixed with link:
142
	 * @static
143
	 * @param $link
144
	 * @return bool|string
145
	 * @since 2.0
146
	 */
147
	private function isLinkParameter( $link ) {
148
		if ( strpos( $link , 'link:' ) === 0 ) {
149
			return substr( $link , 5 );
150
		}
151
152
		return false;
153
	}
154
155
}
156