Completed
Push — master ( 87a9ee...38aa89 )
by Jeroen De
05:37 queued 03:24
created

MapsFactory::newSemanticMapsSetup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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\GeoJsonFetcher;
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 55
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
46 55
		$this->settings = $settings;
47 55
		$this->mediaWikiServices = $mediaWikiServices;
48 55
	}
49
50 55
	public static function newDefault(): self {
51 55
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
52
	}
53
54
	/**
55
	 * Only for legacy code where dependency injection is not possible
56
	 */
57 116
	public static function globalInstance(): self {
58 116
		static $instance = null;
59
60 116
		if ( $instance === null ) {
61
			$instance = self::newDefault();
62
		}
63
64 116
		return $instance;
65
	}
66
67 26
	public function newLocationParser(): LocationParser {
68 26
		return LocationParser::newInstance(
69 26
			$this->getGeocoder(),
70 26
			$this->getFileUrlFinder()
71
		);
72
	}
73
74 107
	public function getGeocoder(): Geocoder {
75 107
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
76
77 107
		if ( $this->settings['egMapsEnableGeoCache'] ) {
78 107
			return new CachingGeocoder(
79 107
				$geocoder,
80 107
				$this->getMediaWikiCache(),
81 107
				$this->settings['egMapsGeoCacheTtl']
82
			);
83
		}
84
85
		return $geocoder;
86
	}
87
88 107
	private function newCoreGeocoder(): Geocoder {
89 107
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
90 107
			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 107
			case 'google':
100
				return $this->newGoogleGeocoder();
101 107
			case 'nominatim':
102 107
				return new NominatimGeocoder(
103 107
					$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 3
	public function getFileFetcher(): FileFetcher {
119 3
		return $this->newFileFetcher();
120
	}
121
122 107
	private function newFileFetcher(): FileFetcher {
123 107
		return new MapsFileFetcher();
124
	}
125
126 3
	public function getGeoJsonFileFetcher(): FileFetcher {
127 3
		if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) {
128 3
			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 107
	private function getMediaWikiCache(): \BagOStuff {
145 107
		return wfGetCache( CACHE_ANYTHING );
146
	}
147
148 2
	public function getPageContentFetcher(): PageContentFetcher {
149 2
		return new PageContentFetcher(
150 2
			$this->mediaWikiServices->getTitleParser(),
151 2
			$this->mediaWikiServices->getRevisionLookup()
152
		);
153
	}
154
155 20
	public function getCoordinateFormatter(): CoordinateFormatter {
156 20
		return new CoordinateFormatter();
157
	}
158
159 85
	public function getFileUrlFinder(): FileUrlFinder {
160 85
		return new MediaWikiFileUrlFinder();
161
	}
162
163 24
	public function getMappingServices(): MappingServices {
164 24
		return new MappingServices(
165 24
			$this->settings['egMapsAvailableServices'],
166 24
			$this->settings['egMapsDefaultService'],
167 24
			new GoogleMapsService(),
168 24
			new LeafletService()
169
		);
170
	}
171
172 24
	public function getDisplayMapFunction(): DisplayMapFunction {
173 24
		return new DisplayMapFunction(
174 24
			$this->getMappingServices()
175
		);
176
	}
177
178 90
	public function getParamDefinitionFactory(): ParamDefinitionFactory {
179 90
		$factory = ParamDefinitionFactory::newDefault();
180
181 90
		$factory->registerType( 'coordinate', [ 'string-parser' => LatLongParser::class ] );
182 90
		$factory->registerType( 'mapslocation', [ 'string-parser' => LocationParser::class ] );
183 90
		$factory->registerType( 'mapsline', [ 'string-parser' => LineParser::class ] );
184 90
		$factory->registerType( 'mapscircle', [ 'string-parser' => CircleParser::class ] );
185 90
		$factory->registerType( 'mapsrectangle', [ 'string-parser' => RectangleParser::class ] );
186 90
		$factory->registerType( 'mapspolygon', [ 'string-parser' => PolygonParser::class ] );
187 90
		$factory->registerType( 'distance', [ 'string-parser' => DistanceParser::class ] );
188 90
		$factory->registerType( 'wmsoverlay', [ 'string-parser' => WmsOverlayParser::class ] );
189 90
		$factory->registerType( 'mapsimageoverlay', [ 'string-parser' => ImageOverlayParser::class ] );
190
191 90
		return $factory;
192
	}
193
194 3
	public function newGeoJsonFetcher( FileFetcher $fileFetcher = null ): GeoJsonFetcher {
195 3
		return new GeoJsonFetcher(
196 3
			$fileFetcher ?? $this->getGeoJsonFileFetcher(),
197 3
			$this->mediaWikiServices->getTitleParser(),
198 3
			$this->mediaWikiServices->getRevisionLookup()
199
		);
200
	}
201
202
	public function smwIntegrationIsEnabled(): bool {
203
		return !$this->settings['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' );
204
	}
205
206
	public function newSemanticMapsSetup( &$mwGlobals ): SemanticMapsSetup {
207
		return SemanticMapsSetup::newFromMediaWikiGlobals( $mwGlobals, $this->getMappingServices() );
208
	}
209
210
}
211