GetEntitiesClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Queryr\Replicator\EntitySource\Api;
4
5
use Queryr\Replicator\EntitySource\EntityPageBatchFetcher;
6
use Queryr\Replicator\Model\EntityPage;
7
8
/**
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class GetEntitiesClient implements EntityPageBatchFetcher {
13
14
	private $http;
15
16
	/**
17
	 * @var GetEntitiesInterpreter
18
	 */
19
	private $responseInterpreter;
20
21 4
	public function __construct( Http $http ) {
22 4
		$this->http = $http;
23 4
		$this->responseInterpreter = new GetEntitiesInterpreter();
24 4
	}
25
26
	/**
27
	 * @param string[] $entityIds
28
	 * @return EntityPage[]
29
	 */
30 4
	public function fetchEntityPages( array $entityIds ): array {
31 4
		if ( empty( $entityIds ) ) {
32 2
			return [];
33
		}
34
35 2
		$response = $this->makeRequest( $this->constructRequestUrl( $entityIds ) );
36 2
		return is_string( $response ) ? $this->getEntityPagesFromResponse( $response ) : [];
37
	}
38
39
	/**
40
	 * @param string[] $entityIds
41
	 * @return string
42
	 */
43 2
	private function constructRequestUrl( array $entityIds ) {
44 2
		$ids = implode( '|', array_map( 'urlencode', $entityIds ) );
45
46 2
		return 'https://www.wikidata.org/w/api.php?action=wbgetentities&ids=' . $ids . '&format=json';
47
	}
48
49 2
	private function makeRequest( $url ) {
50 2
		return $this->http->get( $url );
51
	}
52
53
	/**
54
	 * @param string $response
55
	 * @return EntityPage[]
56
	 */
57
	private function getEntityPagesFromResponse( string $response ) {
58
		return $this->responseInterpreter->getEntityPagesFromResult( $response );
59
	}
60
61
}