mediawiki-extensions-AbuseF.../includes/EchoNotifier.php
Matěj Suchánek 352a207c70 Improve code coverage
Bug: T201193
Change-Id: Ie086fd525bec19c63c13f8710a27897229cc33c8
2020-12-19 16:28:34 +01:00

80 lines
1.6 KiB
PHP

<?php
namespace MediaWiki\Extension\AbuseFilter;
use EchoEvent;
use SpecialAbuseFilter;
use Title;
/**
* Helper service for EmergencyWatcher to notify filter maintainers of throttled filters
* @todo DI not possible due to Echo
*/
class EchoNotifier {
public const SERVICE_NAME = 'AbuseFilterEchoNotifier';
public const EVENT_TYPE = 'throttled-filter';
/** @var FilterLookup */
private $filterLookup;
/** @var bool */
private $isEchoLoaded;
/**
* @param FilterLookup $filterLookup
* @param bool $isEchoLoaded
*/
public function __construct(
FilterLookup $filterLookup,
bool $isEchoLoaded
) {
$this->filterLookup = $filterLookup;
$this->isEchoLoaded = $isEchoLoaded;
}
/**
* @param int $filter
* @return Title
*/
private function getTitleForFilter( int $filter ) : Title {
return SpecialAbuseFilter::getTitleForSubpage( (string)$filter );
}
/**
* @param int $filter
* @return int
*/
private function getLastUserIDForFilter( int $filter ) : int {
return $this->filterLookup->getFilter( $filter, false )->getUserID();
}
/**
* @internal
* @param int $filter
* @return array
*/
public function getDataForEvent( int $filter ) : array {
return [
'type' => self::EVENT_TYPE,
'title' => $this->getTitleForFilter( $filter ),
'extra' => [
'user' => $this->getLastUserIDForFilter( $filter ),
],
];
}
/**
* Send notification about a filter being throttled
*
* @param int $filter
* @return EchoEvent|false
*/
public function notifyForFilter( int $filter ) {
if ( $this->isEchoLoaded ) {
return EchoEvent::create( $this->getDataForEvent( $filter ) );
}
return false;
}
}