PagesImporter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 52
ccs 22
cts 27
cp 0.8148
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A importPages() 0 13 1
A runImportLoop() 0 18 4
A stop() 0 3 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