Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
CRAP
93.75% covered (success)
93.75%
15 / 16
EchoHelper
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
4.00
93.75% covered (success)
93.75%
15 / 16
 send
0.00% covered (danger)
0.00%
0 / 1
4.00
93.75% covered (success)
93.75%
15 / 16
<?php
/**
 * TheWikipediaLibrary Echo notification helper
 *
 * @file
 * @ingroup Extensions
 * @license MIT
 */
namespace MediaWiki\Extension\TheWikipediaLibrary;
use EchoEvent;
use EchoNotification;
use EchoNotificationMapper;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\User\UserIdentity;
use Title;
class EchoHelper {
    /**
     * Notify the user if they haven't already been notified on this wiki
     *
     * @param UserIdentity $user
     * @param Title $title
     * @return bool
     */
    public static function send( UserIdentity $user, Title $title ) {
        $notificationMapper = new EchoNotificationMapper();
        $notifications = $notificationMapper->fetchByUser( $user, 1, null, [ 'twl-eligible' ] );
        $type = 'twl-eligible';
        /** @var EchoNotification $notification */
        foreach ( $notifications as $notification ) {
            if ( $notification->getEvent()->getType() === $type ) {
                LoggerFactory::getInstance( 'TheWikipediaLibrary' )->debug(
                    '{user} (id: {id}) has already been notified about The Wikipedia Library',
                    [
                        'user' => $user->getName(),
                        'id' => $user->getId(),
                    ] );
                // Since we found a local notification return true
                return true;
            }
        }
        if ( EchoEvent::create( [
            'type' => $type,
            'title' => $title,
            'agent' => $user,
        ] )
        ) {
            return true;
        }
        return false;
    }
}