| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Mapper; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Notifications\DbFactory; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
abstract class AbstractMapper { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
protected $dbFactory; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected $listeners; |
| 24 |
|
| 25 |
public function __construct( ?DbFactory $dbFactory = null ) { |
| 26 |
$this->dbFactory = $dbFactory ?? DbFactory::newFromDefault(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function attachListener( $method, $key, $callable ) { |
| 37 |
if ( !method_exists( $this, $method ) ) { |
| 38 |
throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) ); |
| 39 |
} |
| 40 |
if ( !isset( $this->listeners[$method] ) ) { |
| 41 |
$this->listeners[$method] = []; |
| 42 |
} |
| 43 |
|
| 44 |
$this->listeners[$method][$key] = $callable; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function detachListener( $method, $key ) { |
| 54 |
if ( isset( $this->listeners[$method] ) ) { |
| 55 |
unset( $this->listeners[$method][$key] ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getMethodListeners( $method ) { |
| 66 |
if ( !method_exists( $this, $method ) ) { |
| 67 |
throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) ); |
| 68 |
} |
| 69 |
|
| 70 |
return $this->listeners[$method] ?? []; |
| 71 |
} |
| 72 |
|
| 73 |
} |