Completed
Pull Request — master (#13)
by Jeroen De
03:37
created

ResultSimplifier   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newSubjectCollection() 0 9 2
A newSubject() 0 14 3
A newResultArray() 0 8 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline\ResultFacade;
6
7
use SMW\DIWikiPage;
8
use SMW\Query\PrintRequest;
9
use SMWQueryResult;
10
11
class ResultSimplifier {
12
13 4
	public function newSubjectCollection( SMWQueryResult $result ): SubjectCollection {
14 4
		$subjects = [];
15
16 4
		foreach ( $result->getResults() as $diWikiPage ) {
17 3
			$subjects[] = $this->newSubject( $diWikiPage, $result->getPrintRequests(), $result );
18
		}
19
20 4
		return new SubjectCollection( $subjects );
21
	}
22
23
	/**
24
	 * @param DIWikiPage $resultPage
25
	 * @param PrintRequest[] $printRequests
26
	 * @param SMWQueryResult $result
27
	 * @return Subject
28
	 */
29 3
	private function newSubject( DIWikiPage $resultPage, array $printRequests, SMWQueryResult $result ): Subject {
30 3
		$propertyValueCollections = [];
31
32 3
		foreach ( $printRequests as $printRequest ) {
33 3
			$dataItems = $this->newResultArray( $resultPage, $printRequest, $result )->getContent();
34
35 3
			$propertyValueCollections[] = new PropertyValueCollection(
36 3
				$printRequest,
37 3
				$dataItems === false ? [] : $dataItems
38
			);
39
		}
40
41 3
		return new Subject( $resultPage, $propertyValueCollections );
42
	}
43
44
	/**
45
	 * Compat with SMW 3.0
46
	 * In 3.1+ do: ResultArray::factory( $resultPage, $printRequest, $result )
47
	 */
48 3
	private function newResultArray( DIWikiPage $resultPage, PrintRequest $printRequest, SMWQueryResult $result ): \SMWResultArray {
49 3
		return new \SMWResultArray(
50 3
			$resultPage,
51
			$printRequest,
52 3
			$result->getStore(),
53 3
			method_exists( $result, 'getFieldItemFinder' ) ? $result->getFieldItemFinder() : null
54
		);
55
	}
56
57
}
58