EntityRedirectApiLookup   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 47
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0

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 1
	}
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 1
		$entityIdSerialization = $entityId->getSerialization();
45
46 1
		$params = array( 'ids' => $entityIdSerialization );
47 1
		$result = $this->api->getRequest( new SimpleRequest( 'wbgetentities', $params ) );
48
49 1
		$entitiesData = $result['entities'];
50 1
		if( !array_key_exists( $entityIdSerialization, $entitiesData ) ) {
51
			throw new EntityRedirectLookupException( $entityId, "Failed to get $entityIdSerialization" );
52
		}
53
54 1
		$entityData = $entitiesData[$entityIdSerialization];
55 1
		if( !array_key_exists( 'redirects', $entityData ) ) {
56
			throw new EntityRedirectLookupException( $entityId, "$entityIdSerialization is not a redirect" );
57
		}
58
59 1
		$entityIdParser = new BasicEntityIdParser();
60 1
		return $entityIdParser->parse( $entityData['redirects']['to'] );
61
	}
62
63
}
64