Completed
Push — master ( e2f648...1e5fc8 )
by mw
08:29
created

getDefaultRecipientsByType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.024

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
ccs 6
cts 10
cp 0.6
cc 4
eloc 6
nc 4
nop 2
crap 5.024
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
				'agent'
74 1
			),
75 1
			'icon' => $valueChangeNotificationType,
76
		);
77
78 1
		$notifications[$specificationChangeNotificationType] = array(
79 1
			EchoAttributeManager::ATTR_LOCATORS => [
80
				'\SMW\Notifications\ChangeNotification\UserLocator::doLocateEventSubscribers'
81 1
			],
82 1
			'category' => 'smw-specification-change',
83 1
			'group' => 'neutral',
84 1
			'section' => 'alert',
85 1
			'presentation-model' => ChangeNotificationPresentationModel::class,
86 1
			'formatter-class' => ChangeNotificationFormatter::class,
87 1
			'title-message' => "notification-{$valueChangeNotificationType}",
88
			'title-params' => array(
89 1
				'agent', 'difflink', 'title'
90 1
			),
91 2
			'flyout-message' => "notification-{$valueChangeNotificationType}-flyout",
92
			'flyout-params' => array(
93 1
				'agent', 'difflink', 'title'
94 1
			),
95 1
			'payload' => array( 'extra' ),
96
			'bundle' => array(
97 1
				'web' => true,
98
				'email' => false
99 1
			),
100 1
			'email-subject-message' => "notification-{$valueChangeNotificationType}-email-subject",
101
			'email-subject-params' => array(
102 1
				'user',
103
				'agent'
104 1
			),
105 1
			'email-body-batch-message' => "notification-{$valueChangeNotificationType}-email-batch-body",
106
			'email-body-batch-params' => array(
107 1
				'title',
108 1
				'agent',
109
				'item'
110 1
			)
111 1
		);
112
113 1
		$icons[$valueChangeNotificationType] = array(
114
			'path' => "SemanticNotifications/res/smw-entity-change-yellow.png"
115 1
		);
116
117
		// Resolved in ChangeNotificationPresentationModel::getIconType
118 1
		$icons[$specificationChangeNotificationType . '-property'] = array(
119
			'path' => "SemanticNotifications/res/smw-entity-change-blue.png"
120 1
		);
121
122 1
		$icons[$specificationChangeNotificationType . '-category'] = array(
123
			'path' => "SemanticNotifications/res/smw-entity-change-green.png"
124 1
		);
125 1
	}
126
127
	/**
128
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Conventions
129
	 *
130
	 * @since 1.0
131
	 *
132
	 * @param array &$defaultOptions
133
	 *
134
	 */
135 1
	public function addDefaultOptions( array &$defaultOptions ) {
136 1
		$defaultOptions['echo-subscriptions-web-' . ChangeNotificationFilter::VALUE_CHANGE] = true;
137 1
		return true;
138
	}
139
140
	/**
141
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Bundled_notification
142
	 *
143
	 * @since 1.0
144
	 *
145
	 * @param EchoEvent $event
146
	 * @param string &$bundleString
147
	 */
148 1
	public function getNotificationsBundle( EchoEvent $event, &$bundleString ) {
149
150 1
		$extra = $event->getExtra();
151
152
		if (
153 1
			$event->getType() === ChangeNotificationFilter::VALUE_CHANGE ||
154 1
			$event->getType() === ChangeNotificationFilter::SPECIFICATION_CHANGE ) {
155 1
			$bundleString = $extra['subject']->getHash() . $extra['revid'];
156 1
		}
157 1
	}
158
159
	/**
160
	 * @since 1.0
161
	 *
162
	 * @param array $event
163
	 *
164
	 * @return EchoEvent
165
	 */
166 1
	public function createEvent( array $event ) {
167
168 1
		if ( $event === array() ) {
169
			return;
170
		}
171
172 1
		wfDebugLog( 'smw', 'EchoEvent triggered' );
173
174 1
		return EchoEvent::create( $event );
175
	}
176
177
	/**
178
	 * @see https://www.mediawiki.org/wiki/Notifications/Developer_guide#Create_the_notification_via_the_Echo_extension
179
	 *
180
	 * @since 1.0
181
	 *
182
	 * @param EchoEvent $event
183
	 * @param array &$users
184
	 */
185 1
	public function getDefaultRecipientsByType( EchoEvent $event, &$users ) {
186
187 1
		$extra = $event->getExtra();
188
189 1
		if ( $event->getType() === ChangeNotificationFilter::VALUE_CHANGE ) {
190 1
			if ( isset( $extra['recipients'] ) ) {
191
				foreach ( $extra['recipients'] as $key => $recipient ) {
192
					$users[] = \User::newFromName( $recipient, false );
193
				}
194
			}
195 1
		}
196 1
	}
197
198
}
199