Completed
Push — geonames-geocoder ( 9fbfea )
by Jeroen De
03:58
created

GeoNamesGeocoder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 52
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A geocode() 0 17 4
A getRequestUrl() 0 6 1
A getXmlElementValue() 0 5 2
1
<?php
2
3
namespace Maps\Geocoders;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use FileFetcher\FileFetcher;
7
use FileFetcher\FileFetchingException;
8
9
/**
10
 * @since 4.5
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class GeoNamesGeocoder implements Geocoder {
16
17
	private $fileFetcher;
18
	private $geoNamesUser;
19
20 4
	public function __construct( FileFetcher $fileFetcher, $geoNamesUser ) {
21 4
		$this->fileFetcher = $fileFetcher;
22 4
		$this->geoNamesUser = $geoNamesUser;
23 4
	}
24
25
	/**
26
	 * @param string $address
27
	 *
28
	 * @return LatLongValue|null
29
	 */
30 4
	public function geocode( $address ) {
31
		try {
32 4
			$response = $this->fileFetcher->fetchFile( $this->getRequestUrl( $address ) );
33
		}
34 1
		catch ( FileFetchingException $ex ) {
35 1
			return null;
36
		}
37
38 3
		$lon = self::getXmlElementValue( $response, 'lng' );
39 3
		$lat = self::getXmlElementValue( $response, 'lat' );
40
41 3
		if ( !$lon || !$lat ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lon of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $lat of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
42 2
			return null;
43
		}
44
45 1
		return new LatLongValue( (float)$lat, (float)$lon );
46
	}
47
48
	/**
49
	 * @param string $address
50
	 *
51
	 * @return string
52
	 */
53 4
	private function getRequestUrl( $address ) {
54
		return 'http://api.geonames.org/search?q='
55 4
			   . urlencode( $address )
56 4
			   . '&maxRows=1&username='
57 4
			   . urlencode( $this->geoNamesUser );
58
	}
59
60 3
	private function getXmlElementValue( $xml, $tagName ) {
61 3
		$match = [];
62 3
		preg_match( "/<$tagName>(.*?)<\/$tagName>/", $xml, $match );
63 3
		return count( $match ) > 1 ? $match[1] : false;
64
	}
65
66
}
67