Completed
Push — master ( 1811ff...1fe2bc )
by mw
08:02
created

ApprovedByPropertyAnnotator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
	public function __construct( AppFactory $appFactory ) {
42
		$this->appFactory = $appFactory;
43
	}
44
45
	/**
46
	 * @since 2.0
47
	 *
48
	 * @param User $approvedBy
49
	 */
50
	public function setApprovedBy( $approvedBy ) {
51
		$this->approvedBy = $approvedBy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $approvedBy 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...
52
	}
53
54
	/**
55
	 * {@inheritDoc}
56
	 */
57
	public function isAnnotatorFor( DIProperty $property ) {
58
		return $property->getKey() === self::PROP_ID;
59
	}
60
61
	public function getDataItem() {
62
		if ( $this->approvedBy instanceof User ) {
0 ignored issues
show
Bug introduced by
The class User 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...
63
			$userPage = $this->approvedBy->getUserPage();
64
65
			if ( $userPage instanceof Title ) {
0 ignored issues
show
Bug introduced by
The class Title 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...
66
				return DIWikiPage::newFromTitle( $userPage );
67
			}
68
		}
69
	}
70
71
	/**
72
	 * {@inheritDoc}
73
	 */
74
	public function addAnnotation(
75
		DIProperty $property, SemanticData $semanticData
76
	) {
77
		if ( $this->approvedBy === null && class_exists( 'ApprovedRevs' ) ) {
78
			$logReader = $this->appFactory->newDatabaseLogReader(
79
				$semanticData->getSubject()->getTitle(), 'approval'
80
			);
81
			$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...
82
		}
83
84
		$dataItem = $this->getDataItem();
85
		if ( $dataItem ) {
86
			$semanticData->addPropertyObjectValue( $property, $dataItem );
87
		} else {
88
			$semanticData->removeProperty( $property );
89
		}
90
	}
91
}
92