Completed
Push — master ( 16bc1d...b837ed )
by Leszek
11s
created

DisabledEntityTypesEntityLookup::getEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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
use Wikimedia\Assert\Assert;
8
9
/**
10
 * EntityLookup that checks entities have been loaded through it and throws
11
 * an exception if the accessing to that entity type is disabled.
12
 *
13
 * @since 3.9
14
 *
15
 * @license GPL-2.0+
16
 * @author Amir Sarabadani
17
 */
18
class DisabledEntityTypesEntityLookup implements EntityLookup {
19
20
	/**
21
	 * @var EntityLookup
22
	 */
23
	private $entityLookup;
24
25
	/**
26
	 * @var string[]
27
	 */
28
	private $disabledEntityTypes = [];
29
30
	/**
31
	 * @param EntityLookup $entityLookup
32
	 * @param string[] $disabledEntityTypes
33
	 */
34
	public function __construct( EntityLookup $entityLookup, array $disabledEntityTypes ) {
35
		Assert::parameterElementType( 'string', $disabledEntityTypes, '$disabledEntityTypes' );
36
37
		$this->entityLookup = $entityLookup;
38
		$this->disabledEntityTypes = $disabledEntityTypes;
39
	}
40
41
	/**
42
	 * @see EntityLookup::getEntity
43
	 *
44
	 * @param EntityId $entityId
45
	 *
46
	 * @throws EntityLookupException
47
	 * @return EntityDocument
48
	 */
49
	public function getEntity( EntityId $entityId ) {
50
		if ( in_array( $entityId->getEntityType(), $this->disabledEntityTypes ) ) {
51
			throw new EntityLookupException(
52
				$entityId,
53
				'Entity access for this type of entity is disabled: ' . $entityId->getEntityType()
54
			);
55
		}
56
57
		return $this->entityLookup->getEntity( $entityId );
58
	}
59
60
	/**
61
	 * @see EntityLookup::hasEntity
62
	 *
63
	 * @param EntityId $entityId
64
	 *
65
	 * @return bool
66
	 * @throws EntityLookupException
67
	 */
68
	public function hasEntity( EntityId $entityId ) {
69
		return $this->entityLookup->hasEntity( $entityId );
70
	}
71
72
}
73