Completed
Push — master ( 8cfcb0...8d42b2 )
by adam
02:02
created

EntityRedirectApiLookup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Wikibase\Api\Lookup;
4
5
use Mediawiki\Api\MediawikiApi;
6
use Mediawiki\Api\SimpleRequest;
7
use Wikibase\DataModel\Entity\BasicEntityIdParser;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookup;
10
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookupException;
11
12
/**
13
 * @access private
14
 */
15
class EntityRedirectApiLookup implements EntityRedirectLookup {
16
17
	/**
18
	 * @var MediawikiApi
19
	 */
20
	private $api;
21
22
	/**
23
	 * @param MediawikiApi $api
24
	 */
25 1
	public function __construct( MediawikiApi $api ) {
26 1
		$this->api = $api;
27 1
	}
28
29
	/**
30
	 * @see EntityRedirectLookup::getRedirectIds
31
	 */
32
	public function getRedirectIds( EntityId $targetId ) {
33
		// TODO: Implement getRedirectIds() method.
34
		// Note: this is hard currently as we have to discover the namespace of the entity type?
35
		throw new \BadMethodCallException('Not implemented yet');
36
	}
37
38
	/**
39
	 * @see EntityRedirectLookup::getRedirectForEntityId
40
	 */
41 1
	public function getRedirectForEntityId( EntityId $entityId, $forUpdate = '' ) {
42 1
		$entityIdSerialization = $entityId->getSerialization();
43
44 1
		$params = array( 'ids' => $entityIdSerialization );
45 1
		$result = $this->api->getRequest( new SimpleRequest( 'wbgetentities', $params ) );
46
47 1
		$entitiesData = $result['entities'];
48 1
		if( !array_key_exists( $entityIdSerialization, $entitiesData ) ) {
49
			throw new EntityRedirectLookupException( $entityId, "Failed to get $entityIdSerialization" );
50
		}
51
52 1
		$entityData = $entitiesData[$entityIdSerialization];
53 1
		if( !array_key_exists( 'redirects', $entityData ) ) {
54
			throw new EntityRedirectLookupException( $entityId, "$entityIdSerialization is not a redirect" );
55
		}
56
57 1
		$entityIdParser = new BasicEntityIdParser();
58 1
		return $entityIdParser->parse( $entityData['redirects']['to'] );
59
	}
60
61
}
62