Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
NotificationHelper
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 createNotification
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace MediaWiki\Extension\ImageSuggestions;
4
5use MediaWiki\Extension\Notifications\Model\Event;
6use MediaWiki\Title\Title;
7use MediaWiki\User\UserIdentity;
8use Psr\Log\LoggerInterface;
9
10/**
11 * Wrapper for Event::create() with logging, to facilitate testing of Notifier.php
12 */
13class NotificationHelper {
14    public function createNotification(
15        UserIdentity $user,
16        Title $title,
17        string $mediaUrl,
18        ?string $sectionHeading = null,
19        ?LoggerInterface $logger = null,
20        bool $noop = false
21    ): ?Event {
22        if ( $logger ) {
23            $logger->info(
24                "Notification: " .
25                "user: {userName} (id: {userId}), " .
26                "title: {titleText} (id: {titleId}), " .
27                "media-url: {mediaUrl}, " .
28                "section-heading: {sectionHeading} ",
29                [
30                    'userName' => $user->getName(),
31                    'userId' => $user->getId(),
32                    'titleText' => $title->getText(),
33                    'titleId' => $title->getId(),
34                    'mediaUrl' => $mediaUrl,
35                    'sectionHeading' => $sectionHeading ?? 'none',
36                ]
37            );
38        }
39
40        if ( $noop ) {
41            return null;
42        }
43
44        // @codeCoverageIgnoreStart
45        return Event::create( [
46            'type' => Hooks::EVENT_NAME,
47            'title' => $title,
48            'agent' => $user,
49            'extra' => [
50                'media-url' => $mediaUrl,
51                'section-heading' => $sectionHeading,
52            ],
53        ] );
54        // @codeCoverageIgnoreEnd
55    }
56}