RegistryEventHandler::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
namespace Kir\FakePDO\EventHandlers;
3
4
class RegistryEventHandler implements EventHandler {
5
	/** @var array[] */
6
	private $handlers = [];
7
8
	/**
9
	 * @param string $eventName
10
	 * @param callable $callback
11
	 * @param callable $filterFn
12
	 * @return $this
13
	 */
14
	public function add($eventName, callable $callback, callable $filterFn = null) {
15
		$this->handlers[$eventName] = [
16
			'callback' => $callback,
17
			'filterFn' => $filterFn
18
		];
19
		return $this;
20
	}
21
22
	/**
23
	 * @param string $eventName
24
	 * @param array $params
25
	 * @param object $callee
26
	 * @return mixed
27
	 */
28
	public function invoke($eventName, array $params, $callee) {
29
		if(!$this->isResponsible($eventName, $params, $callee)) {
30
			return null;
31
		}
32
		$handler = $this->handlers[$eventName];
33
		if($handler['filterFn'] === null || call_user_func($handler['filterFn'], $eventName, $params, $callee)) {
34
			return call_user_func($handler['callback'], $eventName, $params, $callee);
35
		}
36
		return null;
37
	}
38
39
	/**
40
	 * @param string $eventName
41
	 * @param array $params
42
	 * @param object $callee
43
	 * @return bool
44
	 */
45
	public function isResponsible($eventName, array $params, $callee) {
46
		return array_key_exists($eventName, $this->handlers);
47
	}
48
}
49