Completed
Push — vishual ( 73c053...0d0a79 )
by Jeroen De
06:15
created

MapsFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 9
dl 0
loc 82
rs 10
c 0
b 0
f 0
ccs 25
cts 40
cp 0.625

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A newDefault() 0 3 1
A newLocationParser() 0 3 1
A newGeocoder() 0 13 2
A newCoreGeocoder() 0 21 5
A newGoogleGeocoder() 0 7 1
A getFileFetcher() 0 3 1
A newFileFetcher() 0 3 1
A getMediaWikiCache() 0 3 1
A getPageContentFetcher() 0 6 1
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
use MediaWiki\MediaWikiServices;
14
15
/**
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class MapsFactory {
20
21
	private $settings;
22 26
	private $mediaWikiServices;
23 26
24 26
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
25
		$this->settings = $settings;
26 26
		$this->mediaWikiServices = $mediaWikiServices;
27 26
	}
28
29
	public static function newDefault() {
30 19
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
31 19
	}
32
33
	public function newLocationParser(): LocationParser {
34 26
		return LocationParser::newInstance( $this->newGeocoder() );
35 26
	}
36
37 26
	public function newGeocoder(): Geocoder {
38 26
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
39 26
40 26
		if ( $this->settings['egMapsEnableGeoCache'] ) {
41 26
			return new CachingGeocoder(
42
				$geocoder,
43
				$this->getMediaWikiCache(),
44
				$this->settings['egMapsGeoCacheTtl']
45
			);
46
		}
47
48 26
		return $geocoder;
49 26
	}
50 26
51
	private function newCoreGeocoder(): Geocoder {
52
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
53
			case 'geonames':
54
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
55
					return $this->newGoogleGeocoder();
56
				}
57
58
				return new GeoNamesGeocoder(
59 26
					$this->newFileFetcher(),
60
					$this->settings['egMapsGeoNamesUser']
61 26
				);
62 26
			case 'google':
63 26
				return $this->newGoogleGeocoder();
64
			case 'nominatim':
65
				return new NominatimGeocoder(
66
					$this->newFileFetcher()
67
				);
68
			default:
69
				return new NullGeocoder();
70
		}
71
	}
72
73
	private function newGoogleGeocoder(): Geocoder {
74
		return new GoogleGeocoder(
75
			$this->newFileFetcher(),
76
			$this->settings['egMapsGMaps3ApiKey'],
77
			$this->settings['egMapsGMaps3ApiVersion']
78
		);
79
	}
80
81
	public function getFileFetcher(): FileFetcher {
82 26
		return $this->newFileFetcher();
83 26
	}
84
85
	private function newFileFetcher(): FileFetcher {
86 26
		return new MapsFileFetcher();
87 26
	}
88
89
	private function getMediaWikiCache(): \BagOStuff {
90
		return wfGetCache( CACHE_ANYTHING );
91
	}
92
93
	public function getPageContentFetcher(): PageContentFetcher {
94
		return new PageContentFetcher(
95
			$this->mediaWikiServices->getTitleParser(),
96
			$this->mediaWikiServices->getRevisionLookup()
97
		);
98
	}
99
100
}
101