Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
EchoHelper
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 send
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
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 MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
13use MediaWiki\Extension\Notifications\Model\Event;
14use MediaWiki\Logger\LoggerFactory;
15use MediaWiki\Title\Title;
16use MediaWiki\User\UserIdentity;
17
18class EchoHelper {
19
20    /**
21     * Notify the user if they haven't already been notified on this wiki
22     *
23     * @param UserIdentity $user
24     * @param Title $title
25     * @return bool
26     */
27    public static function send( UserIdentity $user, Title $title ) {
28        $type = 'twl-eligible';
29        $notificationMapper = new NotificationMapper();
30        $notifications = $notificationMapper->fetchByUser( $user, 1, null, [ $type ] );
31        foreach ( $notifications as $notification ) {
32            if ( $notification->getEvent()->getType() === $type ) {
33                LoggerFactory::getInstance( 'TheWikipediaLibrary' )->debug(
34                    '{user} (id: {id}) has already been notified about The Wikipedia Library',
35                    [
36                        'user' => $user->getName(),
37                        'id' => $user->getId(),
38                    ] );
39                // Since we found a local notification return true
40                return true;
41            }
42        }
43        return (bool)Event::create( [
44            'type' => $type,
45            'title' => $title,
46            'agent' => $user,
47        ] );
48    }
49}