Completed
Push — MediaWikiFileUrlFinderTest ( d58bee )
by Jeroen De
03:36
created

MapsFactory::newLocationParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 100
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
32 100
		$this->settings = $settings;
33 100
		$this->mediaWikiServices = $mediaWikiServices;
34 100
	}
35
36 100
	public static function newDefault(): self {
37 100
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
38
	}
39
40
	/**
41
	 * Only for legacy code where dependency injection is not possible
42
	 */
43 40
	public static function globalInstance(): self {
44 40
		static $instance = null;
45
46 40
		if ( $instance === null ) {
47
			$instance = self::newDefault();
48
		}
49
50 40
		return $instance;
51
	}
52
53 19
	public function newLocationParser(): LocationParser {
54 19
		return LocationParser::newInstance( $this->newGeocoder() );
55
	}
56
57 100
	public function newGeocoder(): Geocoder {
58 100
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
59
60 100
		if ( $this->settings['egMapsEnableGeoCache'] ) {
61 100
			return new CachingGeocoder(
62 100
				$geocoder,
63 100
				$this->getMediaWikiCache(),
64 100
				$this->settings['egMapsGeoCacheTtl']
65
			);
66
		}
67
68
		return $geocoder;
69
	}
70
71 100
	private function newCoreGeocoder(): Geocoder {
72 100
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
73 100
			case 'geonames':
74
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
75
					return $this->newGoogleGeocoder();
76
				}
77
78
				return new GeoNamesGeocoder(
79
					$this->newFileFetcher(),
80
					$this->settings['egMapsGeoNamesUser']
81
				);
82 100
			case 'google':
83
				return $this->newGoogleGeocoder();
84 100
			case 'nominatim':
85 100
				return new NominatimGeocoder(
86 100
					$this->newFileFetcher()
87
				);
88
			default:
89
				return new NullGeocoder();
90
		}
91
	}
92
93
	private function newGoogleGeocoder(): Geocoder {
94
		return new GoogleGeocoder(
95
			$this->newFileFetcher(),
96
			$this->settings['egMapsGMaps3ApiKey'],
97
			$this->settings['egMapsGMaps3ApiVersion']
98
		);
99
	}
100
101
	public function getFileFetcher(): FileFetcher {
102
		return $this->newFileFetcher();
103
	}
104
105 100
	private function newFileFetcher(): FileFetcher {
106 100
		return new MapsFileFetcher();
107
	}
108
109 100
	private function getMediaWikiCache(): \BagOStuff {
110 100
		return wfGetCache( CACHE_ANYTHING );
111
	}
112
113
	public function getPageContentFetcher(): PageContentFetcher {
114
		return new PageContentFetcher(
115
			$this->mediaWikiServices->getTitleParser(),
116
			$this->mediaWikiServices->getRevisionLookup()
117
		);
118
	}
119
120 17
	public function getCoordinateFormatter(): CoordinateFormatter {
121 17
		return new CoordinateFormatter();
122
	}
123
124 23
	public function getFileUrlFinder(): FileUrlFinder {
125 23
		return new MediaWikiFileUrlFinder();
126
	}
127
128
}
129