EntityIdLabelFormatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 42
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A formatEntityId() 0 9 2
A lookupEntityLabel() 0 7 2
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 $e ) {
55
			return null;
56 4
		}
57
	}
58 4
59
}
60