Passed
Push — master ( 35164b...6a3f49 )
by Jeroen De
49s
created

ExceptionIgnoringEntityLookup::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
 * An {@link EntityLookup} which ignores any exceptions
10
 * which occur while retrieving an entity
11
 * and instead pretends the entity does not exist.
12
 *
13
 * @license GPL-2.0-or-later
14
 */
15
class ExceptionIgnoringEntityLookup implements EntityLookup {
16
17
	/**
18
	 * @var EntityLookup
19
	 */
20
	private $lookup;
21
22
	public function __construct( EntityLookup $lookup ) {
23
		$this->lookup = $lookup;
24
	}
25
26
	/**
27
	 * Attempt to retrieve the entity,
28
	 * returning `null` if any errors occur.
29
	 *
30
	 * @param EntityId $entityId
31
	 * @return EntityDocument|null
32
	 */
33
	public function getEntity( EntityId $entityId ) {
34
		try {
35
			return $this->lookup->getEntity( $entityId );
36
		} catch ( EntityLookupException $exception ) {
37
			return null;
38
		}
39
	}
40
41
	/**
42
	 * Returns whether the given entity can potentially be looked up using {@link getEntity()}.
43
	 * Note that this does not guarantee {@link getEntity()} will return an {@link EntityDocument} –
44
	 * it may still return `null` if an error occurs retrieving the entity.
45
	 *
46
	 * @param EntityId $entityId
47
	 * @return bool
48
	 */
49
	public function hasEntity( EntityId $entityId ) {
50
		return $this->lookup->hasEntity( $entityId );
51
	}
52
53
}
54