Completed
Push — master ( 21ccfa...6f5178 )
by Mark A.
08:49
created

PropertyAnnotators/ApprovedByPropertyAnnotator.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SESP\AppFactory;
6
use SESP\PropertyAnnotator;
7
use SESP\DatabaseLogReader;
8
use SMW\DIWikiPage;
9
use SMWDataItem as DataItem;
10
use SMW\DIProperty;
11
use SMW\SemanticData;
12
use Title;
13
use User;
14
15
/**
16
 * @private
17
 * @ingroup SESP
18
 *
19
 * @license GNU GPL v2+
20
 */
21
class ApprovedByPropertyAnnotator implements PropertyAnnotator {
22
23
	/**
24
	 * Predefined property ID
25
	 */
26
	const PROP_ID = '___APPROVEDBY';
27
28
	/**
29
	 * @var AppFactory
30
	 */
31
	private $appFactory;
32
33
	/**
34
	 * @var Integer|null
35
	 */
36
	private $approvedBy;
37
38
	/**
39
	 * @param AppFactory $appFactory
40
	 */
41 4
	public function __construct( AppFactory $appFactory ) {
42 4
		$this->appFactory = $appFactory;
43 4
	}
44
45
	/**
46
	 * @since 2.0
47
	 *
48
	 * @param User $approvedBy
49
	 */
50 2
	public function setApprovedBy( $approvedBy ) {
51 2
		$this->approvedBy = $approvedBy;
52 2
	}
53
54
	/**
55
	 * {@inheritDoc}
56
	 */
57 1
	public function isAnnotatorFor( DIProperty $property ) {
58 1
		return $property->getKey() === self::PROP_ID;
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64 2
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
65
66 2
		if ( $this->approvedBy === null && class_exists( 'ApprovedRevs' ) ) {
67
			$logReader = $this->appFactory->newDatabaseLogReader(
68
				$semanticData->getSubject()->getTitle(),
69
				'approval'
70
			);
71
72
			$this->approvedBy = $logReader->getUserForLogEntry();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logReader->getUserForLogEntry() of type object<User> is incompatible with the declared type integer|null of property $approvedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
		}
74
75 2
		$dataItem = $this->getDataItem();
76
77 2
		if ( $dataItem ) {
78 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
79 1
		} else {
80 1
			$semanticData->removeProperty( $property );
81
		}
82 2
	}
83
84 2
	private function getDataItem() {
85 2
		if ( $this->approvedBy instanceof User ) {
86 1
			$userPage = $this->approvedBy->getUserPage();
87
88 1
			if ( $userPage instanceof Title ) {
89 1
				return DIWikiPage::newFromTitle( $userPage );
90
			}
91
		}
92 1
	}
93
94
}
95