Completed
Push — master ( 21f028...112b84 )
by Jeroen De
03:13
created

MapsFactory::getGeocoder()   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\DataAccess\CachingGeocoder;
15
use Maps\DataAccess\MapsFileFetcher;
16
use Maps\DataAccess\MediaWikiFileUrlFinder;
17
use Maps\DataAccess\PageContentFetcher;
18
use Maps\Presentation\CoordinateFormatter;
19
use Maps\Presentation\WikitextParsers\LocationParser;
20
use MediaWiki\MediaWikiServices;
21
22
/**
23
 * @licence GNU GPL v2+
24
 * @author Jeroen De Dauw < [email protected] >
25
 */
26
class MapsFactory {
27
28
	private $settings;
29
	private $mediaWikiServices;
30
31 49
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
32 49
		$this->settings = $settings;
33 49
		$this->mediaWikiServices = $mediaWikiServices;
34 49
	}
35
36 49
	public static function newDefault(): self {
37 49
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
38
	}
39
40
	/**
41
	 * Only for legacy code where dependency injection is not possible
42
	 */
43 75
	public static function globalInstance(): self {
44 75
		static $instance = null;
45
46 75
		if ( $instance === null ) {
47
			$instance = self::newDefault();
48
		}
49
50 75
		return $instance;
51
	}
52
53 20
	public function newLocationParser(): LocationParser {
54 20
		return LocationParser::newInstance(
55 20
			$this->getGeocoder(),
56 20
			$this->getFileUrlFinder()
57
		);
58
	}
59
60 101
	public function getGeocoder(): Geocoder {
61 101
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
62
63 101
		if ( $this->settings['egMapsEnableGeoCache'] ) {
64 101
			return new CachingGeocoder(
65 101
				$geocoder,
66 101
				$this->getMediaWikiCache(),
67 101
				$this->settings['egMapsGeoCacheTtl']
68
			);
69
		}
70
71
		return $geocoder;
72
	}
73
74 101
	private function newCoreGeocoder(): Geocoder {
75 101
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
76 101
			case 'geonames':
77
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
78
					return $this->newGoogleGeocoder();
79
				}
80
81
				return new GeoNamesGeocoder(
82
					$this->newFileFetcher(),
83
					$this->settings['egMapsGeoNamesUser']
84
				);
85 101
			case 'google':
86
				return $this->newGoogleGeocoder();
87 101
			case 'nominatim':
88 101
				return new NominatimGeocoder(
89 101
					$this->newFileFetcher()
90
				);
91
			default:
92
				return new NullGeocoder();
93
		}
94
	}
95
96
	private function newGoogleGeocoder(): Geocoder {
97
		return new GoogleGeocoder(
98
			$this->newFileFetcher(),
99
			$this->settings['egMapsGMaps3ApiKey'],
100
			$this->settings['egMapsGMaps3ApiVersion']
101
		);
102
	}
103
104
	public function getFileFetcher(): FileFetcher {
105
		return $this->newFileFetcher();
106
	}
107
108 101
	private function newFileFetcher(): FileFetcher {
109 101
		return new MapsFileFetcher();
110
	}
111
112 101
	private function getMediaWikiCache(): \BagOStuff {
113 101
		return wfGetCache( CACHE_ANYTHING );
114
	}
115
116
	public function getPageContentFetcher(): PageContentFetcher {
117
		return new PageContentFetcher(
118
			$this->mediaWikiServices->getTitleParser(),
119
			$this->mediaWikiServices->getRevisionLookup()
120
		);
121
	}
122
123 17
	public function getCoordinateFormatter(): CoordinateFormatter {
124 17
		return new CoordinateFormatter();
125
	}
126
127 79
	public function getFileUrlFinder(): FileUrlFinder {
128 79
		return new MediaWikiFileUrlFinder();
129
	}
130
131
}
132