Completed
Push — master ( 21291c...2ce44d )
by Jeroen De
06:22
created

EventExtractor::isImageValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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 RepoGroup;
11
use SMW\DIWikiPage;
12
use SMWDITime;
13
14
class EventExtractor {
15
16
	private $parameters;
17
18 7
	public function __construct( array $parameters ) {
19 7
		$this->parameters = $parameters;
20
	}
21 7
22 5
	/**
23
	 * @param SubjectCollection $pages
24 5
	 * @return Event[]
25 4
	 */
26
	public function extractEvents( SubjectCollection $pages ): array {
27
		$events = [];
28
29 7
		foreach ( $pages->getSubjects() as $subject ) {
30
			[ $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...
31
32 5
			if ( $startDate !== null ) {
33 5
				$event = new Event( $subject, $startDate, $endDate );
34 5
35
				foreach ( $subject->getPropertyValueCollections() as $propertyValues ) {
36 5
					if ( $propertyValues->getPrintRequest()->getText( null ) === $this->parameters['image property'] ) {
37 4
						foreach ( $propertyValues->getDataItems() as $dataItem ) {
38
							if ( $this->isImageValue( $dataItem ) ) {
39 4
								$event->setImageUrl( $this->getUrlForFileTitle( $dataItem->getTitle() ) );
40 4
							}
41 4
						}
42
					}
43 1
				}
44 1
45 1
				$events[] = $event;
46
			}
47
		}
48
49
		return $events;
50 5
	}
51
52
	private function isImageValue( \SMWDataItem $dataItem ) {
53
		return $dataItem instanceof DIWikiPage
0 ignored issues
show
Bug introduced by
The class SMW\DIWikiPage does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
			&& $dataItem->getTitle() instanceof \Title
0 ignored issues
show
Bug introduced by
The class Title does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
55
			&& $dataItem->getTitle()->getNamespace() === NS_FILE
56 5
			&& $dataItem->getTitle()->exists();
57 5
	}
58 5
59 5
	public function getUrlForFileTitle( \Title $existingTitle ): string {
60 4
		return RepoGroup::singleton()->findFile( $existingTitle )->getURL();
61 4
	}
62 5
63
	private function getDates( Subject $subject ): array {
64
		$startDate = null;
65
		$endDate = null;
66
67
		foreach ( $this->getPropertyValueCollectionsWithDates( $subject ) as $propertyValues ) {
68
			$dataItem = $propertyValues->getDataItems()[0];
69
70
			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...
71
				if ( $startDate === null ) {
72
					$startDate = $dataItem;
73
				}
74
				else if ( $endDate === null ) {
75
					$endDate = $dataItem;
76
					break;
77
				}
78
			}
79
		}
80
81
		return [ $startDate, $endDate ];
82
	}
83
84
	/**
85
	 * @return PropertyValueCollection[]
86
	 */
87
	private function getPropertyValueCollectionsWithDates( Subject $subject ) {
88
		return array_filter(
89
			$subject->getPropertyValueCollections()->toArray(),
90
			function( PropertyValueCollection $pvc ) {
91
				return $pvc->getPrintRequest()->getTypeID() === '_dat'
92
					&& $pvc->getDataItems() !== [];
93
			}
94
		);
95
	}
96
97
}
98