Completed
Push — master ( e3a9ee...904bdb )
by
unknown
19:57
created

MapView::getJsDataForRow()   C

Complexity

Conditions 11
Paths 5

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 30.6412

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 10
cts 22
cp 0.4545
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 27
nc 5
nop 1
crap 30.6412

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SRF\Filtered\View;
4
5
use DataValues\Geo\Parsers\GeoCoordinateParser;
6
use Exception;
7
use Message;
8
use SMWPropertyValue;
9
use SRF\Filtered\ResultItem;
10
11
class MapView extends View {
12
13
	private static $viewParams = null;
14
15
	private $mapProvider = null;
16
17
18
	/**
19
	 * @param null $mapProvider
20 2
	 */
21 2
	public function setMapProvider( $mapProvider ) {
22 2
		$this->mapProvider = $mapProvider;
23
	}
24
25
	/**
26
	 * @return null
27 2
	 */
28 2
	public function getMapProvider() {
0 ignored issues
show
Coding Style introduced by
getMapProvider 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...
29 2
		if ( $this->mapProvider === null ) {
30
			$this->setMapProvider( isset( $GLOBALS[ 'srfgMapProvider' ] ) ? $GLOBALS[ 'srfgMapProvider' ] : '' );
31
		}
32 2
33
		return $this->mapProvider;
34
	}
35
36
	/**
37
	 * @param ResultItem $row
38
	 * @return array|null
39 1
	 */
40
	public function getJsDataForRow( ResultItem $row ) {
41 1
42
		$markerPositionPropertyName = str_replace( ' ', '_', $this->getActualParameters()[ 'map view marker position property' ] );
43 1
44
		foreach ( $row->getValue() as $field ) {
45 1
46 1
			$printRequest = $field->getPrintRequest();
47
			$field->reset();
48 1
49 1
			$value = $field->getNextDataItem();
50 1
			if ( $printRequest->getData() instanceof SMWPropertyValue &&
51 1
				$printRequest->getData()->getInceptiveProperty()->getKey() === $markerPositionPropertyName &&
52
				( $value instanceof \SMWDIGeoCoord || $value instanceof \SMWDIBlob )
53
			) {
54
				$values = []; // contains plain text
55
56
				if ( $value instanceof \SMWDIGeoCoord ) {
57
58
					while ( $value instanceof \SMWDIGeoCoord ) {
59
						$values[] = [ 'lat' => $value->getLatitude(), 'lng' => $value->getLongitude() ];
60
						$value = $field->getNextDataItem();
61
					}
62
63
				} elseif ( class_exists( 'DataValues\Geo\Parsers\GeoCoordinateParser' ) ) {
64
65
					$coordParser = new GeoCoordinateParser();
66
					while ( $value instanceof \SMWDataItem ) {
67
						try {
68
							$latlng = $coordParser->parse( $value->getSerialization() );
69
							$values[] = [ 'lat' => $latlng->getLatitude(), 'lng' => $latlng->getLongitude() ];
70
							$value = $field->getNextDataItem();
71
						} catch ( Exception $exception ) {
72
							$this->getQueryPrinter()->addError( "Error on '$value': " . $exception->getMessage() );
73
						}
74
					}
75
76
				} else {
77 1
					$this->getQueryPrinter()->addError( Message::newFromKey( 'srf-filtered-map-geocoordinateparser-missing-error' )->inContentLanguage()->text() );
78
				}
79
80
				return [ 'positions' => $values, ];
81 1
			}
82
		}
83
84
		return null;
85
	}
86
87
	/**
88 1
	 * Returns an array of config data for this view to be stored in the JS
89 1
	 * @return array
90
	 */
91
	public function getJsConfig() {
92 1
		$config = parent::getJsConfig();
93
94
		$jsConfigKeys = [
95
			'height',
96
			'zoom',
97
			'minZoom',
98
			'maxZoom',
99
			'marker cluster',
100
			'marker cluster max zoom',
101
			'maxClusterRadius',
102 1
			'zoomToBoundsOnClick',
103 1
		];
104
105
		foreach ( $jsConfigKeys as $key ) {
106 1
			$this->addToConfig( $config, $key );
107
		}
108 1
109
		$config[ 'map provider' ] = $this->getMapProvider();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $config['map provider'] is correct as $this->getMapProvider() (which targets SRF\Filtered\View\MapView::getMapProvider()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
111
		return $config;
112
	}
113
114
	/**
115
	 * A function to describe the allowed parameters of a query for this view.
116 2
	 *
117
	 * @return array of Parameter
118 2
	 */
119
	public static function getParameters() {
120 1
121
		if ( self::$viewParams === null ) {
122 1
123
			$params = parent::getParameters();
124
125
			$params[ 'marker position property' ] = [
126
				// 'type' => 'string',
127
				'name' => 'map view marker position property',
128
				'message' => 'srf-paramdesc-filtered-map-position',
129
				'default' => '',
130 1
				// 'islist' => false,
131
			];
132
133
			$params[ 'height' ] = [
134
				'type' => 'dimension',
135
				'name' => 'map view height',
136
				'message' => 'srf-paramdesc-filtered-map-height',
137
				'default' => 'auto',
138 1
				// 'islist' => false,
139
			];
140
141
			$params[ 'zoom' ] = [
142
				'type' => 'integer',
143
				'name' => 'map view zoom',
144
				'message' => 'srf-paramdesc-filtered-map-zoom',
145
				'default' => '',
146 1
				// 'islist' => false,
147
			];
148
149
			$params[ 'minZoom' ] = [
150
				'type' => 'integer',
151
				'name' => 'map view min zoom',
152
				'message' => 'srf-paramdesc-filtered-map-min-zoom',
153
				'default' => '',
154 1
				// 'islist' => false,
155
			];
156
157
			$params[ 'maxZoom' ] = [
158
				'type' => 'integer',
159
				'name' => 'map view max zoom',
160
				'message' => 'srf-paramdesc-filtered-map-max-zoom',
161
				'default' => '',
162
				// 'islist' => false,
163 1
			];
164
165
			//markercluster
166
			$params[ 'marker cluster' ] = [
167
				'type' => 'boolean',
168
				'name' => 'map view marker cluster',
169
				'message' => 'srf-paramdesc-filtered-map-marker-cluster',
170
				'default' => true,
171 1
				// 'islist' => false,
172
			];
173
174
			$params[ 'marker cluster max zoom' ] = [
175
				'type' => 'integer',
176
				'name' => 'map view marker cluster max zoom',
177
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-zoom',
178
				'default' => '',
179
				// 'islist' => false,
180 1
			];
181
182
			//clustermaxradius - maxClusterRadius: The maximum radius that a cluster will cover from the central marker (in pixels). Default 80.
183
			$params[ 'maxClusterRadius' ] = [
184
				'type' => 'integer',
185
				'name' => 'map view marker cluster radius',
186
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-max-radius',
187
				'default' => '',
188
				// 'islist' => false,
189 1
			];
190
191
			//clusterzoomonclick - zoomToBoundsOnClick: When you click a cluster we zoom to its bounds.
192
			$params[ 'zoomToBoundsOnClick' ] = [
193
				'type' => 'boolean',
194
				'name' => 'map view marker cluster zoom on click',
195
				'message' => 'srf-paramdesc-filtered-map-marker-cluster-zoom-on-click',
196
				'default' => true,
197 1
				// 'islist' => false,
198
			];
199
200 2
			self::$viewParams = $params;
201
		}
202
203
		return self::$viewParams;
204
	}
205
206
	/**
207
	 * Returns the name of the resource module to load.
208 1
	 *
209 1
	 * @return string
210
	 */
211
	public function getResourceModules() {
212
		return 'ext.srf.filtered.map-view';
213
	}
214
215
	/**
216 1
	 * @param array $config
217
	 * @param string $key
218 1
	 */
219
	private function addToConfig( &$config, $key ) {
220 1
221
		$paramDefinition = self::getParameters()[ $key ];
222 1
223
		$param = $this->getActualParameters()[ $paramDefinition[ 'name' ] ];
224
225
		if ( $param !== $paramDefinition[ 'default' ] ) {
226 1
			$config[ $key ] = $param;
227
		}
228
229
	}
230
231 2
	/**
232 2
	 * @return bool
233
	 */
234
	public function getInitError() {
235
		return $this->getMapProvider() === ''? 'srf-filtered-map-provider-missing-error' : null;
236
	}
237
238
}