Passed
Pull Request — master (#16)
by Alexander
10:48
created

Dispatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 19
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 10 4
A __construct() 0 3 1
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
    public function __construct(ListenerProviderInterface $listenerProvider)
18
    {
19
        $this->listenerProvider = $listenerProvider;
20
    }
21
22
    public function dispatch(object $event): object
23
    {
24
        foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
25
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
26
                return $event;
27
            }
28
            $listener($event);
29
        }
30
31
        return $event;
32
    }
33
}
34