Passed
Push — master ( cb35bf...01d583 )
by Marius
35s
created

InMemoryEntityLookup   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 93
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A addEntity() 0 7 2
A addException() 0 3 1
A getEntity() 0 9 2
A hasEntity() 0 5 1
A throwExceptionIfNeeded() 0 5 2
A getItemForId() 0 3 1
A getPropertyForId() 0 3 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\Lookup;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\DataModel\Entity\EntityId;
8
use Wikibase\DataModel\Entity\ItemId;
9
use Wikibase\DataModel\Entity\PropertyId;
10
11
/**
12
 * EntityLookup that uses an in memory array to retrieve the requested information.
13
 * One can also specify exceptions that should be thrown when an entity with their
14
 * associated ID is requested.
15
 *
16
 * This class can be used as a fake in tests.
17
 *
18
 * @since 2.0
19
 *
20
 * @license GPL-2.0-or-later
21
 * @author Jeroen De Dauw < [email protected] >
22
 */
23
class InMemoryEntityLookup implements EntityLookup, ItemLookup, PropertyLookup {
24
25
	/**
26
	 * @var EntityDocument[]
27
	 */
28
	private $entities = [];
29
30
	/**
31
	 * @var EntityLookupException[]
32
	 */
33
	private $exceptions = [];
34
35
	/**
36
	 * @param EntityDocument ...$entities
37
	 */
38 2
	public function __construct( ...$entities ) {
39 2
		foreach ( $entities as $entity ) {
40
			$this->addEntity( $entity );
41
		}
42
	}
43 2
44 2
	/**
45
	 * @param EntityDocument $entity
46
	 *
47
	 * @throws InvalidArgumentException
48
	 */
49
	public function addEntity( EntityDocument $entity ) {
50
		if ( $entity->getId() === null ) {
51
			throw new InvalidArgumentException( 'The entity needs to have an ID' );
52
		}
53
54 2
		$this->entities[$entity->getId()->getSerialization()] = $entity;
55 2
	}
56 2
57
	/**
58
	 * Registers an exception that will be thrown when a entity with the id in the exception is requested.
59
	 * If an exception with the same EntityId was already present it will be replaced by the new one.
60
	 *
61
	 * @since 3.1
62
	 *
63
	 * @param EntityLookupException $exception
64
	 */
65
	public function addException( EntityLookupException $exception ) {
66 3
		$this->exceptions[$exception->getEntityId()->getSerialization()] = $exception;
67 3
	}
68
69 3
	/**
70 1
	 * @see EntityLookup::getEntity
71
	 *
72
	 * @param EntityId $entityId
73 2
	 *
74
	 * @throws EntityLookupException
75
	 * @return EntityDocument
76
	 */
77
	public function getEntity( EntityId $entityId ) {
78
		$this->throwExceptionIfNeeded( $entityId );
79
80
		if ( array_key_exists( $entityId->getSerialization(), $this->entities ) ) {
81
			return $this->entities[$entityId->getSerialization()];
82
		}
83
84 3
		return null;
85 3
	}
86
87 3
	/**
88
	 * @see EntityLookup::hasEntity
89
	 *
90 6
	 * @param EntityId $entityId
91 6
	 *
92 2
	 * @throws EntityLookupException
93
	 * @return bool
94 6
	 */
95
	public function hasEntity( EntityId $entityId ) {
96
		$this->throwExceptionIfNeeded( $entityId );
97
98
		return array_key_exists( $entityId->getSerialization(), $this->entities );
99
	}
100
101
	private function throwExceptionIfNeeded( EntityId $entityId ) {
102
		if ( array_key_exists( $entityId->getSerialization(), $this->exceptions ) ) {
103
			throw $this->exceptions[$entityId->getSerialization()];
104
		}
105
	}
106
107
	public function getItemForId( ItemId $itemId ) {
108
		return ( new LegacyAdapterItemLookup( $this ) )->getItemForId( $itemId );
109
	}
110
111
	public function getPropertyForId( PropertyId $propertyId ) {
112
		return ( new LegacyAdapterPropertyLookup( $this ) )->getpropertyForId( $propertyId );
113
	}
114
115
}
116