Completed
Push — master ( 2623ee...a82f7c )
by Jeroen De
06:13 queued 04:44
created

EventExtractor::getDates()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2888
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline;
6
7
use ModernTimeline\ResultFacade\PropertyValueCollection;
8
use ModernTimeline\ResultFacade\Subject;
9
use ModernTimeline\ResultFacade\SubjectCollection;
10
use SMWDITime;
11
12
class EventExtractor {
13
14
	/**
15
	 * @param SubjectCollection $pages
16
	 * @return Event[]
17
	 */
18 7
	public function extractEvents( SubjectCollection $pages ): array {
19 7
		$events = [];
20
21 7
		foreach ( $pages->getSubjects() as $subject ) {
22 5
			[ $startDate, $endDate ] = $this->getDates( $subject );
0 ignored issues
show
Bug introduced by
The variable $startDate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $endDate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
23
24 5
			if ( $startDate !== null ) {
25 4
				$events[] = new Event( $subject, $startDate, $endDate );
26
			}
27
		}
28
29 7
		return $events;
30
	}
31
32 5
	private function getDates( Subject $subject ): array {
33 5
		$startDate = null;
34 5
		$endDate = null;
35
36 5
		foreach ( $this->getPropertyValueCollectionsWithDates( $subject ) as $propertyValues ) {
37 4
			$dataItem = $propertyValues->getDataItems()[0];
38
39 4
			if ( $dataItem instanceof SMWDITime ) {
0 ignored issues
show
Bug introduced by
The class SMWDITime does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
40 4
				if ( $startDate === null ) {
41 4
					$startDate = $dataItem;
42
				}
43 1
				else if ( $endDate === null ) {
44 1
					$endDate = $dataItem;
45 1
					break;
46
				}
47
			}
48
		}
49
50 5
		return [ $startDate, $endDate ];
51
	}
52
53
	/**
54
	 * @return PropertyValueCollection[]
55
	 */
56 5
	private function getPropertyValueCollectionsWithDates( Subject $subject ) {
57 5
		return array_filter(
58 5
			$subject->getPropertyValueCollections()->toArray(),
59 5
			function( PropertyValueCollection $pvc ) {
60 4
				return $pvc->getPrintRequest()->getTypeID() === '_dat'
61 4
					&& $pvc->getDataItems() !== [];
62 5
			}
63
		);
64
	}
65
66
}
67