Completed
Push — main ( b55b92...e8d442 )
by
unknown
05:41
created

EntityRedirectApiLookup   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 47
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Addwiki\Wikibase\Api\Lookup;
4
5
use Addwiki\Mediawiki\Api\Client\MediawikiApi;
6
use Addwiki\Mediawiki\Api\Client\SimpleRequest;
7
use BadMethodCallException;
8
use Wikibase\DataModel\Entity\BasicEntityIdParser;
9
use Wikibase\DataModel\Entity\EntityId;
10
use Wikibase\DataModel\Entity\ItemId;
11
use Wikibase\DataModel\Entity\PropertyId;
12
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookup;
13
use Wikibase\DataModel\Services\Lookup\EntityRedirectLookupException;
14
15
/**
16
 * @author Addshore
17
 *
18
 * @access private
19
 */
20
class EntityRedirectApiLookup implements EntityRedirectLookup {
21
22
	private MediawikiApi $api;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
23
24
	/**
25
	 * @param MediawikiApi $api
26
	 */
27
	public function __construct( MediawikiApi $api ) {
28
		$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
	 * @return ItemId|PropertyId
43
	 */
44
	public function getRedirectForEntityId( EntityId $entityId, $forUpdate = '' ) {
45
		$entityIdSerialization = $entityId->getSerialization();
46
47
		$params = [ 'ids' => $entityIdSerialization ];
48
		$result = $this->api->getRequest( new SimpleRequest( 'wbgetentities', $params ) );
49
50
		$entitiesData = $result['entities'];
51
		if ( !array_key_exists( $entityIdSerialization, $entitiesData ) ) {
52
			throw new EntityRedirectLookupException( $entityId, sprintf( 'Failed to get %s', $entityIdSerialization ) );
53
		}
54
55
		$entityData = $entitiesData[$entityIdSerialization];
56
		if ( !array_key_exists( 'redirects', $entityData ) ) {
57
			throw new EntityRedirectLookupException( $entityId, sprintf( '%s is not a redirect', $entityIdSerialization ) );
58
		}
59
60
		$entityIdParser = new BasicEntityIdParser();
61
		return $entityIdParser->parse( $entityData['redirects']['to'] );
62
	}
63
64
}
65