CachedItemLookup::getItemForId()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Wikibase\EntityStore\Cache;
4
5
use Wikibase\DataModel\Entity\ItemId;
6
use Wikibase\DataModel\Services\Lookup\ItemLookup;
7
8
/**
9
 * Internal class
10
 *
11
 * @licence GPLv2+
12
 * @author Thomas Pellissier Tanon
13
 */
14
class CachedItemLookup implements ItemLookup {
15
16
	/**
17
	 * @var ItemLookup
18
	 */
19
	private $itemLookup;
20
21
	/**
22
	 * @var EntityDocumentCache
23
	 */
24
	private $entityCache;
25
26
	/**
27
	 * @param ItemLookup $itemLookup
28
	 * @param EntityDocumentCache $entityCache
29
	 */
30 4
	public function __construct( ItemLookup $itemLookup, EntityDocumentCache $entityCache ) {
31 4
		$this->itemLookup = $itemLookup;
32 4
		$this->entityCache = $entityCache;
33 4
	}
34
35
	/**
36
	 * @see ItemLookup::getItemForId
37
	 */
38 4
	public function getItemForId( ItemId $itemId ) {
39 4
		$item = $this->entityCache->fetch( $itemId );
40
41 4
		if( $item === null ) {
42 3
			$item = $this->itemLookup->getItemForId( $itemId );
43 3
			if( $item !== null ) {
44 2
				$this->entityCache->save( $item );
45 2
			}
46 3
		}
47
48 4
		return $item;
49
	}
50
}
51