Completed
Push — master ( c2ef24...f9d865 )
by mw
8s
created

ChangeNotificationPresentationModel   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 148
ccs 64
cts 69
cp 0.9275
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getIconType() 0 15 4
A canRender() 0 3 1
A callbackForBundleCount() 0 3 1
A getHeaderMessageKey() 0 8 2
B getHeaderMessage() 0 25 3
B getBodyMessage() 0 30 6
A getPrimaryLink() 0 7 1
A getSecondaryLinks() 0 21 2
1
<?php
2
3
namespace SMW\Notifications\ValueChange;
4
5
use EchoEvent;
6
use EchoEventPresentationModel;
7
use Title;
8
use SMW\DIProperty;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class ChangeNotificationPresentationModel extends EchoEventPresentationModel {
17
18
	/**
19
	 * @see EchoEventPresentationModel::getIconType
20
	 */
21 4
	public function getIconType() {
22
23 4
		if ( $this->event->getType() !== ChangeNotifications::SPECIFICATION_CHANGE ) {
24 1
			return $this->event->getType();
25
		}
26
27 3
		$namespace = $this->event->getTitle()->getNamespace();
28
29
		// @see EchoNotificationsManager::addNotificationsDefinitions for the icons
30 3
		if ( $namespace === NS_CATEGORY || $namespace === SMW_NS_CONCEPT ) {
31 2
			return $this->event->getType() . '-category';
32
		}
33
34 1
		return $this->event->getType() . '-property';
35
	}
36
37
	/**
38
	 * @see EchoEventPresentationModel::canRender
39
	 */
40 1
	public function canRender() {
41 1
		return (bool)$this->event->getTitle();
42
	}
43
44
	/**
45
	 * @param EchoEvent
46
	 * @return string
47
	 */
48 2
	public function callbackForBundleCount( EchoEvent $event ) {
49 2
		return $event->getTitle()->getPrefixedText();
50
	}
51
52
	/**
53
	 * @see EchoEventPresentationModel::getHeaderMessageKey
54
	 */
55 2
	public function getHeaderMessageKey() {
56
57 2
		if ( $this->getBundleCount( true, [ $this, 'callbackForBundleCount' ] ) > 1 ) {
58
			return "notification-bundle-header-{$this->type}";
59
		}
60
61 2
		return "notification-header-{$this->type}";
62
	}
63
64
	/**
65
	 * @see EchoEventPresentationModel::getHeaderMessage
66
	 */
67 1
	public function getHeaderMessage() {
68
69 1
		$extra = $this->event->getExtra();
70 1
		$msg = parent::getHeaderMessage();
71
72 1
		$msg->params(
73 1
			$this->getTruncatedTitleText( $this->event->getTitle(), true )
74 1
		);
75
76
		// Plural indicator
77 1
		$msg->params(
78 1
			( isset( $extra['properties'] ) ? count( $extra['properties'] ) : 0 )
79 1
		);
80
81 1
		$count = $this->getNotificationCountForOutput(
82 1
			false, // we need only other pages count
83 1
			[ $this, 'callbackForBundleCount' ]
84 1
		);
85
86 1
		if ( $count > 0 ) {
87
			$msg->numParams( $count );
88
		}
89
90 1
		return $msg;
91
	}
92
93
	/**
94
	 * @see EchoEventPresentationModel::getBodyMessage
95
	 */
96 5
	public function getBodyMessage() {
97
98 5
		$extra = $this->event->getExtra();
99 5
		$labels = array();
100
101 5
		if ( !isset( $extra['properties'] ) || $extra['properties'] === array() ) {
102 3
			return false;
103
		}
104
105 2
		foreach ( $extra['properties'] as $dataItem ) {
106
107 2
			$prefix = '';
108
109 2
			if ( $dataItem->getDBKey() === '' ) {
110 1
				continue;
111
			}
112
113 2
			if ( $dataItem->getNamespace() === NS_CATEGORY ) {
114
				$prefix = $this->language->getNsText( NS_CATEGORY ) . ':';
115
			}
116
117 2
			$labels[] = $prefix . DIProperty::newFromUserLabel( $dataItem->getDBKey() )->getLabel();
118 2
		}
119
120 2
		$msg = wfMessage( "notification-body-{$this->type}" );
121 2
		$msg->params( $this->language->listToText( $labels ) );
122 2
		$msg->params( count( $labels ) );
123
124 2
		return $msg;
125
	}
126
127
	/**
128
	 * @see EchoEventPresentationModel::getPrimaryLink
129
	 */
130 3
	public function getPrimaryLink() {
131 3
		$title = $this->event->getTitle();
132
		return array(
133 3
			'url' => $title->getFullURL(),
134 3
			'label' => $title->getFullText()
135 3
		);
136
	}
137
138
	/**
139
	 * @see EchoEventPresentationModel::getSecondaryLinks
140
	 */
141 3
	public function getSecondaryLinks() {
142
143 3
		$extra = $this->event->getExtra();
0 ignored issues
show
Unused Code introduced by
$extra is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144 3
		$title = $this->event->getTitle();
145
146 3
		$queryParameters['oldid'] = 'prev';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$queryParameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $queryParameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
147
148 3
		if ( $this->event->getExtraParam( 'revid' ) > 0 ) {
149 3
			$queryParameters['diff'] = $this->event->getExtraParam( 'revid' );
150 3
		}
151
152
		$viewChangesLink = array(
153 3
			'url'   => $title->getLocalURL( $queryParameters ),
154 3
			'label' => $this->msg( 'notification-link-text-view-changes', $this->getViewingUserForGender() )->text(),
155 3
			'description' => '',
156 3
			'icon' => 'changes',
157 3
			'prioritized' => true,
158 3
		);
159
160 3
		return array( $this->getAgentLink(), $viewChangesLink );
161
	}
162
163
}
164