ApiEntityLookup   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 12
c 6
b 0
f 3
lcom 1
cbo 4
dl 0
loc 84
ccs 35
cts 35
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityDocumentForId() 0 4 2
A __construct() 0 5 1
A getEntityDocumentsForIds() 0 7 2
A buildRequest() 0 16 3
A serializeEntityIds() 0 10 2
A parseResponse() 0 9 2
1
<?php
2
3
namespace Wikibase\EntityStore\Api;
4
5
use Deserializers\Deserializer;
6
use Mediawiki\Api\MediawikiApi;
7
use Mediawiki\Api\SimpleRequest;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\EntityStore\EntityDocumentLookup;
10
use Wikibase\EntityStore\EntityStore;
11
use Wikibase\EntityStore\EntityStoreOptions;
12
13
/**
14
 * Internal class
15
 *
16
 * @licence GPLv2+
17
 * @author Thomas Pellissier Tanon
18
 *
19
 * TODO: allow to retrieve more than 50 entities
20
 */
21
class ApiEntityLookup implements EntityDocumentLookup {
22
23
	/**
24
	 * @var MediawikiApi
25
	 */
26
	private $api;
27
28
	/**
29
	 * @var Deserializer
30
	 */
31
	private $deserializer;
32
33
	/**
34
	 * @var EntityStoreOptions
35
	 */
36
	private $options;
37
	/**
38
	 * @param MediawikiApi $api
39
	 * @param Deserializer $deserializer
40
	 * @param EntityStoreOptions $options
41
	 */
42 5
	public function __construct( MediawikiApi $api, Deserializer $deserializer, EntityStoreOptions $options ) {
43 5
		$this->api = $api;
44 5
		$this->deserializer = $deserializer;
45 5
		$this->options = $options;
46 5
	}
47
48
	/**
49
	 * @see EntityDocumentLookup::getEntityDocumentForId
50
	 */
51 3
	public function getEntityDocumentForId( EntityId $entityId ) {
52 3
		$entities = $this->getEntityDocumentsForIds( [ $entityId ] );
53 3
		return reset( $entities ) ?: null;
54
	}
55
56
	/**
57
	 * @see EntityDocumentLookup::getEntityDocumentsForIds
58
	 */
59 5
	public function getEntityDocumentsForIds( array $entityIds ) {
60 5
		if( empty( $entityIds ) ) {
61 1
			return [];
62
		}
63
64 4
		return $this->parseResponse( $this->api->getRequest( $this->buildRequest( $entityIds ) ) );
65
	}
66
67 4
	private function buildRequest( array $entityIds ) {
68
		$params = [
69 4
			'ids' => implode( '|', $this->serializeEntityIds( $entityIds ) )
70 4
		];
71
72 4
		if( $this->options->getOption( EntityStore::OPTION_LANGUAGE_FALLBACK ) ) {
73 1
			$params['languagefallback'] = true;
74 1
		}
75
76 4
		$languagesOption = $this->options->getOption( EntityStore::OPTION_LANGUAGES );
77 4
		if( $languagesOption !== null ) {
78 2
			$params['languages'] = implode( '|', $languagesOption );
79 2
		}
80
81 4
		return new SimpleRequest( 'wbgetentities', $params );
82
	}
83
84 4
	private function serializeEntityIds( array $entityIds ) {
85 4
		$serialization = [];
86
87
		/** @var EntityId $entityId */
88 4
		foreach( $entityIds as $entityId ) {
89 4
			$serialization[] = $entityId->getSerialization();
90 4
		}
91
92 4
		return $serialization;
93
	}
94
95 4
	private function parseResponse( array $response ) {
96 4
		$entities = [];
97
98 4
		foreach( $response['entities'] as $serializedEntity ) {
99 3
			$entities[] = $this->deserializer->deserialize( $serializedEntity );
100 4
		}
101
102 4
		return $entities;
103
	}
104
}
105