BasicEventDispatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 1
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A dispatch() 0 17 6
1
<?php
2
3
/*
4
 * (c) Olivier Laviale <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace olvlvl\EventDispatcher;
11
12
use Psr\EventDispatcher\EventDispatcherInterface;
13
use Psr\EventDispatcher\ListenerProviderInterface;
14
use Psr\EventDispatcher\StoppableEventInterface;
15
16
/**
17
 * A basic implementation of an Event Dispatcher.
18
 */
19
final class BasicEventDispatcher implements EventDispatcherInterface
20
{
21
    /**
22
     * @var ListenerProviderInterface
23
     */
24
    private $listenerProvider;
25
26
    public function __construct(ListenerProviderInterface $listenerProvider)
27
    {
28
        $this->listenerProvider = $listenerProvider;
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function dispatch(object $event): object
35
    {
36
        $stoppable = $event instanceof StoppableEventInterface;
37
        if ($stoppable && $event->isPropagationStopped()) {
38
            return $event;
39
        }
40
41
        foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
42
            $listener($event);
43
44
            // @phpstan-ignore-next-line
45
            if ($stoppable && $event->isPropagationStopped()) {
46
                break;
47
            }
48
        }
49
50
        return $event;
51
    }
52
}
53