EntityInserter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A insertEntity() 0 15 2
A getEntityInserterFor() 0 9 3
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\EntityStore;
4
5
use Doctrine\DBAL\Connection;
6
use Wikibase\DataModel\Entity\EntityDocument;
7
use Wikibase\QueryEngine\QueryEngineException;
8
9
/**
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class EntityInserter {
13
14
	private $connection;
15
	private $entityInserters;
16
17
	/**
18
	 * @param Connection $connection
19
	 * @param EntityInsertionStrategy[] $entityInserters
20
	 */
21
	public function __construct( Connection $connection, array $entityInserters ) {
22
		$this->connection = $connection;
23
		$this->entityInserters = $entityInserters;
24
	}
25
26
	public function insertEntity( EntityDocument $entity ) {
27
		$inserter = $this->getEntityInserterFor( $entity );
28
29
		$this->connection->beginTransaction();
30
31
		try {
32
			$inserter->insertEntity( $entity );
33
		}
34
		catch ( QueryEngineException $ex ) {
35
			$this->connection->rollBack();
36
			throw $ex;
37
		}
38
39
		$this->connection->commit();
40
	}
41
42
	/**
43
	 * @param EntityDocument $entity
44
	 * @return EntityInsertionStrategy
45
	 * @throws QueryEngineException
46
	 */
47
	private function getEntityInserterFor( EntityDocument $entity ) {
48
		foreach ( $this->entityInserters as $entityInserter ) {
49
			if ( $entityInserter->canInsert( $entity ) ) {
50
				return $entityInserter;
51
			}
52
		}
53
54
		throw new QueryEngineException( 'There is no insertion strategy for ' . $entity->getId() );
55
	}
56
57
}
58