Completed
Push — newparam ( 72433c...c5fad3 )
by Jeroen De
01:21
created

MapsFactory   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 18

Test Coverage

Coverage 71.26%

Importance

Changes 0
Metric Value
wmc 25
lcom 2
cbo 18
dl 0
loc 156
ccs 62
cts 87
cp 0.7126
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A newDefault() 0 3 1
A globalInstance() 0 9 2
A newLocationParser() 0 6 1
A getGeocoder() 0 13 2
A newCoreGeocoder() 0 21 5
A newGoogleGeocoder() 0 7 1
A getFileFetcher() 0 3 1
A newFileFetcher() 0 3 1
A getGeoJsonFileFetcher() 0 10 2
A getMediaWikiCache() 0 3 1
A getCoordinateFormatter() 0 3 1
A getFileUrlFinder() 0 3 1
A getMappingServices() 0 8 1
A getDisplayMapFunction() 0 5 1
A getParamDefinitionFactory() 0 16 1
A getMediaWikiSimpleCache() 0 6 1
A getPageContentFetcher() 0 6 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use DataValues\Geo\Parsers\LatLongParser;
8
use FileFetcher\Cache\Factory as CacheFactory;
9
use FileFetcher\FileFetcher;
10
use Jeroen\SimpleGeocoder\Geocoder;
11
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder;
12
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder;
13
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder;
14
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder;
15
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder;
16
use Maps\DataAccess\CachingGeocoder;
17
use Maps\DataAccess\JsonFileParser;
18
use Maps\DataAccess\MapsFileFetcher;
19
use Maps\DataAccess\MediaWikiFileUrlFinder;
20
use Maps\DataAccess\PageContentFetcher;
21
use Maps\MediaWiki\ParserHooks\DisplayMapFunction;
22
use Maps\Presentation\CoordinateFormatter;
23
use Maps\Presentation\WikitextParsers\CircleParser;
24
use Maps\Presentation\WikitextParsers\DistanceParser;
25
use Maps\Presentation\WikitextParsers\ImageOverlayParser;
26
use Maps\Presentation\WikitextParsers\LineParser;
27
use Maps\Presentation\WikitextParsers\LocationParser;
28
use Maps\Presentation\WikitextParsers\PolygonParser;
29
use Maps\Presentation\WikitextParsers\RectangleParser;
30
use Maps\Presentation\WikitextParsers\WmsOverlayParser;
31
use MediaWiki\MediaWikiServices;
32
use ParamProcessor\ParamDefinitionFactory;
33
use SimpleCache\Cache\Cache;
34
use SimpleCache\Cache\MediaWikiCache;
35
36
/**
37
 * @licence GNU GPL v2+
38
 * @author Jeroen De Dauw < [email protected] >
39
 */
40
class MapsFactory {
41
42
	private $settings;
43
	private $mediaWikiServices;
44
45 51
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
46 51
		$this->settings = $settings;
47 51
		$this->mediaWikiServices = $mediaWikiServices;
48 51
	}
49
50 51
	public static function newDefault(): self {
51 51
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
52
	}
53
54
	/**
55
	 * Only for legacy code where dependency injection is not possible
56
	 */
57 112
	public static function globalInstance(): self {
58 112
		static $instance = null;
59
60 112
		if ( $instance === null ) {
61
			$instance = self::newDefault();
62
		}
63
64 112
		return $instance;
65
	}
66
67 22
	public function newLocationParser(): LocationParser {
68 22
		return LocationParser::newInstance(
69 22
			$this->getGeocoder(),
70 22
			$this->getFileUrlFinder()
71
		);
72
	}
73
74 103
	public function getGeocoder(): Geocoder {
75 103
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
76
77 103
		if ( $this->settings['egMapsEnableGeoCache'] ) {
78 103
			return new CachingGeocoder(
79 103
				$geocoder,
80 103
				$this->getMediaWikiCache(),
81 103
				$this->settings['egMapsGeoCacheTtl']
82
			);
83
		}
84
85
		return $geocoder;
86
	}
