Completed
Push — vishual ( 38235a...99e673 )
by Jeroen De
09:25 queued 07:44
created

MapsFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 57.78%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 9
dl 0
loc 82
ccs 26
cts 45
cp 0.5778
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A newDefault() 0 3 1
A newLocationParser() 0 3 1
A newGeocoder() 0 13 2
A newFileFetcher() 0 3 1
A __construct() 0 4 1
A newCoreGeocoder() 0 21 5
A newGoogleGeocoder() 0 7 1
A getFileFetcher() 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
	private $mediaWikiServices;
23
24 26
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
25 26
		$this->settings = $settings;
26 26
		$this->mediaWikiServices = $mediaWikiServices;
27 26
	}
28
29 26
	public static function newDefault() {
30 26
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
31
	}
32
33 19
	public function newLocationParser(): LocationParser {
34 19
		return LocationParser::newInstance( $this->newGeocoder() );
35
	}
36
37 26
	public function newGeocoder(): Geocoder {
38 26
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
39
40 26
		if ( $this->settings['egMapsEnableGeoCache'] ) {
41 26
			return new CachingGeocoder(
42 26
				$geocoder,
43 26
				$this->getMediaWikiCache(),
44 26
				$this->settings['egMapsGeoCacheTtl']
45
			);
46
		}
47
48
		return $geocoder;
49
	}
50
51 26
	private function newCoreGeocoder(): Geocoder {
52 26
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
53 26
			case 'geonames':
54
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
55
					return $this->newGoogleGeocoder();
56
				}
57
58
				return new GeoNamesGeocoder(
59
					$this->newFileFetcher(),
60
					$this->settings['egMapsGeoNamesUser']
61
				);
62 26
			case 'google':
63
				return $this->newGoogleGeocoder();
64 26
			case 'nominatim':
65 26
				return new NominatimGeocoder(
66 26
					$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
		return $this->newFileFetcher();
83
	}
84
85 26
	private function newFileFetcher(): FileFetcher {
86 26
		return new MapsFileFetcher();
87
	}
88
89 26
	private function getMediaWikiCache(): \BagOStuff {
90 26
		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