Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.44% covered (success)
94.44%
17 / 18
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventBusRCFeedFormatter
94.44% covered (success)
94.44%
17 / 18
66.67% covered (warning)
66.67%
2 / 3
7.01
0.00% covered (danger)
0.00%
0 / 1
 removeNulls
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 getLine
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 formatArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\EventBus\Adapters\RCFeed;
4
5use MachineReadableRCFeedFormatter;
6use MediaWiki\Extension\EventBus\EventBus;
7use MediaWiki\MediaWikiServices;
8use RecentChange;
9
10/**
11 * Augments the RecentChange object for use with the EventBus service, and then
12 * formats it into a JSON string.
13 *
14 * @extends MachineReadableRCFeedFormatter
15 */
16class EventBusRCFeedFormatter extends MachineReadableRCFeedFormatter {
17    /**
18     * Stream name to which this event belongs.
19     * @var string
20     */
21    public const STREAM = 'mediawiki.recentchange';
22
23    /**
24     * Removes properties which values are 'null' from the event.
25     * Will modify the original event passed in
26     *
27     * @param array $event the event to modify.
28     * @return array
29     */
30    private static function removeNulls( $event ) {
31        if ( !is_array( $event ) ) {
32            return $event;
33        }
34        foreach ( $event as $key => $value ) {
35            if ( $value === null ) {
36                unset( $event[$key] );
37            } elseif ( is_array( $value ) ) {
38                $event[$key] = self::removeNulls( $value );
39            }
40        }
41
42        return $event;
43    }
44
45    /**
46     * Calls MachineReadableRCFeedFormatter's getLine(), augments
47     * the returned object so that it is suitable for POSTing to
48     * the EventBus service, and then returns those events
49     * serialized (AKA formatted) as a JSON string by calling
50     * EventBus serializeEvents().
51     *
52     * @inheritDoc
53     * @suppress PhanTypeMismatchArgument
54     */
55    public function getLine( array $feed, RecentChange $rc, $actionComment ) {
56        $attrs = parent::getLine( $feed, $rc, $actionComment );
57
58        $eventFactory = EventBus::getInstanceForStream( self::STREAM )->getFactory();
59        $eventFactory->setCommentFormatter( MediaWikiServices::getInstance()->getCommentFormatter() );
60        $event = $eventFactory->createRecentChangeEvent(
61            self::STREAM,
62            $rc->getTitle(),
63            $attrs
64        );
65
66        return EventBus::serializeEvents( [ self::removeNulls( $event ) ] );
67    }
68
69    /**
70     * Here, formatArray is implemented to just return the same
71     * event it is given.  Since parent::getLine() calls this,
72     * and we need to augment the $event after it is returned from
73     * parent::getLine, we don't actually want to serialize (AKA format)
74     * the event at this time.  This class' getLine function will
75     * serialize/format the event after it has augmented the
76     * event returned here.
77     *
78     * @inheritDoc
79     * @suppress PhanTypeMismatchReturnProbablyReal
80     */
81    protected function formatArray( array $event ) {
82        return $event;
83    }
84}