GetEntitiesClient   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 50
ccs 14
cts 16
cp 0.875
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchEntityPages() 0 8 3
A constructRequestUrl() 0 5 1
A makeRequest() 0 3 1
A getEntityPagesFromResponse() 0 3 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
}