Dispatcher::dispatch()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\EventDispatcher\Dispatcher;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\EventDispatcher\ListenerProviderInterface;
9
use Psr\EventDispatcher\StoppableEventInterface;
10
11
/**
12
 * Dispatcher executes listeners attached to event passed.
13
 *
14
 * @see https://www.php-fig.org/psr/psr-14/
15
 */
16
final class Dispatcher implements EventDispatcherInterface
17
{
18 3
    public function __construct(private ListenerProviderInterface $listenerProvider)
19
    {
20 3
    }
21
22 3
    public function dispatch(object $event): object
23
    {
24
        /** @var callable $listener */
25 3
        foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
26 3
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
27 1
                return $event;
28
            }
29
30 3
            $spoofableEvent = $event;
31 3
            $listener($spoofableEvent);
32
        }
33
34 2
        return $event;
35
    }
36
}
37