Completed
Push — master ( 4d0076...81538e )
by Jeroen De
26s queued 11s
created

MapsFactory   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 25

Test Coverage

Coverage 71.54%

Importance

Changes 0
Metric Value
wmc 39
lcom 2
cbo 25
dl 0
loc 231
ccs 88
cts 123
cp 0.7154
rs 9.28
c 0
b 0
f 0

29 Methods

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