87
88 103
	private function newCoreGeocoder(): Geocoder {
89 103
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
90 103
			case 'geonames':
91
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
92
					return $this->newGoogleGeocoder();
93
				}
94
95
				return new GeoNamesGeocoder(
96
					$this->newFileFetcher(),
97
					$this->settings['egMapsGeoNamesUser']
98
				);
99 103
			case 'google':
100
				return $this->newGoogleGeocoder();
101 103
			case 'nominatim':
102 103
				return new NominatimGeocoder(
103 103
					$this->newFileFetcher()
104
				);
105
			default:
106
				return new NullGeocoder();
107
		}
108
	}
109
110
	private function newGoogleGeocoder(): Geocoder {
111
		return new GoogleGeocoder(
112
			$this->newFileFetcher(),
113
			$this->settings['egMapsGMaps3ApiKey'],
114
			$this->settings['egMapsGMaps3ApiVersion']
115
		);
116
	}
117
118
	public function getFileFetcher(): FileFetcher {
119
		return $this->newFileFetcher();
120
	}
121
122 103
	private function newFileFetcher(): FileFetcher {
123 103
		return new MapsFileFetcher();
124
	}
125
126
	public function getGeoJsonFileFetcher(): FileFetcher {
127
		if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) {
128
			return $this->getFileFetcher();
129
		}
130
131
		return ( new CacheFactory() )->newJeroenSimpleCacheFetcher(
132
			$this->getFileFetcher(),
133
			$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] )
134
		);
135
	}
136
137
	private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache {
138
		return new MediaWikiCache(
139
			$this->getMediaWikiCache(),
140
			$ttlInSeconds
141
		);
142
	}
143
144 103
	private function getMediaWikiCache(): \BagOStuff {
145 103
		return wfGetCache( CACHE_ANYTHING );
146
	}
147
148 1
	public function getPageContentFetcher(): PageContentFetcher {
149 1
		return new PageContentFetcher(
150 1
			$this->mediaWikiServices->getTitleParser(),
151 1
			$this->mediaWikiServices->getRevisionLookup()
152
		);
153
	}
154
155 19
	public function getCoordinateFormatter(): CoordinateFormatter {
156 19
		return new CoordinateFormatter();
157
	}
158
159 81
	public function getFileUrlFinder(): FileUrlFinder {
160 81
		return new MediaWikiFileUrlFinder();
161
	}
162
163 21
	public function getMappingServices(): MappingServices {
164 21
		return new MappingServices(
165 21
			$this->settings['egMapsAvailableServices'],
166 21
			$this->settings['egMapsDefaultService'],
167 21
			new GoogleMapsService(),
168 21
			new LeafletService()
169
		);
170
	}
171
172 21
	public function getDisplayMapFunction(): DisplayMapFunction {
173 21
		return new DisplayMapFunction(
174 21
			$this->getMappingServices()
175
		);
176
	}
177
178 87
	public function getParamDefinitionFactory(): ParamDefinitionFactory {
179 87
		$factory = ParamDefinitionFactory::newDefault();
180
181 87
		$factory->registerType( 'coordinate', [ 'string-parser' => LatLongParser::class ] );
182 87
		$factory->registerType( 'mapslocation', [ 'string-parser' => LocationParser::class ] );
183 87
		$factory->registerType( 'mapsline', [ 'string-parser' => LineParser::class ] );
184 87
		$factory->registerType( 'mapscircle', [ 'string-parser' => CircleParser::class ] );
185 87
		$factory->registerType( 'mapsrectangle', [ 'string-parser' => RectangleParser::class ] );
186 87
		$factory->registerType( 'mapspolygon', [ 'string-parser' => PolygonParser::class ] );
187 87
		$factory->registerType( 'distance', [ 'string-parser' => DistanceParser::class ] );
188 87
		$factory->registerType( 'wmsoverlay', [ 'string-parser' => WmsOverlayParser::class ] );
189 87
		$factory->registerType( 'mapsimageoverlay', [ 'string-parser' => ImageOverlayParser::class ] );
190 87
		$factory->registerType( 'jsonfile', [ 'string-parser' => JsonFileParser::class ] );
191
192 87
		return $factory;
193
	}
194
195
}
196