Completed
Push — apigeocode ( abd04f...f03722 )
by Jeroen De
05:12
created

ApiGeocode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 68
ccs 14
cts 28
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedParams() 0 9 1
A getParamDescription() 0 5 1
A getDescription() 0 5 1
A getExamples() 0 6 1
B execute() 0 35 6
1
<?php
2
3
namespace Maps\MediaWiki\Api;
4
5
use ApiBase;
6
use Maps\MapsFactory;
7
8
/**
9
 * @licence GNU GPL v2++
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class ApiGeocode extends ApiBase {
13
14 2
	public function execute() {
15 2
		if ( !$this->getUser()->isAllowed( 'geocode' ) || $this->getUser()->isBlocked() ) {
16 1
			$this->dieWithError( 'badaccess-groups' );
17
		}
18
19 1
		$geocoder = MapsFactory::newDefault()->getGeocoder();
20
21 1
		$params = $this->extractRequestParams();
22
23 1
		$results = [];
24
25 1
		foreach ( array_unique( $params['locations'] ) as $location ) {
26
			$result = $geocoder->geocode( $location );
27
28
			$results[$location] = [
29
				'count' => $result === null ? 0 : 1,
30
				'locations' => []
31
			];
32
33
			if ( $result !== null ) {
34
				$results[$location]['locations'][] = [
35
					'latitude' => $result->getLatitude(),
36
					'longitude' =>  $result->getLongitude()
37
				];
38
			}
39
40
			$this->getResult()->setIndexedTagName( $results[$location]['locations'], 'location' );
41
		}
42
43 1
		$this->getResult()->addValue(
44 1
			null,
45 1
			'results',
46 1
			$results
47
		);
48 1
	}
49
50 2
	public function getAllowedParams() {
51
		return [
52 2
			'locations' => [
53
				ApiBase::PARAM_TYPE => 'string',
54
				ApiBase::PARAM_REQUIRED => true,
55
				ApiBase::PARAM_ISMULTI => true,
56
			],
57
		];
58
	}
59
60
	public function getParamDescription() {
61
		return [
62
			'locations' => 'The locations to geocode',
63
		];
64
	}
65
66
	public function getDescription() {
67
		return [
68
			'API module for geocoding.'
69
		];
70
	}
71
72
	protected function getExamples() {
73
		return [
74
			'api.php?action=geocode&locations=new york',
75
			'api.php?action=geocode&locations=new york|brussels|london',
76
		];
77
	}
78
79
}
80