Resque_Event   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 78
ccs 23
cts 25
cp 0.92
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A trigger() 0 18 5
A clearListeners() 0 3 1
A listen() 0 8 2
A stopListening() 0 12 3
1
<?php
2
3
/**
4
 * Resque event/plugin system class
5
 *
6
 * @package		Resque/Event
7
 * @author		Chris Boulton <[email protected]>
8
 * @license		http://www.opensource.org/licenses/mit-license.php
9
 */
10
class Resque_Event
11
{
12
	/**
13
	 * @var array Array containing all registered callbacks, indexked by event name.
14
	 */
15
	private static $events = array();
16
17
	/**
18
	 * Raise a given event with the supplied data.
19
	 *
20
	 * @param string $event Name of event to be raised.
21
	 * @param mixed $data Optional, any data that should be passed to each callback.
22
	 * @return true
23 57
	 */
24
	public static function trigger($event, $data = null)
25 57
	{
26 19
		if (!is_array($data)) {
27
			$data = array($data);
28
		}
29 57
30 55
		if (empty(self::$events[$event])) {
31
			return true;
32
		}
33 8
34 8
		foreach (self::$events[$event] as $callback) {
35
			if (!is_callable($callback)) {
36
				continue;
37 8
			}
38
			call_user_func_array($callback, $data);
39
		}
40 6
41
		return true;
42
	}
43
44
	/**
45
	 * Listen in on a given event to have a specified callback fired.
46
	 *
47
	 * @param string $event Name of event to listen on.
48
	 * @param mixed $callback Any callback callable by call_user_func_array.
49
	 * @return true
50 9
	 */
51
	public static function listen($event, $callback)
52 9
	{
53 9
		if (!isset(self::$events[$event])) {
54
			self::$events[$event] = array();
55
		}
56 9
57 9
		self::$events[$event][] = $callback;
58
		return true;
59
	}
60
61
	/**
62
	 * Stop a given callback from listening on a specific event.
63
	 *
64
	 * @param string $event Name of event.
65
	 * @param mixed $callback The callback as defined when listen() was called.
66
	 * @return true
67 1
	 */
68
	public static function stopListening($event, $callback)
69 1
	{
70
		if (!isset(self::$events[$event])) {
71
			return true;
72
		}
73 1
74 1
		$key = array_search($callback, self::$events[$event]);
75 1
		if ($key !== false) {
76
			unset(self::$events[$event][$key]);
77
		}
78 1
79
		return true;
80
	}
81
82
	/**
83
	 * Call all registered listeners.
84 9
	 */
85
	public static function clearListeners()
86 9
	{
87 9
		self::$events = array();
88
	}
89
}
90