RedirectResolvingEntityLookup::getEntity()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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