getDataTypeIdForProperty()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Wikibase\DataModel\Services\Lookup;
4
5
use Wikibase\DataModel\Entity\Property;
6
use Wikibase\DataModel\Entity\PropertyId;
7
8
/**
9
 * PropertyDataTypeLookup that uses an EntityLookup to find
10
 * a property's data type ID.
11
 *
12
 * @since 1.1
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class EntityRetrievingDataTypeLookup implements PropertyDataTypeLookup {
18
19
	/**
20
	 * @var EntityLookup
21
	 */
22
	private $entityLookup;
23
	private array $propertyIdsInProcess = [];
24 4
25 4
	public function __construct( EntityLookup $entityLookup ) {
26 4
		$this->entityLookup = $entityLookup;
27
	}
28
29
	/**
30
	 * @since 2.0
31
	 *
32
	 * @param PropertyId $propertyId
33
	 *
34
	 * @return string
35
	 * @throws PropertyDataTypeLookupException
36 4
	 */
37 4
	public function getDataTypeIdForProperty( PropertyId $propertyId ) {
38
		if ( array_key_exists( $propertyId->getSerialization(), $this->propertyIdsInProcess ) ) {
39
			// avoid self-referencing loop for newly created properties (T374230)
40
			throw new PropertyDataTypeLookupException( $propertyId, 'loop detected' );
41
		}
42
		$this->propertyIdsInProcess[ $propertyId->getSerialization() ] = true;
43
		try {
44
			return $this->getProperty( $propertyId )->getDataTypeId();
45
		} finally {
46 4
			unset( $this->propertyIdsInProcess[ $propertyId->getSerialization() ] );
47 4
		}
48
	}
49 4
50
	/**
51
	 * @param PropertyId $propertyId
52
	 *
53 4
	 * @return Property
54
	 * @throws PropertyDataTypeLookupException
55
	 */
56
	private function getProperty( PropertyId $propertyId ) {
57
		$property = $this->entityLookup->getEntity( $propertyId );
58
59
		if ( !( $property instanceof Property ) ) {
60
			throw new PropertyDataTypeLookupException( $propertyId );
61
		}
62
63
		return $property;
64
	}
65
66
}
67