EventDispatcher::dispatchAsWebsocketEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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