Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractMapper
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 attachListener
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 detachListener
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getMethodListeners
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Notifications\Mapper;
4
5use InvalidArgumentException;
6use MediaWiki\Extension\Notifications\DbFactory;
7
8/**
9 * Abstract mapper for model
10 */
11abstract 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    /**
26     * @param DbFactory|null $dbFactory
27     */
28    public function __construct( DbFactory $dbFactory = null ) {
29        $this->dbFactory = $dbFactory ?? DbFactory::newFromDefault();
30    }
31
32    /**
33     * Attach a listener
34     *
35     * @param string $method Method name
36     * @param string $key Identification of the callable
37     * @param callable $callable
38     */
39    public function attachListener( $method, $key, $callable ) {
40        if ( !method_exists( $this, $method ) ) {
41            throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) );
42        }
43        if ( !isset( $this->listeners[$method] ) ) {
44            $this->listeners[$method] = [];
45        }
46
47        $this->listeners[$method][$key] = $callable;
48    }
49
50    /**
51     * Detach a listener
52     *
53     * @param string $method Method name
54     * @param string $key identification of the callable
55     */
56    public function detachListener( $method, $key ) {
57        if ( isset( $this->listeners[$method] ) ) {
58            unset( $this->listeners[$method][$key] );
59        }
60    }
61
62    /**
63     * Get the listener for a method
64     *
65     * @param string $method
66     * @return callable[]
67     */
68    public function getMethodListeners( $method ) {
69        if ( !method_exists( $this, $method ) ) {
70            throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) );
71        }
72
73        return $this->listeners[$method] ?? [];
74    }
75
76}