InMemoryBatchingFetcher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fetchNext() 0 10 3
A rewind() 0 3 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace BatchingIterator\Fetchers;
6
7
use BatchingIterator\BatchingFetcher;
8
9
/**
10
 * Trivial implementation of BatchingFetcher that uses an in-memory array.
11
 * Values are returned in first in, first out fashion.
12
 * (In other words: an adapter from array to BatchingFetcher)
13
 *
14
 * @since 2.0
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class InMemoryBatchingFetcher implements BatchingFetcher {
20
21
	private $values;
22
23 11
	public function __construct( array $values ) {
24 11
		$this->values = $values;
25 11
	}
26
27
	/**
28
	 * @see BatchingFetcher::fetchNext
29
	 *
30
	 * @param int $maxFetchCount
31
	 *
32
	 * @return mixed[]
33
	 */
34 11
	public function fetchNext( int $maxFetchCount ): array {
35 11
		$values = [];
36
37 11
		while ( !is_null( key( $this->values ) ) && --$maxFetchCount >= 0 ) {
38 6
			$values[] = current( $this->values );
39 6
			next( $this->values );
40
		}
41
42 11
		return $values;
43
	}
44
45
	/**
46
	 * @see BatchingFetcher::rewind
47
	 */
48 3
	public function rewind() {
49 3
		reset( $this->values );
50 3
	}
51
52
}