Completed
Push — master ( a5b09f...24d151 )
by Jeroen De
04:21 queued 03:05
created

GetPreferences::handleGroup()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.8579

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 16
cts 28
cp 0.5714
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 10
nop 1
crap 10.8579
1
<?php
2
3
namespace SWL\MediaWiki\Hooks;
4
5
use SWLGroup;
6
use User;
7
use Language;
8
use MWNamespace;
9
10
/**
11
 * Adds the preferences relevant to Semantic Watchlist
12
 * https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
13
 *
14
 * @ingroup SWL
15
 *
16
 * @licence GNU GPL v2+
17
 * @since 1.0
18
 *
19
 * @author Jeroen De Dauw < [email protected] >
20
 * @author mwjames
21
 */
22
class GetPreferences {
23
24
	protected $user;
25
	protected $language;
26
	protected $preferences;
27
	protected $configuration;
28
29
	/**
30
	 * @since 1.0
31
	 *
32
	 * @param User $user
33
	 * @param Language $language
34
	 * @param array &$preferences
35
	 */
36 4
	public function __construct( User $user, Language $language, array &$preferences ) {
37 4
		$this->user = $user;
38 4
		$this->language = $language;
39 4
		$this->preferences =& $preferences;
40 4
	}
41
42
	/**
43
	 * @since 1.0
44
	 *
45
	 * @param array $configuration
46
	 */
47 3
	public function setConfiguration( array $configuration ) {
48 3
		$this->configuration = $configuration;
49 3
	}
50
51
	/**
52
	 * @since 1.0
53
	 *
54
	 * @return boolean
55
	 */
56 3
	public function execute() {
57
58 3
		$groups = $this->getAllSwlGroups();
59
60 3
		if ( $this->configuration['egSWLEnableEmailNotify'] ) {
61 1
			$this->preferences['swl_email'] = $this->addEmailNotificationPreference();
62
		}
63
64 3
		if ( $this->configuration['egSWLEnableTopLink'] ) {
65 1
			$this->preferences['swl_watchlisttoplink'] = $this->addTopLinkPreference();
66
		}
67
68 3
		foreach ( $groups as /* SWLGroup */ $group ) {
69 1
			$this->handleGroup( $group );
70
		}
71
72 3
		return true;
73
	}
74
75 1
	private function handleGroup( SWLGroup $group ) {
76 1
		$properties = $group->getProperties();
77
78 1
		if ( empty( $properties ) ) {
79
			return;
80
		}
81
82
		switch ( true ) {
83 1
			case count( $group->getCategories() ) > 0 :
84 1
				$type = 'category';
85 1
				$name = $group->getCategories();
86 1
				$name = $name[0];
87 1
				break;
88
			case count( $group->getNamespaces() ) > 0 :
89
				$type = 'namespace';
90
				$name = $group->getNamespaces();
91
				$name = $name[0] == 0 ? wfMessage( 'main' )->text() : MWNamespace::getCanonicalName( $name[0] );
92
				break;
93
			case count( $group->getConcepts() ) > 0 :
94
				$type = 'concept';
95
				$name = $group->getConcepts();
96
				$name = $name[0];
97
				break;
98
			default:
99
				return;
100
		}
101
102 1
		foreach ( $properties as &$property ) {
103 1
			$property = "''$property''";
104
		}
105
106 1
		$this->preferences['swl_watchgroup_' . $group->getId()] = $this->addGoupPreference(
107 1
			$type,
108 1
			$group->getName(),
109 1
			$name,
110 1
			$properties
111
		);
112 1
	}
113
114
	protected function getAllSwlGroups() {
115
		return \SWLGroups::getAll();
116
	}
117
118 1
	protected function addEmailNotificationPreference() {
119
		return array(
120 1
			'type' => 'toggle',
121
			'label-message' => 'swl-prefs-emailnofity',
122
			'section' => 'swl/swlglobal',
123
		);
124
	}
125
126 1
	protected function addTopLinkPreference() {
127
		return array(
128 1
			'type' => 'toggle',
129
			'label-message' => 'swl-prefs-watchlisttoplink',
130
			'section' => 'swl/swlglobal',
131
		);
132
	}
133
134
	/**
135
	 * @search swl-prefs-category-label, swl-prefs-namespace-label,
136
	 * swl-prefs-concept-label
137
	 */
138 1
	protected function addGoupPreference( $type, $group, $name, $properties ) {
139
		return  array(
140 1
			'type' => 'toggle',
141 1
			'label' => wfMessage(
142 1
				"swl-prefs-$type-label",
143 1
				$group,
144 1
				count( $properties ),
145 1
				$this->language->listToText( $properties ),
146 1
				$name
147 1
			)->inLanguage( $this->language )->text(),
148 1
			'section' => 'swl/swlgroup',
149
		);
150
	}
151
152
}
153