Completed
Push — refact-v2 ( 0317e0 )
by mw
04:47
created

PageViewsPropertyAnnotator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
ccs 0
cts 25
cp 0
wmc 8
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAnnotatorFor() 0 3 1
A addAnnotation() 0 13 3
A getPageViewCount() 0 12 3
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDINumber as DINumber;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
12
/**
13
 * @private
14
 * @ingroup SESP
15
 *
16
 * @license GNU GPL v2+
17
 * @since 2.0
18
 *
19
 * @author mwjames
20
 */
21
class PageViewsPropertyAnnotator implements PropertyAnnotator {
22
23
	/**
24
	 * @var AppFactory
25
	 */
26
	private $appFactory;
27
28
	/**
29
	 * @since 2.0
30
	 *
31
	 * @param AppFactory $appFactory
32
	 */
33
	public function __construct( AppFactory $appFactory ) {
34
		$this->appFactory = $appFactory;
35
	}
36
37
	/**
38
	 * @since 2.0
39
	 *
40
	 * {@inheritDoc}
41
	 */
42
	public function isAnnotatorFor( DIProperty $property ) {
43
		return $property->getKey() === '___VIEWS' ;
44
	}
45
46
	/**
47
	 * @since 2.0
48
	 *
49
	 * {@inheritDoc}
50
	 */
51
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
52
53
		if ( $this->appFactory->getOption( 'wgDisableCounters' ) ) {
54
			return null;
55
		}
56
57
		$page = $this->appFactory->newWikiPage( $semanticData->getSubject()->getTitle() );
0 ignored issues
show
Bug introduced by
It seems like $semanticData->getSubject()->getTitle() can be null; however, newWikiPage() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
		$count = $this->getPageViewCount( $page );
59
60
		if ( is_numeric( $count ) ) {
61
			$semanticData->addPropertyObjectValue( $property, new DINumber( $count ) );
62
		}
63
	}
64
65
	private function getPageViewCount( $page ) {
66
67
		if ( class_exists( '\HitCounters\HitCounters' ) ) {
68
			return \HitCounters\HitCounters::getCount( $page->getTitle() );
69
		}
70
71
		if ( method_exists( $page, 'getCount' ) ) {
72
			return $page->getCount();
73
		}
74
75
		return null;
76
	}
77
78
}
79