EntityRedirectApiLookup::getRedirectIds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
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 = [ '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