AbstractEventDispatcher::dispatch()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
f 0
dl 0
loc 15
ccs 3
cts 3
cp 1
rs 10
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\EventDispatcher;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\EventDispatcher\ListenerProviderInterface;
9
use Psr\EventDispatcher\StoppableEventInterface;
10
11
abstract class AbstractEventDispatcher implements EventDispatcherInterface
12
{
13
    abstract protected function getListenerProvider(): ListenerProviderInterface;
14
15
    /**
16
     * @param object|StoppableEventInterface $event
17
     *
18
     * @throws \Throwable
19
     */
20
    public function dispatch(object $event): object
21
    {
22
        if ($this->isPropagationStopped($event)) {
23
            return $event;
24
        }
25
26
        foreach ($this->getListenerProvider()->getListenersForEvent($event) as $listener) {
27
            $listener($event);
28
29
            if ($this->isPropagationStopped($event)) {
30
                break;
31 8
            }
32
        }
33 8
34 1
        return $event;
35
    }
36
37 7
    /**
38 7
     * @param object|StoppableEventInterface $event
39
     */
40
    protected function isPropagationStopped(object $event): bool
41 7
    {
42 5
        return ($event instanceof StoppableEventInterface && $event->isPropagationStopped());
43
    }
44
}
45