ConfigurableEventDispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10
c 2
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerGenericListeners() 0 3 1
A notifyListener() 0 3 1
A dispatch() 0 8 3
A registerSpecificListener() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Event\Dispatcher;
6
7
final class ConfigurableEventDispatcher implements EventDispatcherInterface
8
{
9
    /** @var array<callable> */
10
    private array $genericListeners = [];
11
12
    /** @var array<class-string,list<callable>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string,list<callable>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,list<callable>>.
Loading history...
13
    private array $specificListeners = [];
14
15
    /**
16
     * @param list<callable> $genericListeners
17 16
     */
18
    public function registerGenericListeners(array $genericListeners): void
19 16
    {
20
        $this->genericListeners = $genericListeners;
21
    }
22
23
    /**
24 16
     * @param class-string $event
25
     */
26 16
    public function registerSpecificListener(string $event, callable $listener): void
27
    {
28
        $this->specificListeners[$event][] = $listener;
29
    }
30
31
    public function dispatch(object $event): void
32 6
    {
33
        foreach ($this->genericListeners as $listener) {
34 6
            $this->notifyListener($listener, $event);
35
        }
36
37 19
        foreach ($this->specificListeners[$event::class] ?? [] as $listener) {
38
            $this->notifyListener($listener, $event);
39 19
        }
40 12
    }
41
42
    private function notifyListener(callable $listener, object $event): void
43 19
    {
44 7
        $listener($event);
45
    }
46
}
47