Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoHelper
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
4.00
0.00% covered (danger)
0.00%
0 / 1
 send
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
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
10namespace MediaWiki\Extension\TheWikipediaLibrary;
11
12use EchoEvent;
13use EchoNotification;
14use EchoNotificationMapper;
15use MediaWiki\Logger\LoggerFactory;
16use MediaWiki\Title\Title;
17use MediaWiki\User\UserIdentity;
18
19class 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}