BatchingEntityPageFetcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 62
ccs 20
cts 21
cp 0.9524
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addPagesToFetch() 0 3 1
A fetchNext() 0 19 5
A rewind() 0 3 1
1
<?php
2
3
namespace Queryr\Replicator\EntitySource;
4
5
use BatchingIterator\BatchingFetcher;
6
use Queryr\Replicator\Model\EntityPage;
7
8
/**
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class BatchingEntityPageFetcher implements BatchingFetcher {
13
14
	/**
15
	 * @var EntityPageBatchFetcher
16
	 */
17
	private $batchFetcher;
18
19
	/**
20
	 * @var string[]
21
	 */
22
	private $pagesToFetch;
23
24
	/**
25
	 * @var int
26
	 */
27
	private $position;
28
29 16
	public function __construct( EntityPageBatchFetcher $batchFetcher, array $pagesToFetch ) {
30 16
		$this->batchFetcher = $batchFetcher;
31 16
		$this->pagesToFetch = array_unique( $pagesToFetch );
32 16
		$this->rewind();
33 16
	}
34
35 4
	public function addPagesToFetch( array $pagesToFetch ) {
36 4
		$this->pagesToFetch = array_unique( array_merge( $this->pagesToFetch, $pagesToFetch ) );
0 ignored issues
show
Documentation Bug introduced by
It seems like array_unique(array_merge...oFetch, $pagesToFetch)) of type array is incompatible with the declared type array<integer,string> of property $pagesToFetch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37 4
	}
38
39
	/**
40
	 * @see BatchingFetcher::fetchNext
41
	 *
42
	 * @param int $maxFetchCount
43
	 *
44
	 * @return EntityPage[]
45
	 */
46 16
	public function fetchNext( $maxFetchCount ) {
47 16
		if ( !is_int( $maxFetchCount ) || $maxFetchCount < 1 ) {
48
			throw new \InvalidArgumentException( '$maxFetchCount needs to be int > 0' );
49
		}
50
51
		do {
52 16
			$idsInBatch = array_slice( $this->pagesToFetch, $this->position, $maxFetchCount );
53 16
			$this->position = min( $this->position + $maxFetchCount, count( $this->pagesToFetch ) );
54
55
56 16
			if ( empty( $idsInBatch ) ) {
57 10
				return [];
58
			}
59
60 14
			 $foundPages = $this->batchFetcher->fetchEntityPages( $idsInBatch );
61 14
		} while( $foundPages === [] );
62
63 13
		return $foundPages;
64
	}
65
66
	/**
67
	 * @see BatchingFetcher::rewind
68
	 */
69 16
	public function rewind() {
70 16
		$this->position = 0;
71 16
	}
72
73
}
74