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

EntityRedirectApiLookup   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 50%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
c 2
b 0
f 2
lcom 1
cbo 5
dl 0
loc 47
ccs 7
cts 14
cp 0.5
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getRedirectIds() 0 5 1
A getRedirectForEntityId() 0 19 3
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