Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
EchoHelper | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
4.00 | |
0.00% |
0 / 1 |
send | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
4.00 |
1 | <?php |
2 | /** |
3 | * TheWikipediaLibrary Echo notification helper |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @license MIT |
8 | */ |
9 | |
10 | namespace MediaWiki\Extension\TheWikipediaLibrary; |
11 | |
12 | use EchoEvent; |
13 | use EchoNotification; |
14 | use EchoNotificationMapper; |
15 | use MediaWiki\Logger\LoggerFactory; |
16 | use MediaWiki\Title\Title; |
17 | use MediaWiki\User\UserIdentity; |
18 | |
19 | class EchoHelper { |
20 | |
21 | /** |
22 | * Notify the user if they haven't already been notified on this wiki |
23 | * |
24 | * @param UserIdentity $user |
25 | * @param Title $title |
26 | * @return bool |
27 | */ |
28 | public static function send( UserIdentity $user, Title $title ) { |
29 | $notificationMapper = new EchoNotificationMapper(); |
30 | $notifications = $notificationMapper->fetchByUser( $user, 1, null, [ 'twl-eligible' ] ); |
31 | $type = 'twl-eligible'; |
32 | /** @var EchoNotification $notification */ |
33 | foreach ( $notifications as $notification ) { |
34 | if ( $notification->getEvent()->getType() === $type ) { |
35 | LoggerFactory::getInstance( 'TheWikipediaLibrary' )->debug( |
36 | '{user} (id: {id}) has already been notified about The Wikipedia Library', |
37 | [ |
38 | 'user' => $user->getName(), |
39 | 'id' => $user->getId(), |
40 | ] ); |
41 | // Since we found a local notification return true |
42 | return true; |
43 | } |
44 | } |
45 | if ( EchoEvent::create( [ |
46 | 'type' => $type, |
47 | 'title' => $title, |
48 | 'agent' => $user, |
49 | ] ) |
50 | ) { |
51 | return true; |
52 | } |
53 | |
54 | return false; |
55 | } |
56 | } |