NearbyParserFunction::parse()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0422

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 8.4586
cc 7
nc 8
nop 1
ccs 19
cts 21
cp 0.9048
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
	 * @param array $params
35
	 *
36
	 * @return string
37
	 */
38 5
	public function parse( array $params ) {
39
40 5
		$parameters = array();
41 5
		$class = 'whats-nearby';
42
43 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...
44
			array_shift( $params );
45
		}
46
47 5
		foreach ( $params as $key => $value ) {
48 5
			$this->doValidateKeyValueForParameterMatch( $key, $value, $class, $parameters );
49 5
		}
50
51 5
		$this->addMapServices( $parameters );
52 5
		$geoip = true;
53
54 5
		if ( isset( $parameters['nolocation'] ) ||
55 5
			( isset( $parameters['detectlocation'] ) && !$parameters['detectlocation'] ) ) {
56 1
			$geoip = false;
57 1
		}
58
59
		// Is to signal the OutputPageParserOutput hook
60 5
		$this->parser->getOutput()->setExtensionData(
61 5
			'wnby-geoip',
62
			$geoip
63 5
		);
64
65 5
		$this->parser->getOutput()->addModules(
66
			'ext.whats.nearby'
67 5
		);
68
69 5
		return $this->getHtmlFor( $class, $parameters );
70
	}
71
72 5
	private function getHtmlFor( $class, $parameters ) {
73 5
		return Html::rawElement(
74 5
			'div',
75
			array(
76 5
				'class' => $class . ( isset( $parameters['class'] ) ? ' ' . $parameters['class'] : '' ),
77 5
				'data-parameters' => json_encode( $parameters )
78 5
			),
79 5
			Html::rawElement( 'div', array( 'id' => 'controls',  'style' => 'display:none' ) ) .
80 5
			Html::rawElement( 'div', array( 'id' => 'selection', 'style' => 'display:none' ) ) .
81 5
			Html::rawElement( 'div', array( 'id' => 'status' ),
82 5
				Html::rawElement( 'span', array( 'class' => 'geolocation' ) ) .
83 5
				Html::rawElement( 'span', array( 'class' => 'localcache' ) ) .
84 5
				Html::rawElement( 'span', array( 'class' => 'error' ) )
85 5
			) .
86 5
			Html::rawElement( 'div', array( 'id' => 'output', 'style' => 'opacity: 0.5;' ), wfMessage( 'wnby-loading' )->text() )
87 5
		);
88
	}
89
90 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...
91
92 5
		if ( $key == 0 && strpos( $value, '=' ) === false ) {
93
			$parameters['condition'] = $value;
94
		}
95
96
		// Build printrequest identifier for a template
97
		// extending argument
98 5
		if ( $value !== '' && $value{0} === '?' ) {
99 1
			$parameters['pr-' . $key] = $value;
100 1
			return;
101
		}
102
103 5
		if ( strpos( $value, '=' ) === false ) {
104 4
			return;
105
		}
106
107 5
		list( $k, $v ) = explode( '=', $value );
108 5
		$parameters[strtolower( $k )] = $v;
109 5
	}
110
111 5
	private function addMapServices( &$parameters ) {
112
113 5
		if ( isset( $parameters['format'] ) &&
114 5
			in_array( $parameters['format'], array( 'openlayers', 'leaflet', 'googlemaps', 'googlemaps3', 'maps', 'google' ) ) ) {
115
			$parameters['maps'] = $parameters['format'];
116
		}
117
118 5
		if ( !isset( $parameters['maps'] ) || !class_exists( 'MapsMappingServices' ) ) {
119 3
			return;
120
		}
121
122 2
		$parserOutput =  $this->parser->getOutput();
123
124 2
		if ( $parameters['maps'] === 'openlayers' ) {
125 1
			$mapsOpenLayers = MapsMappingServices::getServiceInstance( 'openlayers' );
126 1
			$mapsOpenLayers->addDependencies( $parserOutput );
127 1
			$parserOutput->addJsConfigVars( $mapsOpenLayers->getConfigVariables() );
128 1
		}
129
130
		if (
131 2
			$parameters['maps'] === 'googlemaps' ||
132 1
			$parameters['maps'] === 'googlemaps3' ||
133 1
			$parameters['maps'] === 'maps' ||
134 2
			$parameters['maps'] === 'google' ) {
135 1
			$mapsGoogleMaps = MapsMappingServices::getServiceInstance( 'googlemaps3' );
136 1
			$mapsGoogleMaps->addDependencies( $parserOutput );
137 1
		}
138
139 2
		if ( $parameters['maps'] === 'leaflet' || $parameters['maps'] === 'leafletmaps' ) {
140
			$mapsLeaflet = MapsMappingServices::getServiceInstance( 'leaflet' );
141
			$mapsLeaflet->addDependencies( $parserOutput );
142
		}
143 2
	}
144
145
}
146