CachingGeocoder::geocode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\DataAccess;
6
7
use BagOStuff;
8
use DataValues\Geo\Values\LatLongValue;
9
use Jeroen\SimpleGeocoder\Geocoder;
10
11
/**
12
 * @since 5.0
13
 *
14
 * @licence GNU GPL v2+
15
 * @author HgO < [email protected] >
16
 */
17
class CachingGeocoder implements Geocoder {
18
19
	private $geocoder;
20
	private $cache;
21
	private $cacheTtl;
22
23 86
	public function __construct( Geocoder $geocoder, BagOStuff $cache, int $cacheTtl ) {
24 86
		$this->geocoder = $geocoder;
25 86
		$this->cache = $cache;
26 86
		$this->cacheTtl = $cacheTtl;
27 86
	}
28
29
	/**
30
	 * @return LatLongValue|null
31
	 */
32 76
	public function geocode( string $address ) {
33 76
		$key = $this->cache->makeKey( __CLASS__, $address );
34
35 76
		$coordinates = $this->cache->get( $key );
36
37
		// There was no entry in the cache, so we retrieve the coordinates
38 76
		if ( $coordinates === false ) {
39 7
			$coordinates = $this->geocoder->geocode( $address );
40
41 7
			$this->cache->set( $key, $coordinates, $this->cacheTtl );
42
		}
43
44 76
		return $coordinates;
45
	}
46
}
47