CachedItemIdForQueryLookup::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Wikibase\EntityStore\Cache;
4
5
use Ask\Language\Description\Description;
6
use Ask\Language\Option\QueryOptions;
7
use OutOfBoundsException;
8
use Wikibase\EntityStore\ItemIdForQueryLookup;
9
10
/**
11
 * Internal class
12
 *
13
 * @licence GPLv2+
14
 * @author Thomas Pellissier Tanon
15
 */
16
class CachedItemIdForQueryLookup implements ItemIdForQueryLookup {
17
18
	/**
19
	 * @var ItemIdForQueryLookup
20
	 */
21
	private $itemIdForQueryLookup;
22
23
	/**
24
	 * @var EntityIdForQueryCache
25
	 */
26
	private $entityIdForQueryCache;
27
28
	/**
29
	 * @param ItemIdForQueryLookup $itemIdForQueryLookup
30
	 * @param EntityIdForQueryCache $entityIdForQueryCache
31
	 */
32 3
	public function __construct(
33
		ItemIdForQueryLookup $itemIdForQueryLookup,
34
		EntityIdForQueryCache $entityIdForQueryCache
35
	) {
36 3
		$this->itemIdForQueryLookup = $itemIdForQueryLookup;
37 3
		$this->entityIdForQueryCache = $entityIdForQueryCache;
38 3
	}
39
40
	/**
41
	 * @see ItemIdForQueryLookup::getItemIdsForQuery
42
	 */
43 3
	public function getItemIdsForQuery( Description $description, QueryOptions $queryOptions = null ) {
44
		try {
45 3
			return $this->entityIdForQueryCache->fetch( $description, $queryOptions, 'item' );
46 2
		} catch( OutOfBoundsException $e ) {
47 2
			$itemIds = $this->itemIdForQueryLookup->getItemIdsForQuery( $description, $queryOptions );
48 2
			$this->entityIdForQueryCache->save( $description, $queryOptions, 'item', $itemIds );
49 2
			return $itemIds;
50
		}
51
	}
52
}
53