Completed
Pull Request — master (#6)
by Jeroen De
08:53 queued 05:58
created

ObjectDumpIterator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 87
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onError() 0 3 1
A current() 0 3 1
A next() 0 6 1
A key() 0 3 1
A valid() 0 3 1
A rewind() 0 5 1
A getCurrentFromString() 0 20 4
A reportError() 0 5 2
1
<?php
2
3
namespace Wikibase\JsonDumpReader\Iterator;
4
5
use Iterator;
6
7
/**
8
 * Package private
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class ObjectDumpIterator implements Iterator {
14
15
	/**
16
	 * @var callable|null
17
	 */
18
	private $errorReporter = null;
19
20
	/**
21
	 * @var string|null
22
	 */
23
	private $current = null;
24
25
	/**
26
	 * @var Iterator
27
	 */
28
	private $stringIterator;
29
30 4
	public function __construct( Iterator $stringIterator ) {
31 4
		$this->stringIterator = $stringIterator;
32 4
	}
33
34
	/**
35
	 * @param callable|null $errorReporter
36
	 */
37 4
	public function onError( callable $errorReporter = null ) {
38 4
		$this->errorReporter = $errorReporter;
39 4
	}
40
41
	/**
42
	 * @return string|null
43
	 */
44 3
	public function current() {
45 3
		return $this->current;
46
	}
47
48
	/**
49
	 * @return string|null
50
	 */
51 4
	public function next() {
52 4
		$this->stringIterator->next();
53 4
		$this->getCurrentFromString();
54
55 4
		return $this->current;
56
	}
57
58 4
	private function getCurrentFromString() {
59 4
		while ( true ) {
60 4
			$jsonString = $this->stringIterator->current();
61
62 4
			if ( $jsonString === null ) {
63 1
				$this->current = null;
64 1
				return;
65
			}
66
67 4
			$data = json_decode( $jsonString, true );
68 4
			if ( $data === null ) {
69 1
				$this->reportError( json_last_error_msg() );
70 1
				$this->stringIterator->next();
71
			}
72
			else {
73 4
				$this->current = $data;
74 4
				return;
75
			}
76
		}
77
	}
78
79 1
	private function reportError( $errorMessage ) {
80 1
		if ( $this->errorReporter !== null ) {
81 1
			call_user_func( $this->errorReporter, $errorMessage );
82
		}
83 1
	}
84
85 2
	public function key() {
86 2
		return $this->stringIterator->key();
87
	}
88
89 1
	public function valid() {
90 1
		return $this->current !== null;
91
	}
92
93 4
	public function rewind() {
94 4
		$this->current = null;
95 4
		$this->stringIterator->rewind();
96 4
		$this->getCurrentFromString();
97 4
	}
98
99
}
100