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

ShortUrlPropertyAnnotator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
ccs 0
cts 29
cp 0
wmc 9
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAnnotatorFor() 0 3 1
A addAnnotation() 0 17 4
A getUrlPrefix() 0 10 2
A hasShortUrlUtils() 0 3 1
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDIUri as DIUri;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
use RuntimeException;
12
13
/**
14
 * @private
15
 * @ingroup SESP
16
 *
17
 * @license GNU GPL v2+
18
 * @since 2.0
19
 *
20
 * @author mwjames
21
 * @author rotsee
22
 */
23
class ShortUrlPropertyAnnotator implements PropertyAnnotator {
24
25
	/**
26
	 * @var AppFactory
27
	 */
28
	private $appFactory;
29
30
	/**
31
	 * @since 2.0
32
	 *
33
	 * @param AppFactory $appFactory
34
	 */
35
	public function __construct( AppFactory $appFactory ) {
36
		$this->appFactory = $appFactory;
37
	}
38
39
	/**
40
	 * @since 2.0
41
	 *
42
	 * {@inheritDoc}
43
	 */
44
	public function isAnnotatorFor( DIProperty $property ) {
45
		return $property->getKey() === '___SHORTURL' ;
46
	}
47
48
	/**
49
	 * @since 2.0
50
	 *
51
	 * {@inheritDoc}
52
	 */
53
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
54
55
		if ( !$this->hasShortUrlUtils() ) {
56
			throw new RuntimeException( 'Class ShortUrlUtils is not available' );
57
		}
58
59
		$dataItem = null;
60
		$title = $semanticData->getSubject()->getTitle();
61
62
		if ( \ShortUrlUtils::needsShortUrl( $title ) ) {
63
			$dataItem = new DIUri( 'http', $this->getUrlPrefix() . \ShortUrlUtils::encodeTitle( $title ), '', '' );
64
		}
65
66
		if ( $dataItem instanceof DataItem ) {
67
			$semanticData->addPropertyObjectValue( $property, $dataItem );
68
		}
69
	}
70
71
	protected function getUrlPrefix() {
72
73
		$shortUrlPrefix = $this->appFactory->getOption( 'wgShortUrlPrefix', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
		if ( $shortUrlPrefix === '' ) {
76
			return SpecialPage::getTitleFor( 'ShortUrl' )->getFullUrl() . '/';
77
		}
78
79
		return $shortUrlPrefix;
80
	}
81
82
	protected function hasShortUrlUtils() {
83
		return class_exists( 'ShortUrlUtils' );
84
	}
85
86
}
87