Completed
Push — cln ( 863127...4377d0 )
by Jeroen De
09:58
created

CachingGeocoder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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