Completed
Push — master ( 59ef79...0173b4 )
by mw
10s
created

NearbyParserFunction::parse()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0422

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 33
ccs 19
cts 21
cp 0.9048
rs 6.7272
cc 7
eloc 18
nc 8
nop 1
crap 7.0422
1
<?php
2
3
namespace WNBY;
4
5
use Parser;
6
use MapsMappingServices;
7
use Html;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author mwjames
14
 */
15
class NearbyParserFunction {
16
17
	/**
18
	 * @var Parser
19
	 */
20
	private $parser;
21
22
	/**
23
	 * @since  1.0
24
	 *
25
	 * @return Parser $parser
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
	 */
27 6
	public function __construct( Parser $parser ) {
28 6
		$this->parser = $parser;
29 6
	}
30
31
	/**
32
	 * @since  1.0
33
	 *
34
	 * @return array $params
35
	 */
36 5
	public function parse( array $params ) {
37
38 5
		$parameters = array();
39 5
		$class = 'whats-nearby';
40
41 5
		if( isset( $params[0] ) && $params[0] instanceof \Parser ) {
0 ignored issues
show
Bug introduced by
The class Parser does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
42
			array_shift( $params );
43
		}
44
45 5
		foreach ( $params as $key => $value ) {
46 5
			$this->doValidateKeyValueForParameterMatch( $key, $value, $class, $parameters );
47 5
		}
48
49 5
		$this->addMapServicesToOutput( $parameters );
50 5
		$geoip = true;
51
52 5
		if ( isset( $parameters['nolocation'] ) ||
53 5
			( isset( $parameters['detectlocation'] ) && !$parameters['detectlocation'] ) ) {
54 1
			$geoip = false;
55 1
		}
56
57
		// Is to signal the OutputPageParserOutput hook
58 5
		$this->parser->getOutput()->setExtensionData(
59 5
			'wnby-geoip',
60
			$geoip
61 5
		);
62
63 5
		$this->parser->getOutput()->addModules(
64
			'ext.whats.nearby'
65 5
		);
66
67 5
		return $this->getHtmlFor( $class, $parameters );
68
	}
69
70 5
	private function getHtmlFor( $class, $parameters ) {
71 5
		return Html::rawElement(
72 5
			'div',
73
			array(
74 5
				'class' => $class . ( isset( $parameters['class'] ) ? ' ' . $parameters['class'] : '' ),
75 5
				'data-parameters' => json_encode( $parameters )
76 5
			),
77 5
			Html::rawElement( 'div', array( 'id' => 'controls',  'style' => 'display:none' ) ) .
78 5
			Html::rawElement( 'div', array( 'id' => 'selection', 'style' => 'display:none' ) ) .
79 5
			Html::rawElement( 'div', array( 'id' => 'status' ),
80 5
				Html::rawElement( 'span', array( 'class' => 'geolocation' ) ) .
81 5
				Html::rawElement( 'span', array( 'class' => 'localcache' ) ) .
82 5
				Html::rawElement( 'span', array( 'class' => 'error' ) )
83 5
			) .
84 5
			Html::rawElement( 'div', array( 'id' => 'output', 'style' => 'opacity: 0.5;' ), wfMessage( 'wnby-loading' )->text() )
85 5
		);
86
	}
87
88 5
	private function doValidateKeyValueForParameterMatch( $key, $value, &$class, &$parameters ) {
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
90 5
		if ( $key == 0 && strpos( $value, '=' ) === false ) {
91
			$parameters['condition'] = $value;
92
		}
93
94
		// Build printrequest identifier for a template
95
		// extending argument
96 5
		if ( $value !== '' && $value{0} === '?' ) {
97 1
			$parameters['pr-' . $key] = $value;
98 1
			return;
99
		}
100
101 5
		if ( strpos( $value, '=' ) === false ) {
102 4
			return;
103
		}
104
105 5
		list( $k, $v ) = explode( '=', $value );
106 5
		$parameters[strtolower( $k )] = $v;
107 5
	}
108
109 5
	private function addMapServicesToOutput( &$parameters ) {
110
111 5
		if ( isset( $parameters['format'] ) &&
112 5
			in_array( $parameters['format'], array( 'openlayers', 'leaflet', 'googlemaps', 'googlemaps3', 'maps', 'google' ) ) ) {
113
			$parameters['maps'] = $parameters['format'];
114
		}
115
116 5
		if ( !isset( $parameters['maps'] ) || !class_exists( 'MapsMappingServices' ) ) {
117 3
			return;
118
		}
119
120 2
		if ( $parameters['maps'] === 'openlayers' ) {
121 1
			$mapsOpenLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
122 1
			$mapsOpenLayers->addDependencies( $this->parser );
123 1
			$this->parser->getOutput()->addJsConfigVars( $mapsOpenLayers->getConfigVariables() );
124 1
		}
125
126
		if (
127 2
			$parameters['maps'] === 'googlemaps' ||
128 1
			$parameters['maps'] === 'googlemaps3' ||
129 1
			$parameters['maps'] === 'maps' ||
130 2
			$parameters['maps'] === 'google' ) {
131 1
			$mapsGoogleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
132 1
			$mapsGoogleMaps->addDependencies( $this->parser );
133 1
		}
134
135 2
		if ( $parameters['maps'] === 'leaflet' || $parameters['maps'] === 'leafletmaps' ) {
136
			$mapsLeaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
137
			$mapsLeaflet->addDependencies( $this->parser );
138
		}
139 2
	}
140
141
}
142