WikidataQueryApi::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 2
b 0
f 1
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 7
cp 0.7143
crap 2.0932
rs 9.4285
1
<?php
2
3
namespace WikidataQueryApi;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\ClientInterface;
7
8
/**
9
 * @licence GPLv2+
10
 * @author Thomas Pellissier Tanon
11
 */
12
class WikidataQueryApi {
13
14
	/**
15
	 * @var string
16
	 */
17
	private $apiUrl;
18
19
	/**
20
	 * @var ClientInterface
21
	 */
22
	private $client;
23
24
	/**
25
	 * @param string $apiUrl the URL of the Wikidata Query API
26
	 * @param ClientInterface|null $client the Guzzle client to use
27
	 */
28 2
	public function __construct( $apiUrl, ClientInterface $client = null ) {
29 2
		if( $client === null ) {
30
			$client = new Client();
31
		}
32
33 2
		$this->apiUrl = $apiUrl;
34 2
		$this->client = $client;
35 2
	}
36
37
	/**
38
	 * @param array $params
39
	 * @return array
40
	 *
41
	 * @throws WikibaseQueryApiException
42
	 */
43 2
	public function doQuery( $params ) {
44 2
		$result = json_decode( $this->client->get(
45 2
			$this->apiUrl,
46 2
			[ 'query' => $params ]
47 2
		)->getBody(), true );
48
49 2
		if ( $result['status']['error'] !== 'OK' ) {
50 1
			throw new WikibaseQueryApiException( $result['status']['error'] );
51
		}
52
53 1
		return $result;
54
	}
55
}
56