EntityRemover   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 removeEntity() 0 15 2
A getEntityRemoverFor() 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 EntityRemover {
13
14
	private $connection;
15
	private $entityRemovers;
16
17
	/**
18
	 * @param Connection $connection
19
	 * @param EntityRemovalStrategy[] $entityRemovers
20
	 */
21
	public function __construct( Connection $connection, array $entityRemovers ) {
22
		$this->connection = $connection;
23
		$this->entityRemovers = $entityRemovers;
24
	}
25
26
	public function removeEntity( EntityDocument $entity ) {
27
		$inserter = $this->getEntityRemoverFor( $entity );
28
29
		$this->connection->beginTransaction();
30
31
		try {
32
			$inserter->removeEntity( $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 EntityRemovalStrategy
45
	 * @throws QueryEngineException
46
	 */
47
	private function getEntityRemoverFor( EntityDocument $entity ) {
48
		foreach ( $this->entityRemovers as $entityInserter ) {
49
			if ( $entityInserter->canRemove( $entity ) ) {
50
				return $entityInserter;
51
			}
52
		}
53
54
		throw new QueryEngineException( 'There is no removal strategy for ' . $entity->getId() );
55
	}
56
57
}
58