Completed
Push — LocationParsers ( afa02e )
by Jeroen De
04:46
created

MapsFactory::newFileFetcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Maps;
4
5
use FileFetcher\SimpleFileFetcher;
6
use Maps\Geocoders\CachingGeocoder;
7
use Maps\Geocoders\Geocoder;
8
use Maps\Geocoders\GoogleGeocoder;
9
use Maps\Geocoders\NominatimGeocoder;
10
use Maps\Geocoders\NullGeocoder;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class MapsFactory {
17
18
	private $settings;
19
20
	private function __construct( array $settings ) {
21
		$this->settings = $settings;
22
	}
23
24
	public static function newDefault() {
0 ignored issues
show
Coding Style introduced by
newDefault uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
		return new self( $GLOBALS );
26
	}
27
28
	/**
29
	 * @return LocationParser
30
	 */
31
	public function newLocationParser() {
32
		return LocationParser::newInstance( $this->newGeocoder() );
33
	}
34
35
	/**
36
	 * @return Geocoder
37
	 */
38
	public function newGeocoder() {
39
		$geocoder = $this->newCoreGeocoder();
40
41
		if ( $this->settings['egMapsEnableGeoCache'] ) {
42
			return new CachingGeocoder(
43
				$geocoder,
44
				$this->getMediaWikiCache()
45
			);
46
		}
47
48
		return $geocoder;
49
	}
50
51
	/**
52
	 * @return Geocoder
53
	 */
54
	private function newCoreGeocoder() {
55
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
56
			case 'geonames':
57
				return new Geocoders\GeoNamesGeocoder(
58
					$this->newFileFetcher(),
59
					$this->settings['egMapsGeoNamesUser']
60
				);
61
			case 'google':
62
				return new GoogleGeocoder(
63
					$this->newFileFetcher(),
64
					$this->settings['egMapsGMaps3ApiKey'],
65
					$this->settings['egMapsGMaps3ApiVersion']
66
				);
67
			case 'nominatim':
68
				return new NominatimGeocoder(
69
					$this->newFileFetcher()
70
				);
71
			default:
72
				return new NullGeocoder();
73
		}
74
	}
75
76
	private function newFileFetcher() {
77
		return new SimpleFileFetcher();
78
	}
79
80
	/**
81
	 * @return \BagOStuff
82
	 */
83
	private function getMediaWikiCache() {
84
		return wfGetCache( CACHE_ANYTHING );
85
	}
86
87
}
88