Debian Codegraph @

extensions/Echo/includes/Mapper/AbstractMapper.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Mapper;
4
5 use InvalidArgumentException;
6 use MediaWiki\Extension\Notifications\DbFactory;
7
8 /**
9 * Abstract mapper for model
10 */
11 abstract class AbstractMapper {
12
13 /**
14 * Echo database factory
15 * @var DbFactory
16 */
17 protected $dbFactory;
18
19 /**
20 * Event listeners for method like insert/delete
21 * @var array[]
22 */
23 protected $listeners;
24
25 public function __construct( ?DbFactory $dbFactory = null ) {
26 $this->dbFactory = $dbFactory ?? DbFactory::newFromDefault();
27 }
28
29 /**
30 * Attach a listener
31 *
32 * @param string $method Method name
33 * @param string $key Identification of the callable
34 * @param callable $callable
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 * Detach a listener
49 *
50 * @param string $method Method name
51 * @param string $key identification of the callable
52 */
53 public function detachListener( $method, $key ) {
54 if ( isset( $this->listeners[$method] ) ) {
55 unset( $this->listeners[$method][$key] );
56 }
57 }
58
59 /**
60 * Get the listener for a method
61 *
62 * @param string $method
63 * @return callable[]
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 }