EntityIdLabelFormatter::lookupEntityLabel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Wikibase\DataModel\Services\EntityId;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookupException;
7
use Wikibase\DataModel\Services\Lookup\LabelLookup;
8
use Wikibase\DataModel\Term\Term;
9
10
/**
11
 * @since 1.1
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Katie Filbert < [email protected] >
16
 * @author Daniel Kinzler
17
 */
18
class EntityIdLabelFormatter implements EntityIdFormatter {
19
20
	/**
21
	 * @var LabelLookup
22
	 */
23
	private $labelLookup;
24
25
	public function __construct( LabelLookup $labelLookup ) {
26
		$this->labelLookup = $labelLookup;
27
	}
28 4
29 4
	/**
30 4
	 * @see EntityIdFormatter::formatEntityId
31
	 *
32
	 * @param EntityId $entityId
33
	 *
34
	 * @return string Plain text
35
	 */
36
	public function formatEntityId( EntityId $entityId ) {
37
		$term = $this->lookupEntityLabel( $entityId );
38
		if ( $term !== null ) {
39 4
			return $term->getText();
40 4
		}
41 4
42 1
		// @fixme check if the entity is deleted and format differently?
43
		return $entityId->getSerialization();
44
	}
45
46 3
	/**
47
	 * @param EntityId $entityId
48
	 *
49
	 * @return Term|null Null if no label was found or the entity does not exist
50
	 */
51
	protected function lookupEntityLabel( EntityId $entityId ) {
52
		try {
53
			return $this->labelLookup->getLabel( $entityId );
54
		} catch ( LabelDescriptionLookupException ) {
55
			return null;
56 4
		}
57
	}
58 4
59
}
60