EntityDumpIterator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.11%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 93
ccs 35
cts 38
cp 0.9211
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onError() 0 3 1
A current() 0 3 1
A next() 0 7 1
A getCurrentFromObject() 0 19 4
A reportError() 0 5 2
A key() 0 3 1
A valid() 0 3 1
A rewind() 0 5 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\JsonDumpReader\Iterator;
6
7
use Deserializers\Deserializer;
8
use Deserializers\Exceptions\DeserializationException;
9
use Iterator;
10
use Wikibase\DataModel\Entity\EntityDocument;
11
12
/**
13
 * Package private
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class EntityDumpIterator implements Iterator {
19
20
	/**
21
	 * @var Deserializer
22
	 */
23
	private $deserializer;
24
25
	/**
26
	 * @var Iterator
27
	 */
28
	private $dumpIterator;
29
30
	/**
31
	 * @var callable|null
32
	 */
33
	private $errorReporter = null;
34
35
	/**
36
	 * @var EntityDocument|null
37
	 */
38
	private $current = null;
39
40 9
	public function __construct( Iterator $objectIterator, Deserializer $entityDeserializer ) {
41 9
		$this->dumpIterator = $objectIterator;
42 9
		$this->deserializer = $entityDeserializer;
43 9
	}
44
45
	/**
46
	 * @param callable|null $errorReporter
47
	 */
48 8
	public function onError( callable $errorReporter = null ) {
49 8
		$this->errorReporter = $errorReporter;
50 8
	}
51
52
	/**
53
	 * @return EntityDocument|null
54
	 */
55 5
	public function current() {
56 5
		return $this->current;
57
	}
58
59
	/**
60
	 * @return EntityDocument|null
61
	 */
62 6
	public function next() {
63 6
		$this->dumpIterator->next();
64
65 6
		$this->getCurrentFromObject();
66
67 6
		return $this->current;
68
	}
69
70 9
	private function getCurrentFromObject() {
71 9
		while ( true ) {
72
			try {
73 9
				$jsonData = $this->dumpIterator->current();
74
75 9
				if ( $jsonData === null ) {
76 9
					$this->current = null;
77 9
					return;
78
				}
79
80 7
				$this->current = $this->deserializer->deserialize( $jsonData );
81 6
				return;
82
			}
83 3
			catch ( DeserializationException $ex ) {
84 3
				$this->reportError( $ex->getMessage() );
85 3
				$this->dumpIterator->next();
86
			}
87
		}
88
	}
89
90 3
	private function reportError( $errorMessage ) {
91 3
		if ( $this->errorReporter !== null ) {
92 1
			call_user_func( $this->errorReporter, $errorMessage );
93
		}
94 3
	}
95
96
	public function key() {
97
		return $this->current->getId()->getSerialization();
98
	}
99
100 9
	public function valid() {
101 9
		return $this->current !== null;
102
	}
103
104 9
	public function rewind() {
105 9
		$this->current = null;
106 9
		$this->dumpIterator->rewind();
107 9
		$this->getCurrentFromObject();
108 9
	}
109
110
}
111