Dispatcher   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 43
ccs 10
cts 13
cp 0.7692
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B dispatch() 0 23 7
A __construct() 0 3 1
1
<?php
2
3
namespace Nip\Records\EventManager\Dispatcher;
4
5
use Psr\EventDispatcher\ListenerProviderInterface;
6
use Psr\EventDispatcher\StoppableEventInterface;
7
8
/**
9
 * Class Dispatcher
10
 * @package Nip\Records\EventManager\Dispatcher
11
 */
12
class Dispatcher implements \Psr\EventDispatcher\EventDispatcherInterface
13
{
14
15
    /**
16
     * @var ListenerProviderInterface
17
     */
18
    protected $listenerProvider = null;
19
20
    /**
21
     * EventDispatcher constructor.
22
     * @param ListenerProviderInterface $listenerProvider
23
     */
24 1
    public function __construct(ListenerProviderInterface $listenerProvider)
25
    {
26 1
        $this->listenerProvider = $listenerProvider;
27 1
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 2
    public function dispatch(object $event)
33
    {
34
        // If the event is already stopped, this method becomes a no-op.
35 2
        if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
36
            return $event;
37
        }
38
39 2
        foreach ($this->listenerProvider->getListenersForEvent($event) as $listener) {
40
            // Technically this has an extraneous stopped-check after the last listener,
41
            // but that doesn't violate the spec since it's still technically checking
42
            // before each listener is called, given the check above.
43
            try {
44 2
                $listener($event);
45 2
                if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
46 2
                    break;
47
                }
48
            } catch (\Exception $e) {
49
                // We do not catch Errors here, because Errors indicate the developer screwed up in
50
                // some way. Let those bubble up because they should just become fatals.
51
                throw $e;
52
            }
53
        }
54 2
        return $event;
55
    }
56
}
57