Passed
Push — 26-use-psr-14-event-dispatcher ( ab95da )
by Jakub
02:36
created

TesterEventDispatcher::dispatch()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 6
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Psr\EventDispatcher\ListenerProviderInterface;
8
use Psr\EventDispatcher\StoppableEventInterface;
9
10
/**
11
 * Event dispatcher for {@see Tester}
12
 *
13
 * @author Jakub Konečný
14
 * @internal
15
 */
16
final class TesterEventDispatcher implements EventDispatcherInterface
17
{
18
    private ListenerProviderInterface $listenerProvider;
19
20
    public function __construct(ListenerProviderInterface $listenerProvider)
21
    {
22 1
        $this->listenerProvider = $listenerProvider;
23 1
    }
24
25
    /**
26
     * @template T of object
27
     * @param T $event
28
     * @return T
29
     */
30
    public function dispatch(object $event): object
31
    {
32 1
        $listeners = $this->listenerProvider->getListenersForEvent($event);
33 1
        foreach ($listeners as $listener) {
34 1
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
35 1
                break;
36
            }
37 1
            $listener($event);
38
        }
39 1
        return $event;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $event also could return the type Psr\EventDispatcher\StoppableEventInterface which is incompatible with the documented return type MyTester\T.
Loading history...
40
    }
41
}
42