Completed
Push — move ( 04f5b5...ed2c91 )
by Jeroen De
17:58 queued 15:41
created

MapsFactory::newGeocoder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.0078
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use FileFetcher\FileFetcher;
8
use Jeroen\SimpleGeocoder\Geocoder;
9
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder;
10
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder;
11
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder;
12
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder;
13
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder;
14
use Maps\Geocoders\CachingGeocoder;
15
use Maps\Presentation\WikitextParsers\LocationParser;
16
use MediaWiki\MediaWikiServices;
17
18
/**
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class MapsFactory {
23
24
	private $settings;
25
	private $mediaWikiServices;
26
27 100
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
28 100
		$this->settings = $settings;
29 100
		$this->mediaWikiServices = $mediaWikiServices;
30 100
	}
31
32 100
	public static function newDefault(): self {
33 100
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
34
	}
35
36
	/**
37
	 * Only for legacy code where dependency injection is not possible
38
	 */
39 17
	public static function globalInstance(): self {
40 17
		static $instance = null;
41
42 17
		if ( $instance === null ) {
43 1
			$instance = self::newDefault();
44
		}
45
46 17
		return $instance;
47
	}
48
49 19
	public function newLocationParser(): LocationParser {
50 19
		return LocationParser::newInstance( $this->newGeocoder() );
51
	}
52
53 100
	public function newGeocoder(): Geocoder {
54 100
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
55
56 100
		if ( $this->settings['egMapsEnableGeoCache'] ) {
57 100
			return new CachingGeocoder(
58 100
				$geocoder,
59 100
				$this->getMediaWikiCache(),
60 100
				$this->settings['egMapsGeoCacheTtl']
61
			);
62
		}
63
64
		return $geocoder;
65
	}
66
67 100
	private function newCoreGeocoder(): Geocoder {
68 100
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
69 100
			case 'geonames':
70
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
71
					return $this->newGoogleGeocoder();
72
				}
73
74
				return new GeoNamesGeocoder(
75
					$this->newFileFetcher(),
76
					$this->settings['egMapsGeoNamesUser']
77
				);
78 100
			case 'google':
79
				return $this->newGoogleGeocoder();
80 100
			case 'nominatim':
81 100
				return new NominatimGeocoder(
82 100
					$this->newFileFetcher()
83
				);
84
			default:
85
				return new NullGeocoder();
86
		}
87
	}
88
89
	private function newGoogleGeocoder(): Geocoder {
90
		return new GoogleGeocoder(
91
			$this->newFileFetcher(),
92
			$this->settings['egMapsGMaps3ApiKey'],
93
			$this->settings['egMapsGMaps3ApiVersion']
94
		);
95
	}
96
97
	public function getFileFetcher(): FileFetcher {
98
		return $this->newFileFetcher();
99
	}
100
101 100
	private function newFileFetcher(): FileFetcher {
102 100
		return new MapsFileFetcher();
103
	}
104
105 100
	private function getMediaWikiCache(): \BagOStuff {
106 100
		return wfGetCache( CACHE_ANYTHING );
107
	}
108
109
	public function getPageContentFetcher(): PageContentFetcher {
110
		return new PageContentFetcher(
111
			$this->mediaWikiServices->getTitleParser(),
112
			$this->mediaWikiServices->getRevisionLookup()
113
		);
114
	}
115
116 17
	public function getCoordinateFormatter(): CoordinateFormatter {
117 17
		return new CoordinateFormatter();
118
	}
119
120
}
121