PagesImporter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Queryr\Replicator\Importer;
4
5
use Iterator;
6
use Queryr\Replicator\Model\EntityPage;
7
8
/**
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class PagesImporter {
13
14
	private $importer;
15
	private $statsReporter;
16
	private $onAborted;
17
18
	private $shouldStop = false;
19
20 1
	public function __construct( PageImporter $importer, StatsReporter $statsReporter, callable $onAborted = null ) {
21 1
		$this->importer = $importer;
22 1
		$this->statsReporter = $statsReporter;
23 1
		$this->onAborted = $onAborted;
24 1
	}
25
26 1
	public function importPages( Iterator $entityPageIterator ) {
27 1
		$startTime = microtime( true );
28
29 1
		$reporter = new StatsTrackingReporter( $this->importer->getReporter() );
30
31 1
		$this->importer->setReporter( $reporter );
32
33 1
		$this->runImportLoop( $entityPageIterator );
34
35 1
		$stats = $reporter->getStats();
36 1
		$stats->setDuration( microtime( true ) - $startTime );
37 1
		$this->statsReporter->reportStats( $stats );
38 1
	}
39
40 1
	private function runImportLoop( Iterator $entityPageIterator ) {
41 1
		$this->shouldStop = false;
42
43
		/**
44
		 * @var EntityPage $entityPage
45
		 */
46 1
		foreach ( $entityPageIterator as $entityPage ) {
47 1
			$this->importer->import( $entityPage );
48
49 1
			pcntl_signal_dispatch();
50 1
			if ( $this->shouldStop ) {
51
				if ( $this->onAborted !== null ) {
52
					call_user_func( $this->onAborted, $entityPage->getTitle() );
53
				}
54 1
				return;
55
			}
56
		}
57 1
	}
58
59
	public function stop() {
60
		$this->shouldStop = true;
61
	}
62
63
}
64
65