Completed
Push — master ( 030030...90a95a )
by Matze
10:56 queued 07:21
created

EventDispatcher::addCatchall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BrainExe\Core\EventDispatcher;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Annotations\Annotations\Service;
7
use BrainExe\Core\Websockets\WebSocketEvent;
8
use RuntimeException;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
11
use Symfony\Component\EventDispatcher\Event;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
/**
15
 * @Service("EventDispatcher", public=false)
16
 * @api
17
 */
18
class EventDispatcher extends ContainerAwareEventDispatcher
19
{
20
21
    /**
22
     * @var EventDispatcherInterface[]
23
     */
24
    private $catchall = [];
25
26
    /**
27
     * @Inject({"@service_container"})
28
     * @param ContainerInterface $container
29
     */
30 8
    public function __construct(ContainerInterface $container)
31
    {
32 8
        parent::__construct($container);
33 8
    }
34
35
    /**
36
     * @param EventDispatcherInterface $dispatcher
37
     */
38
    public function addCatchall(EventDispatcherInterface $dispatcher)
39
    {
40
        $this->catchall[] = $dispatcher;
41
    }
42
43
    /**
44
     * @param string $eventName
45
     * @param Event $event
46
     * @return Event
47
     */
48 2
    public function dispatch($eventName, Event $event = null)
49
    {
50 2
        if (empty($event)) {
51 1
            throw new RuntimeException('You have to pass an Event into EventDispatcher::dispatch');
52
        }
53
54 1
        foreach ($this->catchall as $dispatcher) {
55
            $dispatcher->dispatch($eventName, $event);
56
        }
57
58 1
        return parent::dispatch($eventName, $event);
59
    }
60
61
    /**
62
     * @param AbstractEvent $event
63
     */
64 4
    public function dispatchEvent(AbstractEvent $event)
65
    {
66 4
        $this->dispatch($event->eventName, $event);
67 4
        if ($event instanceof PushViaWebsocket) {
68
            /** @var AbstractEvent $event */
69 1
            $this->dispatchAsWebsocketEvent($event);
70
        }
71 4
    }
72
73
    /**
74
     * @param AbstractEvent $event
75
     */
76 1
    public function dispatchAsWebsocketEvent(AbstractEvent $event)
77
    {
78 1
        $wrappedEvent = new WebSocketEvent($event);
79
80 1
        $this->dispatch($wrappedEvent->eventName, $wrappedEvent);
81 1
    }
82
83
    /**
84
     * @param AbstractEvent $event
85
     * @param integer|null $timestamp
86
     */
87 2
    public function dispatchInBackground(AbstractEvent $event, $timestamp = 0)
88
    {
89 2
        if ($timestamp) {
90 1
            $wrapper = new DelayedEvent($event, $timestamp);
91
        } else {
92 1
            $wrapper = new BackgroundEvent($event);
93
        }
94
95 2
        $this->dispatchEvent($wrapper);
96 2
    }
97
}
98