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