Completed
Push — master ( 31d688...478159 )
by adam
04:05 queued 01:35
created

EntityRedirectApiLookup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

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