ConfigurableEventDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
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