Completed
Push — master ( 09cbbd...be79d2 )
by Jeroen De
03:39
created

MapsFactory   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 22

Test Coverage

Coverage 78.18%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 22
dl 0
loc 203
ccs 86
cts 110
cp 0.7818
rs 9.6
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A globalInstance() 0 9 2
A __construct() 0 4 1
A newDefault() 0 3 1
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 getMediaWikiSimpleCache() 0 6 1
A getMediaWikiCache() 0 3 1
A getPageContentFetcher() 0 6 1
A getCoordinateFormatter() 0 3 1
A getFileUrlFinder() 0 3 1
A getMappingServices() 0 8 1
A getGoogleMapsService() 0 7 2
A getLeafletService() 0 7 2
A getDisplayMapFunction() 0 5 1
A getParamDefinitionFactory() 0 15 1
A newGeoJsonFetcher() 0 7 1
A smwIntegrationIsEnabled() 0 3 2
A newSemanticMapsSetup() 0 3 1
A newSemanticGeoJsonStore() 0 8 1
A getSmwFactory() 0 3 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\GeoJsonFetcher;
18
use Maps\DataAccess\GeoJsonStore\GeoJsonStore;
19
use Maps\DataAccess\GeoJsonStore\SemanticGeoJsonStore;
20
use Maps\DataAccess\GeoJsonStore\SubObjectBuilder;
21
use Maps\DataAccess\MapsFileFetcher;
22
use Maps\DataAccess\MediaWikiFileUrlFinder;
23
use Maps\DataAccess\PageContentFetcher;
24
use Maps\MediaWiki\ParserHooks\DisplayMapFunction;
25
use Maps\Presentation\CoordinateFormatter;
26
use Maps\Presentation\WikitextParsers\CircleParser;
27
use Maps\Presentation\WikitextParsers\DistanceParser;
28
use Maps\Presentation\WikitextParsers\ImageOverlayParser;
29
use Maps\Presentation\WikitextParsers\LineParser;
30
use Maps\Presentation\WikitextParsers\LocationParser;
31
use Maps\Presentation\WikitextParsers\PolygonParser;
32
use Maps\Presentation\WikitextParsers\RectangleParser;
33
use Maps\Presentation\WikitextParsers\WmsOverlayParser;
34
use MediaWiki\MediaWikiServices;
35
use ParamProcessor\ParamDefinitionFactory;
36
use SimpleCache\Cache\Cache;
37
use SimpleCache\Cache\MediaWikiCache;
38
use SMW\ApplicationFactory;
39
use Title;
40
41
/**
42
 * @licence GNU GPL v2+
43
 * @author Jeroen De Dauw < [email protected] >
44
 */
45
class MapsFactory {
46
47
	private $settings;
48
	private $mediaWikiServices;
49
50
	private $leafletService;
51
	private $googleService;
52
53 2
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
54 2
		$this->settings = $settings;
55 2
		$this->mediaWikiServices = $mediaWikiServices;
56 2
	}
57
58
	/**
59
	 * Only for legacy code where dependency injection is not possible
60
	 */
61 126
	public static function globalInstance(): self {
62 126
		static $instance = null;
63
64 126
		if ( $instance === null ) {
65
			$instance = self::newDefault();
66
		}
67
68 126
		return $instance;
69
	}
70
71 2
	public static function newDefault(): self {
72 2
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
73
	}
74
75 26
	public function newLocationParser(): LocationParser {
76 26
		return LocationParser::newInstance(
77 26
			$this->getGeocoder(),
78 26
			$this->getFileUrlFinder()
79
		);
80
	}
81
82 107
	public function getGeocoder(): Geocoder {
83 107
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
84
85 107
		if ( $this->settings['egMapsEnableGeoCache'] ) {
86 107
			return new CachingGeocoder(
87 107
				$geocoder,
88 107
				$this->getMediaWikiCache(),
89 107
				$this->settings['egMapsGeoCacheTtl']
90
			);
91
		}
92
93
		return $geocoder;
94
	}
95
96 107
	private function newCoreGeocoder(): Geocoder {
97 107
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
98 107
			case 'geonames':
99
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
100
					return $this->newGoogleGeocoder();
101
				}
102
103
				return new GeoNamesGeocoder(
104
					$this->newFileFetcher(),
105
					$this->settings['egMapsGeoNamesUser']
106
				);
107 107
			case 'google':
108
				return $this->newGoogleGeocoder();
109 107
			case 'nominatim':
110 107
				return new NominatimGeocoder(
111 107
					$this->newFileFetcher()
112
				);
113
			default:
114
				return new NullGeocoder();
115
		}
116
	}
117
118
	private function newGoogleGeocoder(): Geocoder {
119
		return new GoogleGeocoder(
120
			$this->newFileFetcher(),
121
			$this->settings['egMapsGMaps3ApiKey'],
122
			$this->settings['egMapsGMaps3ApiVersion']
123
		);
124
	}
125
126 3
	public function getFileFetcher(): FileFetcher {
127 3
		return $this->newFileFetcher();
128
	}
