SimpleQueryService::doQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace WikidataQueryApi\Services;
4
5
use Serializers\Serializer;
6
use Wikibase\DataModel\Entity\ItemId;
7
use WikidataQueryApi\Query\AbstractQuery;
8
use WikidataQueryApi\WikibaseQueryApiException;
9
use WikidataQueryApi\WikidataQueryApi;
10
11
/**
12
 * Simple service that returns a list of ItemId for a given query.
13
 *
14
 * @licence GPLv2+
15
 * @author Thomas Pellissier Tanon
16
 */
17
class SimpleQueryService {
18
19
	/**
20
	 * @var WikidataQueryApi
21
	 */
22
	private $api;
23
24
	/**
25
	 * @var Serializer
26
	 */
27
	private $querySerializer;
28
29
	/**
30
	 * @param WikidataQueryApi $api
31
	 * @param Serializer $querySerializer
32
	 */
33 1
	public function __construct( WikidataQueryApi $api, Serializer $querySerializer ) {
34 1
		$this->api = $api;
35 1
		$this->querySerializer = $querySerializer;
36 1
	}
37
38
	/**
39
	 * @param AbstractQuery $query
40
	 * @return ItemId[]
41
	 *
42
	 * @throws WikibaseQueryApiException
43
	 */
44 1
	public function doQuery( AbstractQuery $query ) {
45 1
		$result = $this->api->doQuery( [
46 1
			'q' => $this->querySerializer->serialize( $query )
47 1
		] );
48
49 1
		return $this->parseItemList( $result['items'] );
50
	}
51
52 1
	private function parseItemList( array $itemNumericIds ) {
53 1
		$itemIds = [];
54
55 1
		foreach ( $itemNumericIds as $itemNumericId ) {
56 1
			$itemIds[] = ItemId::newFromNumber( $itemNumericId );
57 1
		}
58
59 1
		return $itemIds;
60
	}
61
}
62