ExceptionIgnoringEntityLookup   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getEntity() 0 7 2
A hasEntity() 0 3 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