129
130 107
	private function newFileFetcher(): FileFetcher {
131 107
		return new MapsFileFetcher();
132
	}
133
134 3
	public function getGeoJsonFileFetcher(): FileFetcher {
135 3
		if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) {
136 3
			return $this->getFileFetcher();
137
		}
138
139
		return ( new CacheFactory() )->newJeroenSimpleCacheFetcher(
140
			$this->getFileFetcher(),
141
			$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] )
142
		);
143
	}
144
145
	private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache {
146
		return new MediaWikiCache(
147
			$this->getMediaWikiCache(),
148
			$ttlInSeconds
149
		);
150
	}
151
152 107
	private function getMediaWikiCache(): \BagOStuff {
153 107
		return wfGetCache( CACHE_ANYTHING );
154
	}
155
156 2
	public function getPageContentFetcher(): PageContentFetcher {
157 2
		return new PageContentFetcher(
158 2
			$this->mediaWikiServices->getTitleParser(),
159 2
			$this->mediaWikiServices->getRevisionLookup()
160
		);
161
	}
162
163 20
	public function getCoordinateFormatter(): CoordinateFormatter {
164 20
		return new CoordinateFormatter();
165
	}
166
167 85
	public function getFileUrlFinder(): FileUrlFinder {
168 85
		return new MediaWikiFileUrlFinder();
169
	}
170
171 24
	public function getMappingServices(): MappingServices {
172 24
		return new MappingServices(
173 24
			$this->settings['egMapsAvailableServices'],
174 24
			$this->settings['egMapsDefaultService'],
175 24
			$this->getGoogleMapsService(),
176 24
			$this->getLeafletService()
177
		);
178
	}
179
180 24
	private function getGoogleMapsService(): GoogleMapsService {
181 24
		if ( $this->googleService === null ) {
182
			$this->googleService = new GoogleMapsService();
183
		}
184
185 24
		return $this->googleService;
186
	}
187
188 24
	private function getLeafletService(): LeafletService {
189 24
		if ( $this->leafletService === null ) {
190
			$this->leafletService = new LeafletService();
191
		}
192
193 24
		return $this->leafletService;
194
	}
195
196 24
	public function getDisplayMapFunction(): DisplayMapFunction {
197 24
		return new DisplayMapFunction(
198 24
			$this->getMappingServices()
199
		);
200
	}
201
202 90
	public function getParamDefinitionFactory(): ParamDefinitionFactory {
203 90
		$factory = ParamDefinitionFactory::newDefault();
204
205 90
		$factory->registerType( 'coordinate', [ 'string-parser' => LatLongParser::class ] );
206 90
		$factory->registerType( 'mapslocation', [ 'string-parser' => LocationParser::class ] );
207 90
		$factory->registerType( 'mapsline', [ 'string-parser' => LineParser::class ] );
208 90
		$factory->registerType( 'mapscircle', [ 'string-parser' => CircleParser::class ] );
209 90
		$factory->registerType( 'mapsrectangle', [ 'string-parser' => RectangleParser::class ] );
210 90
		$factory->registerType( 'mapspolygon', [ 'string-parser' => PolygonParser::class ] );
211 90
		$factory->registerType( 'distance', [ 'string-parser' => DistanceParser::class ] );
212 90
		$factory->registerType( 'wmsoverlay', [ 'string-parser' => WmsOverlayParser::class ] );
213 90
		$factory->registerType( 'mapsimageoverlay', [ 'string-parser' => ImageOverlayParser::class ] );
214
215 90
		return $factory;
216
	}
217
218 3
	public function newGeoJsonFetcher( FileFetcher $fileFetcher = null ): GeoJsonFetcher {
219 3
		return new GeoJsonFetcher(
220 3
			$fileFetcher ?? $this->getGeoJsonFileFetcher(),
221 3
			$this->mediaWikiServices->getTitleParser(),
222 3
			$this->mediaWikiServices->getRevisionLookup()
223
		);
224
	}
225
226 3
	public function smwIntegrationIsEnabled(): bool {
227 3
		return !$this->settings['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' );
228
	}
229
230
	public function newSemanticMapsSetup( &$mwGlobals ): SemanticMapsSetup {
231
		return SemanticMapsSetup::newFromMediaWikiGlobals( $mwGlobals, $this->getMappingServices() );
232
	}
233
234 3
	public function newSemanticGeoJsonStore( \ParserOutput $parserOutput, Title $subjectPage ): GeoJsonStore {
235 3
		return new SemanticGeoJsonStore(
236 3
			$this->getSmwFactory()->newParserData( $subjectPage, $parserOutput ),
237
			$subjectPage,
238 3
			$this->getSmwFactory()->getEventDispatcher(),
239 3
			new SubObjectBuilder( $subjectPage )
240
		);
241
	}
242
243 3
	private function getSmwFactory(): ApplicationFactory {
244 3
		return ApplicationFactory::getInstance();
245
	}
246
247
}
248