Completed
Push — master ( aac542...3ee696 )
by Jeroen De
05:47
created

IdLookup::getEntityIdByLabel()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 3
crap 3.072
1
<?php
2
3
namespace Queryr\TermStore;
4
5
use Doctrine\DBAL\DBALException;
6
7
/**
8
 * Package private.
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class IdLookup implements EntityIdLookup {
14
15
	private $labelTable;
16
	private $aliasesTable;
17
18 16
	public function __construct( TableQueryExecutor $labelTable, TableQueryExecutor $aliasesTable ) {
19 16
		$this->labelTable = $labelTable;
20 16
		$this->aliasesTable = $aliasesTable;
21 16
	}
22
23
	/**
24
	 * Returns the first matching entity id. Case insensitive.
25
	 *
26
	 * @param string $labelLanguageCode
27
	 * @param string $labelText
28
	 *
29
	 * @return string|null
30
	 * @throws TermStoreException
31
	 */
32 2
	public function getIdByLabel( $labelLanguageCode, $labelText ) {
33 2
		return $this->getEntityIdByLabel( $labelLanguageCode, $labelText );
34
	}
35
36
	/**
37
	 * Returns the first matching item id. Case insensitive.
38
	 *
39
	 * @param string $labelLanguageCode
40
	 * @param string $labelText
41
	 *
42
	 * @return string|null
43
	 * @throws TermStoreException
44
	 */
45 2
	public function getItemIdByLabel( $labelLanguageCode, $labelText ) {
46 2
		return $this->getEntityIdByLabel( $labelLanguageCode, $labelText, 'item' );
47
	}
48
49
	/**
50
	 * Returns the first matching property id. Case insensitive.
51
	 *
52
	 * @param string $labelLanguageCode
53
	 * @param string $labelText
54
	 *
55
	 * @return string|null
56
	 * @throws TermStoreException
57
	 */
58 2
	public function getPropertyIdByLabel( $labelLanguageCode, $labelText ) {
59 2
		return $this->getEntityIdByLabel( $labelLanguageCode, $labelText, 'property' );
60
	}
61
62
	/**
63
	 * @param string $labelLanguageCode
64
	 * @param string $labelText
65
	 * @param string|null $entityTypeFilter
66
	 *
67
	 * @return string|null
68
	 * @throws TermStoreException
69
	 */
70 12
	private function getEntityIdByLabel( $labelLanguageCode, $labelText, $entityTypeFilter = null ) {
71
		$conditions = [
72 12
			'text_lowercase' => strtolower( $labelText ),
73 12
			'language' => $labelLanguageCode,
74
		];
75
76 12
		if ( $entityTypeFilter !== null ) {
77 8
			$conditions['entity_type'] = $entityTypeFilter;
78
		}
79
80
		try {
81 12
			return $this->labelTable->selectOneField(
82 12
				'entity_id',
83 12
				$conditions
84
			);
85
		}
86
		catch ( DBALException $ex ) {
87
			throw new TermStoreException( $ex->getMessage(), $ex );
88
		}
89
	}
90
91
	/**
92
	 * Returns the first matching entity id. Case insensitive.
93
	 *
94
	 * @param string $languageCode
95
	 * @param string $termText
96
	 *
97
	 * @return string|null
98
	 * @throws TermStoreException
99
	 */
100 2
	public function getIdByText( $languageCode, $termText ) {
101 2
		return $this->getEntityIdByText( $languageCode, $termText );
102
	}
103
104
	/**
105
	 * Returns the first matching item id. Case insensitive.
106
	 *
107
	 * @param string $languageCode
108
	 * @param string $termText
109
	 *
110
	 * @return string|null
111
	 * @throws TermStoreException
112
	 */
113 2
	public function getItemIdByText( $languageCode, $termText ) {
114 2
		return $this->getEntityIdByText( $languageCode, $termText, 'item' );
115
	}
116
117
	/**
118
	 * Returns the first matching property id. Case insensitive.
119
	 *
120
	 * @param string $languageCode
121
	 * @param string $termText
122
	 *
123
	 * @return string|null
124
	 * @throws TermStoreException
125
	 */
126 2
	public function getPropertyIdByText( $languageCode, $termText ) {
127 2
		return $this->getEntityIdByText( $languageCode, $termText, 'property' );
128
	}
129
130
	/**
131
	 * @param string $languageCode
132
	 * @param string $termText
133
	 * @param string|null $entityTypeFilter
134
	 *
135
	 * @return string|null
136
	 * @throws TermStoreException
137
	 */
138 6
	private function getEntityIdByText( $languageCode, $termText, $entityTypeFilter = null ) {
139 6
		$labelMatch = $this->getEntityIdByLabel( $languageCode, $termText, $entityTypeFilter );
140
141 6
		if ( $labelMatch !== null ) {
142 3
			return $labelMatch;
143
		}
144
145 3
		return $this->getIdByAlias( $languageCode, $termText, $entityTypeFilter );
146
	}
147
148
	/**
149
	 * @param string $aliasLanguageCode
150
	 * @param string $aliasText
151
	 * @param string|null $entityTypeFilter
152
	 *
153
	 * @return string|null
154
	 * @throws TermStoreException
155
	 */
156 3
	private function getIdByAlias( $aliasLanguageCode, $aliasText, $entityTypeFilter = null ) {
157
		$conditions = [
158 3
			'text_lowercase' => strtolower( $aliasText ),
159 3
			'language' => $aliasLanguageCode
160
		];
161
162 3
		if ( $entityTypeFilter !== null ) {
163 2
			$conditions['entity_type'] = $entityTypeFilter;
164
		}
165
166
		try {
167 3
			return $this->aliasesTable->selectOneField(
168 3
				'entity_id',
169 3
				$conditions
170
			);
171
		}
172
		catch ( DBALException $ex ) {
173
			throw new TermStoreException( $ex->getMessage(), $ex );
174
		}
175
	}
176
177
}