AbstractEventDispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isPropagationStopped() 0 3 2
A dispatch() 0 15 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