LanguageLabelDescriptionLookup   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 62
ccs 16
cts 18
cp 0.8889
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLabel() 0 13 3
A getDescription() 0 13 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Lookup;
4
5
use Wikibase\DataModel\Entity\EntityId;
6
use Wikibase\DataModel\Term\Term;
7
8
/**
9
 * @since 1.1
10
 *
11
 * @license GPL-2.0-or-later
12
 * @author Katie Filbert < [email protected] >
13
 * @author Marius Hoch < [email protected] >
14
 * @author Addshore
15
 */
16
class LanguageLabelDescriptionLookup implements LabelDescriptionLookup {
17
18
	/**
19
	 * @var TermLookup
20
	 */
21
	private $termLookup;
22
23
	/**
24
	 * @var string
25
	 */
26
	private $languageCode;
27
28
	/**
29
	 * @param TermLookup $termLookup
30
	 * @param string $languageCode
31
	 */
32 4
	public function __construct( TermLookup $termLookup, $languageCode ) {
33 4
		$this->termLookup = $termLookup;
34 4
		$this->languageCode = $languageCode;
35 4
	}
36
37
	/**
38
	 * @param EntityId $entityId
39
	 *
40
	 * @throws LabelDescriptionLookupException
41
	 * @return Term|null
42
	 */
43 2
	public function getLabel( EntityId $entityId ) {
44
		try {
45 2
			$text = $this->termLookup->getLabel( $entityId, $this->languageCode );
46 2
		} catch ( TermLookupException $ex ) {
47
			throw new LabelDescriptionLookupException( $entityId, 'Failed to lookup label', $ex );
48
		}
49
50 2
		if ( $text === null ) {
51 1
			return null;
52
		}
53
54 1
		return new Term( $this->languageCode, $text );
55
	}
56
57
	/**
58
	 * @param EntityId $entityId
59
	 *
60
	 * @throws LabelDescriptionLookupException
61
	 * @return Term|null
62
	 */
63 2
	public function getDescription( EntityId $entityId ) {
64
		try {
65 2
			$text = $this->termLookup->getDescription( $entityId, $this->languageCode );
66 2
		} catch ( TermLookupException $ex ) {
67
			throw new LabelDescriptionLookupException( $entityId, 'Failed to lookup description', $ex );
68
		}
69
70 2
		if ( $text === null ) {
71 1
			return null;
72
		}
73
74 1
		return new Term( $this->languageCode, $text );
75
	}
76
77
}
78