Total Complexity | 7 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
14 | class ListenerCollection implements IteratorAggregate, ListenerCollectorInterface, ListenerLocatorInterface |
||
15 | { |
||
16 | /** @var array<string, array<int, callable>> */ |
||
17 | private array $listeners; |
||
18 | |||
19 | 5 | public function __construct() |
|
20 | { |
||
21 | 5 | $this->listeners = []; |
|
22 | 5 | } |
|
23 | |||
24 | 3 | public function addListener(string $eventClass, callable $listener): void |
|
25 | { |
||
26 | 3 | if ($this->has($eventClass)) { |
|
27 | 3 | $this->listeners[$eventClass][] = $listener; |
|
28 | 3 | return; |
|
29 | } |
||
30 | |||
31 | 3 | $this->listeners[$eventClass] = [$listener]; |
|
32 | 3 | } |
|
33 | |||
34 | /** |
||
35 | * @return iterable<callable> |
||
36 | */ |
||
37 | 3 | public function get(string $eventClass): iterable |
|
38 | { |
||
39 | 3 | if ($this->has($eventClass)) { |
|
40 | 3 | yield from $this->listeners[$eventClass]; |
|
41 | } |
||
42 | 3 | } |
|
43 | |||
44 | 3 | public function has(string $eventClass): bool |
|
45 | { |
||
46 | 3 | return array_key_exists($eventClass, $this->listeners); |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return \Generator<mixed>|\Traversable<mixed> |
||
51 | */ |
||
52 | 1 | public function getIterator() |
|
55 | 1 | } |
|
56 | } |
||
57 |