RedirectResolvingEntityLookup   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getEntity() 0 7 2
A hasEntity() 0 7 2
1
<?php
2
3
namespace Wikibase\DataModel\Services\Lookup;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Entity\EntityId;
7
8
/**
9
 * Implementation of EntityLookup that opaquely resolves one level
10
 * of redirects when looking up entities.
11
 *
12
 * @since 2.0
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Daniel Kinzler
16
 */
17
class RedirectResolvingEntityLookup implements EntityLookup {
18
19
	/**
20
	 * An EntityRedirectResolvingDecorator wrapping and emulating a EntityLookup.
21
	 *
22
	 * @note This does not formally implement EntityLookup!
23
	 *
24
	 * @var EntityLookup
25
	 */
26
	private $lookup;
27
28
	/**
29
	 * @param EntityLookup $lookup The lookup to use
30
	 */
31 9
	public function __construct( EntityLookup $lookup ) {
32 9
		$this->lookup = $lookup;
33 9
	}
34
35
	/**
36
	 * @see EntityLookup::getEntity
37
	 *
38
	 * If the given entity ID points to a redirect, that redirect is resolved and the
39
	 * target entity returned.
40
	 *
41
	 * Callers can detect the presence of a redirect by comparing the ID of the returned
42
	 * Entity with the request ID.
43
	 *
44
	 * @param EntityId $entityId
45
	 *
46
	 * @return EntityDocument|null
47
	 * @throws EntityLookupException
48
	 */
49 5
	public function getEntity( EntityId $entityId ) {
50
		try {
51 5
			return $this->lookup->getEntity( $entityId );
52 2
		} catch ( UnresolvedEntityRedirectException $ex ) {
53 2
			return $this->lookup->getEntity( $ex->getRedirectTargetId() );
54
		}
55
	}
56
57
	/**
58
	 * @see EntityLookup::hasEntity
59
	 *
60
	 * If the given entity ID points to a redirect, that redirect is resolved and the
61
	 * existence of the target entity is checked.
62
	 *
63
	 * @param EntityId $entityId
64
	 *
65
	 * @return bool
66
	 * @throws EntityLookupException
67
	 */
68 4
	public function hasEntity( EntityId $entityId ) {
69
		try {
70 4
			return $this->lookup->hasEntity( $entityId );
71 2
		} catch ( UnresolvedEntityRedirectException $ex ) {
72 2
			return $this->lookup->hasEntity( $ex->getRedirectTargetId() );
73
		}
74
	}
75
76
}
77