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