EventDispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 11
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 17 5
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace OpenEngine\EventDispatcher;
4
5
use Psr\EventDispatcher\EventDispatcherInterface;
6
use Psr\EventDispatcher\ListenerProviderInterface;
7
use Psr\EventDispatcher\StoppableEventInterface;
8
9
/**
10
 * Defines a dispatcher for events.
11
 */
12
class EventDispatcher implements EventDispatcherInterface
13
{
14
    /**
15
     * @var ListenerProviderInterface
16
     */
17
    private $provider;
18
19
    /**
20
     * EventDispatcher constructor.
21
     * @param ListenerProviderInterface $provider
22
     */
23
    public function __construct(ListenerProviderInterface $provider)
24
    {
25
        $this->provider = $provider;
26
    }
27
28
    /**
29
     * Provide all listeners with an event to process.
30
     *
31
     * @param object $event
32
     *  The object to process.
33
     *
34
     * @return object
35
     *  The Event that was passed, now modified by listeners.
36
     */
37
    public function dispatch(object $event)
38
    {
39
        $returnedEvent = $event;
40
41
        foreach ($this->provider->getListenersForEvent($event) as $listener) {
42
            $returnedEvent = $listener($event);
43
44
            if (!$returnedEvent instanceof $event) {
45
                throw new \LogicException('Listener must return same event');
46
            }
47
48
            if ($returnedEvent instanceof StoppableEventInterface && $returnedEvent->isPropagationStopped()) {
49
                break;
50
            }
51
        }
52
53
        return $returnedEvent;
54
    }
55
}
56