EntityIdHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseTableName() 0 3 1
A completeTable() 0 9 1
A getEqualityFieldName() 0 3 1
A getInsertValues() 0 12 2
A getEqualityFieldValue() 0 7 2
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\DVHandler;
4
5
use DataValues\DataValue;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Types\Type;
8
use InvalidArgumentException;
9
use Wikibase\DataModel\Entity\EntityIdValue;
10
use Wikibase\QueryEngine\SQLStore\DataValueHandler;
11
12
/**
13
 * Represents the mapping between EntityIdValue and
14
 * the corresponding table in the store.
15
 *
16
 * @since 0.1
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class EntityIdHandler extends DataValueHandler {
22
23
	/**
24
	 * @see DataValueHandler::getBaseTableName
25
	 *
26
	 * @return string
27
	 */
28
	protected function getBaseTableName() {
29
		return 'entityid';
30
	}
31
32
	/**
33
	 * @see DataValueHandler::completeTable
34
	 */
35
	protected function completeTable( Table $table ) {
36
		// Same length as MediaWiki titles.
37
		$table->addColumn( 'value_id', Type::STRING, array( 'length' => 255 ) );
38
		// Same length as in the Wikibase tables.
39
		$table->addColumn( 'value_type', Type::STRING, array( 'length' => 32 ) );
40
41
		$table->addIndex( array( 'value_id' ) );
42
		$table->addIndex( array( 'value_type' ) );
43
	}
44
45
	/**
46
	 * @see DataValueHandler::getEqualityFieldName
47
	 *
48
	 * @return string
49
	 */
50
	public function getEqualityFieldName() {
51
		return 'value_id';
52
	}
53
54
	/**
55
	 * @see DataValueHandler::getInsertValues
56
	 *
57
	 * @param DataValue $value
58
	 *
59
	 * @return array
60
	 * @throws InvalidArgumentException
61
	 */
62
	public function getInsertValues( DataValue $value ) {
63
		if ( !( $value instanceof EntityIdValue ) ) {
64
			throw new InvalidArgumentException( '$value is not a EntityIdValue.' );
65
		}
66
67
		$values = array(
68
			'value_id' => $value->getEntityId()->getSerialization(),
69
			'value_type' => $value->getEntityId()->getEntityType(),
70
		);
71
72
		return $values;
73
	}
74
75
	/**
76
	 * @see DataValueHandler::getEqualityFieldValue
77
	 *
78
	 * @param DataValue $value
79
	 *
80
	 * @return string
81
	 * @throws InvalidArgumentException
82
	 */
83
	public function getEqualityFieldValue( DataValue $value ) {
84
		if ( !( $value instanceof EntityIdValue ) ) {
85
			throw new InvalidArgumentException( '$value is not a EntityIdValue.' );
86
		}
87
88
		return $value->getEntityId()->getSerialization();
89
	}
90
91
}
92