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

MapsFactory::newCoreGeocoder()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 0
1
<?php
2
3
namespace Maps;
4
5
use FileFetcher\FileFetcher;
6
use Jeroen\SimpleGeocoder\Geocoder;
7
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder;
8
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder;
9
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder;
10
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder;
11
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder;
12
use Maps\Geocoders\CachingGeocoder;
13
14
/**
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class MapsFactory {
19
20
	private $settings;
21
22
	private function __construct( array $settings ) {
23
		$this->settings = $settings;
24
	}
25
26
	public static function newDefault() {
27
		return new self( $GLOBALS );
28
	}
29
30
	public function newLocationParser(): LocationParser {
31
		return LocationParser::newInstance( $this->newGeocoder() );
32
	}
33
34
	public function newGeocoder(): Geocoder {
35
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
36
37
		if ( $this->settings['egMapsEnableGeoCache'] ) {
38
			return new CachingGeocoder(
39
				$geocoder,
40
				$this->getMediaWikiCache()
41
			);
42
		}
43
44
		return $geocoder;
45
	}
46
47
	private function newCoreGeocoder(): Geocoder {
48
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
49
			case 'geonames':
50
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
51
					return $this->newGoogleGeocoder();
52
				}
53
54
				return new GeoNamesGeocoder(
55
					$this->newFileFetcher(),
56
					$this->settings['egMapsGeoNamesUser']
57
				);
58
			case 'google':
59
				return $this->newGoogleGeocoder();
60
			case 'nominatim':
61
				return new NominatimGeocoder(
62
					$this->newFileFetcher()
63
				);
64
			default:
65
				return new NullGeocoder();
66
		}
67
	}
68
69
	private function newGoogleGeocoder(): Geocoder {
70
		return new GoogleGeocoder(
71
			$this->newFileFetcher(),
72
			$this->settings['egMapsGMaps3ApiKey'],
73
			$this->settings['egMapsGMaps3ApiVersion']
74
		);
75
	}
76
77
	private function newFileFetcher(): FileFetcher {
78
		return new MapsFileFetcher();
79
	}
80
81
	private function getMediaWikiCache(): \BagOStuff {
82
		return wfGetCache( CACHE_ANYTHING );
83
	}
84
85
}
86