CachingGeocoder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 30
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A geocode() 0 14 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