PersonalUrls::isEnabledForUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace SWL\MediaWiki\Hooks;
4
5
use Title;
6
use User;
7
8
/**
9
 * Called after the personal URLs have been set up, before they are shown.
10
 * https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Manual:Hooks/PersonalUrls
11
 *
12
 * @ingroup SWL
13
 *
14
 * @licence GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author mwjames
19
 */
20
class PersonalUrls {
21
22
	protected $personalUrls;
23
	protected $title;
24
	protected $user;
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @param array &$personalUrls
30
	 * @param Title &$title
31
	 * @param User $user
32
	 */
33 4
	public function __construct( array &$personalUrls, Title $title, User $user ) {
34 4
		$this->personalUrls =& $personalUrls;
35 4
		$this->title = $title;
36 4
		$this->user = $user;
37 4
	}
38
39
	/**
40
	 * @since 1.0
41
	 *
42
	 * @param array $configuration
43
	 */
44 3
	public function setConfiguration( array $configuration ) {
45 3
		$this->configuration = $configuration;
0 ignored issues
show
Bug introduced by
The property configuration does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46 3
	}
47
48
	/**
49
	 * @since 1.0
50
	 *
51
	 * @return boolean
52
	 */
53 3
	public function execute() {
54 3
		return $this->isEnabledForTopLink() && $this->isEnabledForUser() ? $this->addSwlTopLinkUrl(): true;
55
	}
56
57 3
	protected function isEnabledForTopLink() {
58 3
		return isset( $this->configuration['egSWLEnableTopLink'] ) && $this->configuration['egSWLEnableTopLink'];
59
	}
60
61 2
	protected function isEnabledForUser() {
62 2
		return $this->user->isLoggedIn() && $this->user->getOption( 'swl_watchlisttoplink' );
63
	}
64
65 1
	protected function addSwlTopLinkUrl() {
66
67 1
		$url = \SpecialPage::getTitleFor( 'SemanticWatchlist' )->getLinkUrl();
68
69
		$semanticWatchlist = array(
70 1
			'text' => wfMessage( 'prefs-swl' )->inLanguage( $this->title->getPageLanguage() )->text(),
71 1
			'href' => $url,
72 1
			'active' => ( $url == $this->title->getLinkUrl() )
73
		);
74
75 1
		$keys = array_keys( $this->personalUrls );
76
77 1
		array_splice(
78 1
			$this->personalUrls,
79 1
			$this->getWatchListLocation( $keys ),
80 1
			1,
81 1
			array( $this->getWatchListItem( $keys ), $semanticWatchlist )
82
		);
83
84 1
		return true;
85
	}
86
87 1
	protected function getWatchListLocation( $keys ) {
88 1
		return array_search( 'watchlist', $keys );
89
	}
90
91 1
	protected function getWatchListItem( $keys ) {
92 1
		return $this->personalUrls[ $keys [ $this->getWatchListLocation( $keys ) ] ];
93
	}
94
95
}
96