Completed
Push — master ( d1bd53...48f3de )
by mw
8s
created

doValidateKeyValueForParameterMatch()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 12
cp 0.8333
rs 8.8571
cc 6
eloc 10
nc 6
nop 4
crap 6.1666
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 5
	public function __construct( Parser $parser ) {
28 5
		$this->parser = $parser;
29 5
	}
30
31
	/**
32
	 * @since  1.0
33
	 *
34
	 * @return array $params
35
	 */
36 4
	public function parse( array $params ) {
37
38 4
		$parameters = array();
39 4
		$class = 'whats-nearby';
40
41 4
		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 4
		foreach ( $params as $key => $value ) {
46 4
			$this->doValidateKeyValueForParameterMatch( $key, $value, $class, $parameters );
47 4
		}
48
49 4
		$this->addMapServicesToOutput( $parameters );
50
51 4
		$this->parser->getOutput()->addModules(
52
			'ext.whats.nearby'
53 4
		);
54
55 4
		return Html::rawElement(
56 4
			'div',
57
			array(
58 4
				'class' => $class . ( isset( $parameters['class'] ) ? ' ' . $parameters['class'] : '' ),
59 4
				'data-parameters' => json_encode( $parameters )
60 4
			),
61 4
			Html::rawElement( 'div', array( 'id' => 'controls',  'style' => 'display:none' ) ) .
62 4
			Html::rawElement( 'div', array( 'id' => 'selection', 'style' => 'display:none' ) ) .
63 4
			Html::rawElement( 'div', array( 'id' => 'status' ),
64 4
				Html::rawElement( 'span', array( 'class' => 'geolocation' ) ) .
65 4
				Html::rawElement( 'span', array( 'class' => 'localcache' ) ) .
66 4
				Html::rawElement( 'span', array( 'class' => 'error' ) )
67 4
			) .
68 4
			Html::rawElement( 'div', array( 'id' => 'output', 'style' => 'opacity: 0.5;' ), wfMessage( 'wnby-loading' )->text() )
69 4
		);
70
	}
71
72 4
	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...
73
74 4
		if ( $key == 0 && strpos( $value, '=' ) === false ) {
75
			$parameters['condition'] = $value;
76
		}
77
78
		// Build printrequest identifier for a template
79
		// extending argument
80 4
		if ( $value !== '' && $value{0} === '?' ) {
81 1
			$parameters['pr-' . $key] = $value;
82 1
			return;
83
		}
84
85 4
		if ( strpos( $value, '=' ) === false ) {
86 4
			return;
87
		}
88
89 4
		list( $k, $v ) = explode( '=', $value );
90 4
		$parameters[strtolower( $k )] = $v;
91 4
	}
92
93 4
	private function addMapServicesToOutput( &$parameters ) {
94
95 4
		if ( isset( $parameters['format'] ) &&
96 4
			in_array( $parameters['format'], array( 'openlayers', 'leaflet', 'googlemaps', 'googlemaps3', 'maps', 'google' ) ) ) {
97
			$parameters['maps'] = $parameters['format'];
98
		}
99
100 4
		if ( !isset( $parameters['maps'] ) || !class_exists( 'MapsMappingServices' ) ) {
101 2
			return;
102
		}
103
104 2
		if ( $parameters['maps'] === 'openlayers' ) {
105 1
			$mapsOpenLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
106 1
			$mapsOpenLayers->addDependencies( $this->parser );
107 1
			$this->parser->getOutput()->addJsConfigVars( $mapsOpenLayers->getConfigVariables() );
108 1
		}
109
110
		if (
111 2
			$parameters['maps'] === 'googlemaps' ||
112 1
			$parameters['maps'] === 'googlemaps3' ||
113 1
			$parameters['maps'] === 'maps' ||
114 2
			$parameters['maps'] === 'google' ) {
115 1
			$mapsGoogleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
116 1
			$mapsGoogleMaps->addDependencies( $this->parser );
117 1
		}
118
119 2
		if ( $parameters['maps'] === 'leaflet' || $parameters['maps'] === 'leafletmaps' ) {
120
			$mapsLeaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
121
			$mapsLeaflet->addDependencies( $this->parser );
122
		}
123 2
	}
124
125
}
126