Completed
Pull Request — master (#16)
by mw
09:58
created

EchoNotificationsManager::createEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 2
eloc 5
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace SMW\Notifications;
4
5
use EchoEvent;
6
use EchoAttributeManager;
7
use SMW\Notifications\ChangeNotification\ChangeNotificationFilter;
8
use SMW\Notifications\ChangeNotification\ChangeNotificationFormatter;
9
use SMW\Notifications\ChangeNotification\ChangeNotificationPresentationModel;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 1.0
14
 *
15
 * @author mwjames
16
 */
17
class EchoNotificationsManager {
18
19
	/**
20
	 * @see Echo::BeforeCreateEchoEvent
21
	 *
22
	 * @since 1.0
23
	 *
24
	 * @param array &$notifications
25
	 * @param array &$notificationCategories
26
	 * @param array &$icons
27
	 */
28 2
	public function addNotificationsDefinitions( array &$notifications, array &$notificationCategories, array &$icons ) {
29
30 1
		$valueChangeNotificationType = ChangeNotificationFilter::VALUE_CHANGE;
31 1
		$specificationChangeNotificationType = ChangeNotificationFilter::SPECIFICATION_CHANGE;
32
33 1
		$notificationCategories[$valueChangeNotificationType] = array(
34 1
			'priority' => 5,
35 1
			'tooltip' => 'echo-pref-tooltip-smw-value-change',
36
		);
37
38 1
		$notificationCategories[$specificationChangeNotificationType] = array(
39 1
			'priority' => 5,
40 1
			'tooltip' => 'echo-pref-tooltip-smw-specification-change',
41
		);
42
43 1
		$notifications[$valueChangeNotificationType] = array(
44 1
			EchoAttributeManager::ATTR_LOCATORS => [
45
				'\SMW\Notifications\ChangeNotification\UserLocator::doLocateEventSubscribers'
46 1
			],
47 1
			'category' => 'smw-value-change',
48 1
			'group' => 'neutral',
49 1
			'section' => 'alert',
50 1
			'presentation-model' => ChangeNotificationPresentationModel::class,
51 1
			'formatter-class' => ChangeNotificationFormatter::class,
52 1
			'title-message' => "notification-{$valueChangeNotificationType}",
53
			'title-params' => array(
54 1
				'agent', 'difflink', 'title'
55 1
			),
56 1
			'flyout-message' => "notification-{$valueChangeNotificationType}-flyout",
57
			'flyout-params' => array(
58 1
				'agent', 'difflink', 'title'
59 1
			),
60 1
			'payload' => array( 'extra' ),
61
			'bundle' => array(
62 1
				'web' => true,
63
				'email' => false
64 1
			),
65 1
			'email-subject-message' => "notification-{$valueChangeNotificationType}-email-subject",
66
			'email-subject-params' => array(
67 1
				'user',
68
				'agent'
69 1
			),
70 1
			'email-body-batch-message' => "notification-{$valueChangeNotificationType}-email-batch-body",
71
			'email-body-batch-params' => array(
72 1
				'title',
73 1
				'agent',
74
				'item'
75 1
			),
76 1
			'icon' => $valueChangeNotificationType,
77
		);
78
79 1
		$notifications[$specificationChangeNotificationType] = array(
80 1
			EchoAttributeManager::ATTR_LOCATORS => [
81
				'\SMW\Notifications\ChangeNotification\UserLocator::doLocateEventSubscribers'
82 1
			],
83 1
			'category' => 'smw-specification-change',
84 1
			'group' => 'neutral',
85 1
			'section' => 'alert',
86 1
			'presentation-model' => ChangeNotificationPresentationModel::class,
87 1
			'formatter-class' => ChangeNotificationFormatter::class,
88 1
			'title-message' => "notification-{$valueChangeNotificationType}",
89
			'title-params' => array(
90 1
				'agent', 'difflink', 'title'
91 2
			),
92 1
			'flyout-message' => "notification-{$valueChangeNotificationType}-flyout",
93
			'flyout-params' => array(
94 1
				'agent', 'difflink', 'title'
95 1
			),
96 1
			'payload' => array( 'extra' ),
97
			'bundle' => array(
98 1
				'web' => true,
99
				'email' => false
100 1
			),
101 1
			'email-subject-message' => "notification-{$valueChangeNotificationType}-email-subject",
102
			'email-subject-params' => array(
103 1
				'user',
104
				'agent'
105 1
			),
106 1
			'email-body-batch-message' => "notification-{$valueChangeNotificationType}-email-batch-body",
107
			'email-body-batch-params' => array(
108 1
				'title',
109 1
				'agent',
110
				'item'
111 1
			)
112 1
		);
113
114 1
		$icons[$valueChangeNotificationType] = array(
115
			'path' => "SemanticNotifications/res/smw-entity-change-yellow.png"
116 1
		);
117
118
		// Resolved in ChangeNotificationPresentationModel::getIconType
119 1
		$icons[$specificationChangeNotificationType . '-property'] = array(
120
			'path' => "SemanticNotifications/res/smw-entity-change-blue.png"
121 1
		);
122
123 1
		$icons[$specificationChangeNotificationType . '-category'] = array(
124
			'path' => "SemanticNotifications/res/smw-entity-change-green.png"
125 1
		);
126 1
	}
127
128
	/**
129
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Conventions
130
	 *
131
	 * @since 1.0
132
	 *
133
	 * @param array &$defaultOptions
134
	 *
135
	 */
136 1
	public function addDefaultOptions( array &$defaultOptions ) {
137 1
		$defaultOptions['echo-subscriptions-web-' . ChangeNotificationFilter::VALUE_CHANGE] = true;
138 1
		return true;
139
	}
140
141
	/**
142
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Bundled_notification
143
	 *
144
	 * @since 1.0
145
	 *
146
	 * @param EchoEvent $event
147
	 * @param string &$bundleString
148
	 */
149 1
	public function getNotificationsBundle( EchoEvent $event, &$bundleString ) {
150
151 1
		$extra = $event->getExtra();
152
153
		if (
154 1
			$event->getType() === ChangeNotificationFilter::VALUE_CHANGE ||
155 1
			$event->getType() === ChangeNotificationFilter::SPECIFICATION_CHANGE ) {
156 1
			$bundleString = $extra['subject']->getHash() . $extra['revid'];
157 1
		}
158 1
	}
159
160
	/**
161
	 * @since 1.0
162
	 *
163
	 * @param array $event
164
	 *
165
	 * @return EchoEvent
166
	 */
167 1
	public function createEvent( array $event ) {
168
169 1
		if ( $event === array() ) {
170
			return;
171
		}
172
173 1
		wfDebugLog( 'smw', 'EchoEvent triggered' );
174
175 1
		return EchoEvent::create( $event );
176
	}
177
178
	/**
179
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Create_the_notification_via_the_Echo_extension
180
	 *
181
	 * @since 1.0
182
	 *
183
	 * @param EchoEvent $event
184
	 * @param array &$users
185
	 */
186 1
	public function getDefaultRecipientsByType( EchoEvent $event, &$users ) {
187
188 1
		$extra = $event->getExtra();
189
190 1
		if ( $event->getType() === ChangeNotificationFilter::VALUE_CHANGE ) {
191 1
			if ( isset( $extra['recipients'] ) ) {
192
				foreach ( $extra['recipients'] as $key => $recipient ) {
193
					$users[] = \User::newFromName( $recipient, false );
194
				}
195
			}
196 1
		}
197 1
	}
198
199
}
200