Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CdnPurgeEventRelayer
95.83% covered (success)
95.83%
23 / 24
50.00% covered (danger)
50.00%
1 / 2
3
0.00% covered (danger)
0.00%
0 / 1
 __construct
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
 doNotify
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\EventBus\Adapters\EventRelayer;
4
5use InvalidArgumentException;
6use MediaWiki\Extension\EventBus\EventBus;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Utils\MWTimestamp;
9use Wikimedia\Assert\Assert;
10use Wikimedia\EventRelayer\EventRelayer;
11
12/**
13 * @since 1.35
14 */
15class CdnPurgeEventRelayer extends EventRelayer {
16
17    /** @var string */
18    private $purgeStream;
19
20    /** @var EventBus */
21    private $eventBus;
22
23    /**
24     * @param array $params
25     *  - string 'stream' - the name of the stream the CDN purge events
26     *  will be produced to. Required.
27     * @throws InvalidArgumentException if $params are misconfigured
28     */
29    public function __construct( array $params ) {
30        parent::__construct( $params );
31        if ( !isset( $params['stream'] ) ) {
32            throw new InvalidArgumentException( 'purge_stream must be configured' );
33        }
34
35        $this->purgeStream = $params['stream'];
36        $this->eventBus = MediaWikiServices::getInstance()
37            ->getService( 'EventBus.EventBusFactory' )
38            ->getInstanceForStream( $this->purgeStream );
39    }
40
41    /**
42     * @param string $channel
43     * @param array $events
44     * @return bool
45     */
46    protected function doNotify( $channel, array $events ) {
47        Assert::precondition(
48            $channel === 'cdn-url-purges',
49            "Invalid CdnPurgeEventRelayer configuration. Called on $channel"
50        );
51        return $this->eventBus->send(
52            array_map( function ( $event ) {
53                return $this->eventBus->getFactory()->createEvent(
54                    $event['url'],
55                    '/resource_change/1.0.0',
56                    $this->purgeStream,
57                    [ 'tags' => [ 'mediawiki' ] ],
58                    null,
59                    MWTimestamp::convert( TS_ISO_8601, $event['timestamp'] )
60                );
61            }, $events ),
62            EventBus::TYPE_PURGE
63        ) === true;
64    }
65}