Passed
Push — master ( 4ffe17...21fc8e )
by Jeroen De
02:43 queued 10s
created

InMemoryEntityLookup::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.1481
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
9
/**
10
 * EntityLookup that uses an in memory array to retrieve the requested information.
11
 * One can also specify exceptions that should be thrown when an entity with their
12
 * associated ID is requested.
13
 *
14
 * This class can be used as a fake in tests.
15
 *
16
 * @since 2.0
17
 *
18
 * @license GPL-2.0-or-later
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class InMemoryEntityLookup implements EntityLookup {
22
23
	/**
24
	 * @var EntityDocument[]
25
	 */
26
	private $entities = [];
27
28
	/**
29
	 * @var EntityLookupException[]
30
	 */
31
	private $exceptions = [];
32
33
	/**
34
	 * @param EntityDocument ...$entities
35
	 */
36
	public function __construct( ...$entities ) {
37
		foreach ( $entities as $entity ) {
38 2
			$this->addEntity( $entity );
39 2
		}
40
	}
41
42
	/**
43 2
	 * @param EntityDocument $entity
44 2
	 *
45
	 * @throws InvalidArgumentException
46
	 */
47
	public function addEntity( EntityDocument $entity ) {
48
		if ( $entity->getId() === null ) {
49
			throw new InvalidArgumentException( 'The entity needs to have an ID' );
50
		}
51
52
		$this->entities[$entity->getId()->getSerialization()] = $entity;
53
	}
54 2
55 2
	/**
56 2
	 * Registers an exception that will be thrown when a entity with the id in the exception is requested.
57
	 * If an exception with the same EntityId was already present it will be replaced by the new one.
58
	 *
59
	 * @since 3.1
60
	 *
61
	 * @param EntityLookupException $exception
62
	 */
63
	public function addException( EntityLookupException $exception ) {
64
		$this->exceptions[$exception->getEntityId()->getSerialization()] = $exception;
65
	}
66 3
67 3
	/**
68
	 * @see EntityLookup::getEntity
69 3
	 *
70 1
	 * @param EntityId $entityId
71
	 *
72
	 * @throws EntityLookupException
73 2
	 * @return EntityDocument
74
	 */
75
	public function getEntity( EntityId $entityId ) {
76
		$this->throwExceptionIfNeeded( $entityId );
77
78
		if ( array_key_exists( $entityId->getSerialization(), $this->entities ) ) {
79
			return $this->entities[$entityId->getSerialization()];
80
		}
81
82
		return null;
83
	}
84 3
85 3
	/**
86
	 * @see EntityLookup::hasEntity
87 3
	 *
88
	 * @param EntityId $entityId
89
	 *
90 6
	 * @throws EntityLookupException
91 6
	 * @return bool
92 2
	 */
93
	public function hasEntity( EntityId $entityId ) {
94 6
		$this->throwExceptionIfNeeded( $entityId );
95
96
		return array_key_exists( $entityId->getSerialization(), $this->entities );
97
	}
98
99
	private function throwExceptionIfNeeded( EntityId $entityId ) {
100
		if ( array_key_exists( $entityId->getSerialization(), $this->exceptions ) ) {
101
			throw $this->exceptions[$entityId->getSerialization()];
102
		}
103
	}
104
105
}
106