GetEntitiesInterpreter::getEntityPagesFromResult()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Queryr\Replicator\EntitySource\Api;
4
5
use Queryr\Replicator\Model\EntityPage;
6
7
/**
8
 * Interpreter for output created by the Wikibase wbgetentities web API module.
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class GetEntitiesInterpreter {
14
15
	/**
16
	 * @param string $resultJson
17
	 *
18
	 * @return EntityPage[]
19
	 */
20 6
	public function getEntityPagesFromResult( $resultJson ): array {
21 6
		$resultData = json_decode( $resultJson, true );
22
23 6
		if ( !is_array( $resultData ) ) {
24 1
			return [];
25
		}
26
27 5
		if ( array_key_exists( 'entities', $resultData ) ) {
28 4
			return $this->constructEntityPages( $resultData['entities'] );
29
		}
30
31 1
		return [];
32
	}
33
34 4
	private function constructEntityPages( array $entityPagesData ) {
35 4
		$entityPages = [];
36
37 4
		foreach ( $entityPagesData as $key => $entityPageData ) {
38 4
			$isActualEntity = array_key_exists( 'id', $entityPageData )
39 4
				&& $entityPageData['id'] === $key
40 4
				&& !array_key_exists( 'missing', $entityPageData );
41
42 4
			if ( $isActualEntity ) {
43 4
				$entityPages[] = $this->constructEntityPage( $entityPageData );
44
			}
45
		}
46
47 4
		return $entityPages;
48
	}
49
50 3
	private function constructEntityPage( array $entityPageData ) {
51 3
		return new EntityPage(
52 3
			json_encode( $entityPageData ),
53 3
			$entityPageData['title'],
54 3
			$entityPageData['ns'],
55 3
			$entityPageData['lastrevid'],
56 3
			$entityPageData['modified']
57
		);
58
	}
59
60
}
61