GenericEventDispatcher   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A registerWatcher() 0 9 2
A unregisterWatcher() 0 9 4
A dispatch() 0 9 3
1
<?php
2
3
namespace Wikibase\Repo;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Dispatches a notification to a set of watchers.
9
 *
10
 * @todo should go into MediaWiki core.
11
 *
12
 * @license GPL-2.0-or-later
13
 * @author Daniel Kinzler
14
 */
15
class GenericEventDispatcher {
16
17
	/**
18
	 * @var array
19
	 */
20
	private $watchers = [];
21
22
	/**
23
	 * @var int
24
	 */
25
	private $key = 0;
26
27
	/**
28
	 * @var string
29
	 */
30
	private $interface;
31
32
	/**
33
	 * @param string $interface the interface watchers must implement
34
	 */
35
	public function __construct( $interface ) {
36
		$this->interface = $interface;
37
	}
38
39
	/**
40
	 * Registers a watcher. The watcher will be called whenever
41
	 * the dispatch() method is called, until the watcher is unregistered.
42
	 *
43
	 * @param object $listener
44
	 *
45
	 * @throws InvalidArgumentException
46
	 * @return mixed The listener key, for removing the listener later.
47
	 */
48
	public function registerWatcher( $listener ) {
49
		if ( !is_subclass_of( $listener, $this->interface ) ) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $this->interface can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
50
			throw new InvalidArgumentException( '$listener must implement ' . $this->interface );
51
		}
52
53
		$key = ++$this->key;
54
		$this->watchers[ $key ] = $listener;
55
		return $key;
56
	}
57
58
	/**
59
	 * Unregisters a watcher using its registration key. The watcher will no longer
60
	 * be called by dispatch().
61
	 *
62
	 * @param mixed $key A watcher key as returned by registerWatcher().
63
	 *
64
	 * @throws InvalidArgumentException
65
	 */
66
	public function unregisterWatcher( $key ) {
67
		if ( is_object( $key ) || is_array( $key ) ) {
68
			throw new InvalidArgumentException( '$key must be a primitive value' );
69
		}
70
71
		if ( isset( $this->watchers[$key] ) ) {
72
			unset( $this->watchers[$key] );
73
		}
74
	}
75
76
	/**
77
	 * Dispatches a notification to all registered watchers.
78
	 *
79
	 * @param string $event the name of the event, that is,
80
	 *        the name of the method to call on the watchers.
81
	 * @param mixed ...$args Any extra parameters are passed to the watcher method.
82
	 *
83
	 * @throws InvalidArgumentException
84
	 */
85
	public function dispatch( $event, ...$args ) {
86
		if ( !is_string( $event ) ) {
87
			throw new InvalidArgumentException( '$event must be a string' );
88
		}
89
90
		foreach ( $this->watchers as $watcher ) {
91
			$watcher->$event( ...$args );
92
		}
93
	}
94
95
}
96