AbstractEventDispatcher::isPropagationStopped()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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