Completed
Push — leaflet-attribution ( 0121c0 )
by Peter
04:35
created

CachingGeocoder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Maps\Geocoders;
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
20
	public function __construct( Geocoder $geocoder, BagOStuff $cache ) {
21
		$this->geocoder = $geocoder;
22
		$this->cache = $cache;
23
	}
24
25
	/**
26
	 * @return LatLongValue|null
27
	 */
28
	public function geocode( string $address ) {
29
		$key = $this->cache->makeKey( __CLASS__, $address );
30
31
		$coordinates = $this->cache->get( $key );
32
33
		// There was no entry in the cache, so we retrieve the coordinates
34
		if ( $coordinates === false ) {
35
			$coordinates = $this->geocoder->geocode( $address );
36
37
			$this->cache->set( $key, $coordinates, BagOStuff::TTL_DAY );
38
		}
39
40
		return $coordinates;
41
	}
42
}
43