Completed
Push — master ( 44b89d...13d44b )
by Matze
07:10
created

EventDispatcher::dispatchInBackground()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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