Passed
Pull Request — master (#41)
by Viktor
13:13
created

Dispatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 3 1
A dispatch() 0 11 4
1
<?php
2
3
namespace Yiisoft\EventDispatcher\Dispatcher;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
use Psr\EventDispatcher\ListenerProviderInterface;
7
use Psr\EventDispatcher\StoppableEventInterface;
8
9
/**
10
 * Dispatcher executes listeners attached to event passed
11
 * @see https://www.php-fig.org/psr/psr-14/
12
 */
13
final class Dispatcher implements EventDispatcherInterface
14
{
15
    private ListenerProviderInterface $listenerProvider;
16
17 2
    public function __construct(ListenerProviderInterface $listenerProvider)
18
    {
19 2
        $this->listenerProvider = $listenerProvider;
20 2
    }
21
22 2
    public function dispatch(object $event): object
23
    {
24 2
        foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
25 2
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
26 1
                return $event;
27
            }
28 2
29
            $this->handle($event, $listener);
30
        }
31 1
32
        return $event;
33
    }
34
35
    private function handle(object $event, callable $listener)
36
    {
37
        $listener($event);
38
    }
39
}
40