Completed
Pull Request — master (#22)
by Jeroen De
19:03
created

NearbyParserFunction   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 0
loc 123
ccs 66
cts 72
cp 0.9167
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B parse() 0 33 7
A getHtmlFor() 0 17 2
B doValidateKeyValueForParameterMatch() 0 20 6
B addMapServices() 0 20 6
A addMappingServiceDependencies() 0 4 1
1
<?php
2
3
namespace WNBY;
4
5
use Maps\MappingService;
6
use Maps\MappingServices;
7
use Maps\MapsFactory;
8
use Parser;
9
use Html;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class NearbyParserFunction {
18
19
	/**
20
	 * @var Parser
21
	 */
22
	private $parser;
23
24
	/**
25
	 * @since  1.0
26
	 *
27 6
	 * @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...
28 6
	 */
29 6
	public function __construct( Parser $parser ) {
30
		$this->parser = $parser;
31
	}
32
33
	/**
34
	 * @since  1.0
35
	 *
36
	 * @param array $params
37
	 *
38 5
	 * @return string
39
	 */
40 5
	public function parse( array $params ) {
41 5
42
		$parameters = array();
43 5
		$class = 'whats-nearby';
44
45
		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...
46
			array_shift( $params );
47 5
		}
48 5
49 5
		foreach ( $params as $key => $value ) {
50
			$this->doValidateKeyValueForParameterMatch( $key, $value, $class, $parameters );
51 5
		}
52 5
53
		$this->addMapServices( $parameters );
54 5
		$geoip = true;
55 5
56 1
		if ( isset( $parameters['nolocation'] ) ||
57 1
			( isset( $parameters['detectlocation'] ) && !$parameters['detectlocation'] ) ) {
58
			$geoip = false;
59
		}
60 5
61 5
		// Is to signal the OutputPageParserOutput hook
62
		$this->parser->getOutput()->setExtensionData(
63 5
			'wnby-geoip',
64
			$geoip
65 5
		);
66
67 5
		$this->parser->getOutput()->addModules(
68
			'ext.whats.nearby'
69 5
		);
70
71
		return $this->getHtmlFor( $class, $parameters );
72 5
	}
73 5
74 5
	private function getHtmlFor( $class, $parameters ) {
75
		return Html::rawElement(
76 5
			'div',
77 5
			array(
78 5
				'class' => $class . ( isset( $parameters['class'] ) ? ' ' . $parameters['class'] : '' ),
79 5
				'data-parameters' => json_encode( $parameters )
80 5
			),
81 5
			Html::rawElement( 'div', array( 'id' => 'controls',  'style' => 'display:none' ) ) .
82 5
			Html::rawElement( 'div', array( 'id' => 'selection', 'style' => 'display:none' ) ) .
83 5
			Html::rawElement( 'div', array( 'id' => 'status' ),
84 5
				Html::rawElement( 'span', array( 'class' => 'geolocation' ) ) .
85 5
				Html::rawElement( 'span', array( 'class' => 'localcache' ) ) .
86 5
				Html::rawElement( 'span', array( 'class' => 'error' ) )
87 5
			) .
88
			Html::rawElement( 'div', array( 'id' => 'output', 'style' => 'opacity: 0.5;' ), wfMessage( 'wnby-loading' )->text() )
89
		);
90 5
	}
91
92 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...
93
94
		if ( $key == 0 && strpos( $value, '=' ) === false ) {
95
			$parameters['condition'] = $value;
96
		}
97
98 5
		// Build printrequest identifier for a template
99 1
		// extending argument
100 1
		if ( $value !== '' && $value{0} === '?' ) {
101
			$parameters['pr-' . $key] = $value;
102
			return;
103 5
		}
104 4
105
		if ( strpos( $value, '=' ) === false ) {
106
			return;
107 5
		}
108 5
109 5
		list( $k, $v ) = explode( '=', $value );
110
		$parameters[strtolower( $k )] = $v;
111 5
	}
112
113 5
	private function addMapServices( &$parameters ) {
114 5
115
		if ( isset( $parameters['format'] ) &&
116
			in_array( $parameters['format'], array( 'openlayers', 'leaflet', 'googlemaps', 'googlemaps3', 'maps', 'google' ) ) ) {
117
			$parameters['maps'] = $parameters['format'];
118 5
		}
119 3
120
		if ( !isset( $parameters['maps'] ) || !class_exists( MappingServices::class ) ) {
121
			return;
122 2
		}
123
124 2
		$services = MapsFactory::globalInstance()->getMappingServices();
125 1
126 1
		if ( $services->nameIsKnown( $parameters['maps'] ) ) {
127 1
			$this->addMappingServiceDependencies(
128 1
				$services->getService( $parameters['maps'] ),
129
				$parameters
130
			);
131 2
		}
132 1
	}
133 1
134 2
	private function addMappingServiceDependencies( MappingService $service, array $parameters ) {
135 1
		$this->parser->getOutput()->addHeadItem( $service->getDependencyHtml( $parameters ) );
136 1
		$this->parser->getOutput()->addModules( $service->getResourceModules() );
137 1
	}
138
139
}
140