EntityRetrievingTermLookup   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 151
ccs 34
cts 34
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLabel() 0 9 2
A getLabels() 0 5 1
A getDescription() 0 9 2
A getDescriptions() 0 5 1
A getAllLabels() 0 12 3
A getAllDescriptions() 0 12 3
A fetchEntity() 0 13 3
1
<?php
2
3
namespace Wikibase\DataModel\Services\Lookup;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Term\DescriptionsProvider;
8
use Wikibase\DataModel\Term\LabelsProvider;
9
use Wikibase\DataModel\Term\TermList;
10
11
/**
12
 * @since 1.1
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Katie Filbert < [email protected] >
16
 * @author Addshore
17
 */
18
class EntityRetrievingTermLookup implements TermLookup {
19
20
	/**
21
	 * @var EntityLookup
22
	 */
23
	private $entityLookup;
24
25
	/**
26
	 * @var TermList[] Labels in all languages, indexed by entity ID serialization.
27
	 */
28
	private $labels;
29
30
	/**
31 14
	 * @var TermList[] Descriptions in all languages, indexed by entity ID serialization.
32 14
	 */
33 14
	private $descriptions;
34
35
	public function __construct( EntityLookup $entityLookup ) {
36
		$this->entityLookup = $entityLookup;
37
	}
38
39
	/**
40
	 * @see TermLookup::getLabel()
41
	 *
42
	 * @param EntityId $entityId
43
	 * @param string $languageCode
44 4
	 *
45 4
	 * @return string|null
46
	 * @throws TermLookupException
47 2
	 */
48 1
	public function getLabel( EntityId $entityId, $languageCode ) {
49
		$labels = $this->getAllLabels( $entityId, [ $languageCode ] );
50
51 1
		if ( $labels->hasTermForLanguage( $languageCode ) ) {
52
			return $labels->getByLanguage( $languageCode )->getText();
53
		}
54
55
		return null;
56
	}
57
58
	/**
59
	 * @see TermLookup::getLabels()
60
	 *
61
	 * @param EntityId $entityId
62
	 * @param string[] $languages
63 3
	 *
64 3
	 * @throws TermLookupException
65 3
	 * @return string[]
66
	 */
67 3
	public function getLabels( EntityId $entityId, array $languages ) {
68
		$labels = $this->getAllLabels( $entityId, $languages )->toTextArray();
69
70
		return array_intersect_key( $labels, array_flip( $languages ) );
71
	}
72
73
	/**
74
	 * @see TermLookup::getDescription()
75
	 *
76
	 * @param EntityId $entityId
77
	 * @param string $languageCode
78
	 *
79 4
	 * @throws TermLookupException
80 4
	 * @return string|null
81
	 */
82 2
	public function getDescription( EntityId $entityId, $languageCode ) {
83 1
		$descriptions = $this->getAllDescriptions( $entityId, [ $languageCode ] );
84
85
		if ( $descriptions->hasTermForLanguage( $languageCode ) ) {
86 1
			return $descriptions->getByLanguage( $languageCode )->getText();
87
		}
88
89
		return null;
90
	}
91
92
	/**
93
	 * @see TermLookup::getDescriptions()
94
	 *
95
	 * @param EntityId $entityId
96
	 * @param string[] $languages
97
	 *
98 3
	 * @throws TermLookupException
99 3
	 * @return string[]
100 3
	 */
101
	public function getDescriptions( EntityId $entityId, array $languages ) {
102 3
		$descriptions = $this->getAllDescriptions( $entityId, $languages )->toTextArray();
103
104
		return array_intersect_key( $descriptions, array_flip( $languages ) );
105
	}
106
107
	/**
108
	 * @param EntityId $entityId
109
	 * @param string[] $languageCodes Not used for filtering but in thrown exceptions.
110
	 *
111
	 * @throws TermLookupException
112 14
	 * @return TermList
113 14
	 */
114
	private function getAllLabels( EntityId $entityId, array $languageCodes ) {
115 14
		$id = $entityId->getSerialization();
116 14
117 10
		if ( !isset( $this->labels[$id] ) ) {
118
			$entity = $this->fetchEntity( $entityId, $languageCodes );
119 10
			$this->labels[$id] = $entity instanceof LabelsProvider
120
				? $entity->getLabels()
121
				: new TermList();
122
		}
123
124
		return $this->labels[$id];
125
	}
126
127
	/**
128
	 * @param EntityId $entityId
129 14
	 * @param string[] $languageCodes Not used for filtering but in thrown exceptions.
130
	 *
131 14
	 * @throws TermLookupException
132 14
	 * @return TermList
133 2
	 */
134
	private function getAllDescriptions( EntityId $entityId, array $languageCodes ) {
135
		$id = $entityId->getSerialization();
136 12
137 2
		if ( !isset( $this->descriptions[$id] ) ) {
138
			$entity = $this->fetchEntity( $entityId, $languageCodes );
139
			$this->descriptions[$id] = $entity instanceof DescriptionsProvider
140 10
				? $entity->getDescriptions()
141
				: new TermList();
142
		}
143
144
		return $this->descriptions[$id];
145
	}
146
147
	/**
148
	 * @param EntityId $entityId
149
	 * @param string[] $languages Not used for filtering but in thrown exceptions.
150
	 *
151
	 * @throws TermLookupException
152
	 * @return EntityDocument
153
	 */
154
	private function fetchEntity( EntityId $entityId, array $languages ) {
155
		try {
156
			$entity = $this->entityLookup->getEntity( $entityId );
157
		} catch ( EntityLookupException $ex ) {
158
			throw new TermLookupException( $entityId, $languages, 'The entity could not be loaded', $ex );
159
		}
160
161
		if ( $entity === null ) {
162
			throw new TermLookupException( $entityId, $languages, 'The entity could not be loaded' );
163
		}
164
165
		return $entity;
166
	}
167
168
}
169