EntitiesImporter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A run() 0 19 4
A importEntity() 0 16 3
A stop() 0 3 1
1
<?php
2
3
namespace Wikibase\QueryEngine\Importer;
4
5
use Exception;
6
use Iterator;
7
use Wikibase\DataModel\Entity\Entity;
8
use Wikibase\DataModel\Entity\EntityDocument;
9
use Wikibase\QueryEngine\QueryStoreWriter;
10
11
/**
12
 * @since 0.3
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class EntitiesImporter {
17
18
	private $storeWriter;
19
	private $entityIterator;
20
	private $reporter;
21
22
	private $shouldStop = false;
23
24
	/**
25
	 * @param QueryStoreWriter $storeWriter
26
	 * @param Iterator $entityIterator Each value should be of type Entity
27
	 * @param ImportReporter|null $reporter
28
	 */
29
	public function __construct( QueryStoreWriter $storeWriter, Iterator $entityIterator, ImportReporter $reporter = null ) {
30
		$this->storeWriter = $storeWriter;
31
		$this->entityIterator = $entityIterator;
32
		$this->reporter = $reporter === null ? new NullImportReporter() : $reporter;
33
	}
34
35
	public function run() {
36
		$this->reporter->onImportStarted();
37
38
		foreach ( $this->entityIterator as $entity ) {
39
			if ( function_exists( 'pcntl_signal_dispatch' ) ) {
40
				pcntl_signal_dispatch();
41
			}
42
43
			if ( $this->shouldStop ) {
44
				$this->shouldStop = false;
45
				$this->reporter->onImportAborted();
46
				return;
47
			}
48
49
			$this->importEntity( $entity );
50
		}
51
52
		$this->reporter->onImportCompleted();
53
	}
54
55
	private function importEntity( EntityDocument $entity ) {
56
		$this->reporter->onEntityInsertStarted( $entity );
57
		$wasSuccessful = false;
58
59
		try {
60
			$this->storeWriter->updateEntity( $entity );
61
			$wasSuccessful = true;
62
		}
63
		catch ( Exception $ex ) {
64
			$this->reporter->onEntityInsertFailed( $entity, $ex );
65
		}
66
67
		if ( $wasSuccessful ) {
68
			$this->reporter->onEntityInsertSucceeded( $entity );
69
		}
70
	}
71
72
	public function stop() {
73
		$this->shouldStop = true;
74
	}
75
76
}
77