ValuelessSnakStore::canStore()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\SnakStore;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7
use InvalidArgumentException;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\QueryEngine\QueryEngineException;
10
11
/**
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class ValuelessSnakStore extends SnakStore {
16
17
	private $connection;
18
	private $tableName;
19
20
	public function __construct( Connection $connection, $tableName ) {
21
		$this->connection = $connection;
22
		$this->tableName = $tableName;
23
	}
24
25
	public function canStore( SnakRow $snakRow ) {
26
		return $snakRow instanceof ValuelessSnakRow;
27
	}
28
29
	public function storeSnakRow( SnakRow $snakRow ) {
30
		if ( !$this->canStore( $snakRow ) ) {
31
			throw new InvalidArgumentException( 'Can only store ValuelessSnakRow in ValuelessSnakStore' );
32
		}
33
34
		/**
35
		 * @var ValuelessSnakRow $snakRow
36
		 */
37
		try {
38
			$this->insertSnakRow( $snakRow );
39
		}
40
		catch ( DBALException $ex ) {
41
			throw new QueryEngineException( $ex->getMessage(), 0, $ex );
42
		}
43
	}
44
45
	private function insertSnakRow( ValuelessSnakRow $snakRow ) {
46
		$this->connection->insert(
47
			$this->tableName,
48
			array(
49
				'subject_id' => $snakRow->getSubjectId()->getSerialization(),
50
				'subject_type' => $snakRow->getSubjectId()->getEntityType(),
51
				'property_id' => $snakRow->getPropertyId(),
52
				'statement_rank' => $snakRow->getStatementRank(),
53
				'snak_type' => $snakRow->getInternalSnakType(),
54
			)
55
		);
56
	}
57
58
	public function removeSnaksOfSubject( EntityId $subjectId ) {
59
		try {
60
			$this->deleteSnakRow( $subjectId );
61
		}
62
		catch ( DBALException $ex ) {
63
			throw new QueryEngineException( $ex->getMessage(), 0, $ex );
64
		}
65
	}
66
67
	private function deleteSnakRow( EntityId $subjectId ) {
68
		$this->connection->delete(
69
			$this->tableName,
70
			array( 'subject_id' => $subjectId->getSerialization() )
71
		);
72
	}
73
74
}
75