EntityInserter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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