Test Failed
Pull Request — master (#10)
by Alice
04:15
created

Mediator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 44
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A removeListener() 0 7 2
A addListener() 0 5 1
A notify() 0 4 2
A getListeners() 0 3 1
1
<?php
2
3
namespace Wonderland\Thread\Mediator;
4
5
use Wonderland\Thread\Mediator\Event\EventInterface;
6
use Wonderland\Thread\Mediator\Listener\ListenerInterface;
7
8
class Mediator
9
{
10
	/** @var ListenerInterface[] */
11
	private $listeners = [];
12
13
	/**
14
	 * @return ListenerInterface[]
15
	 */
16
	public function getListeners(): array
17
	{
18
		return $this->listeners;
19
	}
20
21
	/**
22
	 * @param ListenerInterface $listener
23
	 * @return Mediator
24
	 */
25
	public function addListener(ListenerInterface $listener): self
26
	{
27
		$this->listeners[] = $listener;
28
29
		return $this;
30
	}
31
32
	/**
33
	 * @param ListenerInterface $listener
34
	 * @return Mediator
35
	 */
36
	public function removeListener(ListenerInterface $listener): self
37
	{
38
        if (false !== ($key = array_search($listener, $this->listeners, true))) {
39
            unset($this->listeners[$key]);
40
        }
41
42
		return $this;
43
	}
44
45
	/**
46
	 * @param EventInterface $event
47
	 */
48
	public function notify(EventInterface $event)
49
	{
50
		foreach ($this->listeners as $listener) {
51
			$listener->notify($event);
52
		}
53
	}
54
55
}
56