Completed
Push — master ( 959f5f...dece17 )
by
unknown
02:35
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 WikibaseQuality\ConstraintReport\ConstraintCheck\Helper;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Services\Lookup\EntityLookup;
8
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
9
10
/**
11
 * An {@link EntityLookup} which ignores any exceptions
12
 * which occur while retrieving an entity
13
 * and instead pretends the entity does not exist.
14
 *
15
 * @license GPL-2.0-or-later
16
 */
17
class ExceptionIgnoringEntityLookup implements EntityLookup {
18
19
	/**
20
	 * @var EntityLookup
21
	 */
22
	private $lookup;
23
24
	public function __construct( EntityLookup $lookup ) {
25
		$this->lookup = $lookup;
26
	}
27
28
	/**
29
	 * Attempt to retrieve the entity,
30
	 * returning `null` if any errors occur.
31
	 *
32
	 * @param EntityId $entityId
33
	 * @return EntityDocument|null
34
	 */
35
	public function getEntity( EntityId $entityId ) {
36
		try {
37
			return $this->lookup->getEntity( $entityId );
38
		} catch ( EntityLookupException $exception ) {
39
			return null;
40
		}
41
	}
42
43
	/**
44
	 * Returns whether the given entity can potentially be looked up using {@link getEntity()}.
45
	 * Note that this does not guarantee {@link getEntity()} will return an {@link EntityDocument} –
46
	 * it may still return `null` if an error occurs retrieving the entity.
47
	 *
48
	 * @param EntityId $entityId
49
	 * @return bool
50
	 */
51
	public function hasEntity( EntityId $entityId ) {
52
		return $this->lookup->hasEntity( $entityId );
53
	}
54
55
}
56