Completed
Push — master ( a7a34c...1e3be1 )
by Jeroen De
03:21
created

MapsFactory::getMediaWikiCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use FileFetcher\Cache\Factory as CacheFactory;
8
use FileFetcher\FileFetcher;
9
use Jeroen\SimpleGeocoder\Geocoder;
10
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder;
11
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder;
12
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder;
13
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder;
14
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder;
15
use Maps\DataAccess\CachingGeocoder;
16
use Maps\DataAccess\MapsFileFetcher;
17
use Maps\DataAccess\MediaWikiFileUrlFinder;
18
use Maps\DataAccess\PageContentFetcher;
19
use Maps\MediaWiki\ParserHooks\DisplayMapFunction;
20
use Maps\Presentation\CoordinateFormatter;
21
use Maps\Presentation\WikitextParsers\LocationParser;
22
use MediaWiki\MediaWikiServices;
23
use SimpleCache\Cache\Cache;
24
use SimpleCache\Cache\MediaWikiCache;
25
26
/**
27
 * @licence GNU GPL v2+
28
 * @author Jeroen De Dauw < [email protected] >
29
 */
30
class MapsFactory {
31
32
	private $settings;
33
	private $mediaWikiServices;
34
35 49
	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
36 49
		$this->settings = $settings;
37 49
		$this->mediaWikiServices = $mediaWikiServices;
38 49
	}
39
40 49
	public static function newDefault(): self {
41 49
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
42
	}
43
44
	/**
45
	 * Only for legacy code where dependency injection is not possible
46
	 */
47 95
	public static function globalInstance(): self {
48 95
		static $instance = null;
49
50 95
		if ( $instance === null ) {
51
			$instance = self::newDefault();
52
		}
53
54 95
		return $instance;
55
	}
56
57 20
	public function newLocationParser(): LocationParser {
58 20
		return LocationParser::newInstance(
59 20
			$this->getGeocoder(),
60 20
			$this->getFileUrlFinder()
61
		);
62
	}
63
64 101
	public function getGeocoder(): Geocoder {
65 101
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );
66
67 101
		if ( $this->settings['egMapsEnableGeoCache'] ) {
68 101
			return new CachingGeocoder(
69 101
				$geocoder,
70 101
				$this->getMediaWikiCache(),
71 101
				$this->settings['egMapsGeoCacheTtl']
72
			);
73
		}
74
75
		return $geocoder;
76
	}
77
78 101
	private function newCoreGeocoder(): Geocoder {
79 101
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
80 101
			case 'geonames':
81
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
82
					return $this->newGoogleGeocoder();
83
				}
84
85
				return new GeoNamesGeocoder(
86
					$this->newFileFetcher(),
87
					$this->settings['egMapsGeoNamesUser']
88
				);
89 101
			case 'google':
90
				return $this->newGoogleGeocoder();
91 101
			case 'nominatim':
92 101
				return new NominatimGeocoder(
93 101
					$this->newFileFetcher()
94
				);
95
			default:
96
				return new NullGeocoder();
97
		}
98
	}
99
100
	private function newGoogleGeocoder(): Geocoder {
101
		return new GoogleGeocoder(
102
			$this->newFileFetcher(),
103
			$this->settings['egMapsGMaps3ApiKey'],
104
			$this->settings['egMapsGMaps3ApiVersion']
105
		);
106
	}
107
108
	public function getFileFetcher(): FileFetcher {
109
		return $this->newFileFetcher();
110
	}
111
112 101
	private function newFileFetcher(): FileFetcher {
113 101
		return new MapsFileFetcher();
114
	}
115
116
	public function getGeoJsonFileFetcher(): FileFetcher {
117
		if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) {
118
			return $this->getFileFetcher();
119
		}
120
121
		return ( new CacheFactory() )->newJeroenSimpleCacheFetcher(
122
			$this->getFileFetcher(),
123
			$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] )
124
		);
125
	}
126
127
	private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache {
128
		return new MediaWikiCache(
129
			$this->getMediaWikiCache(),
130
			$ttlInSeconds
131
		);
132
	}
133
134 101
	private function getMediaWikiCache(): \BagOStuff {
135 101
		return wfGetCache( CACHE_ANYTHING );
136
	}
137
138
	public function getPageContentFetcher(): PageContentFetcher {
139
		return new PageContentFetcher(
140
			$this->mediaWikiServices->getTitleParser(),
141
			$this->mediaWikiServices->getRevisionLookup()
142
		);
143
	}
144
145 17
	public function getCoordinateFormatter(): CoordinateFormatter {
146 17
		return new CoordinateFormatter();
147
	}
148
149 79
	public function getFileUrlFinder(): FileUrlFinder {
150 79
		return new MediaWikiFileUrlFinder();
151
	}
152
153 20
	public function getMappingServices(): MappingServices {
154 20
		return new MappingServices(
155 20
			$this->settings['egMapsAvailableServices'],
156 20
			$this->settings['egMapsDefaultService'],
157 20
			new GoogleMapsService(),
158 20
			new LeafletService()
159
		);
160
	}
161
162 20
	public function getDisplayMapFunction(): DisplayMapFunction {
163 20
		return new DisplayMapFunction(
164 20
			$this->getMappingServices()
165
		);
166
	}
167
168
}
169