EventDispatcher   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 17
c 1
b 0
f 1
dl 0
loc 75
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 6 1
A dispatchSync() 0 13 2
A __construct() 0 6 1
A dispatchAsync() 0 5 1
A getEventListenerProvider() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Event;
6
7
class EventDispatcher implements EventDispatcherInterface
8
{
9
    /**
10
     * @var \Jellyfish\Event\EventListenerProviderInterface
11
     */
12
    protected $eventListenerProvider;
13
14
    /**
15
     * @var \Jellyfish\Event\EventQueueProducerInterface
16
     */
17
    protected $eventQueueProducer;
18
19
    /**
20
     * @param \Jellyfish\Event\EventListenerProviderInterface $eventListenerProvider
21
     * @param \Jellyfish\Event\EventQueueProducerInterface $eventQueueProducer
22
     */
23
    public function __construct(
24
        EventListenerProviderInterface $eventListenerProvider,
25
        EventQueueProducerInterface $eventQueueProducer
26
    ) {
27
        $this->eventListenerProvider = $eventListenerProvider;
28
        $this->eventQueueProducer = $eventQueueProducer;
29
    }
30
31
    /**
32
     * @param \Jellyfish\Event\EventInterface $event
33
     *
34
     * @return \Jellyfish\Event\EventDispatcherInterface
35
     */
36
    public function dispatch(EventInterface $event): EventDispatcherInterface
37
    {
38
        $this->dispatchSync($event);
39
        $this->dispatchAsync($event);
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param \Jellyfish\Event\EventInterface $event
46
     *
47
     * @return \Jellyfish\Event\EventDispatcherInterface
48
     */
49
    protected function dispatchSync(EventInterface $event): EventDispatcherInterface
50
    {
51
        $listeners = $this->eventListenerProvider->getListenersByTypeAndEventName(
52
            EventListenerInterface::TYPE_SYNC,
53
            $event->getName()
54
        );
55
56
        foreach ($listeners as $listener) {
57
            /** @var \Jellyfish\Event\EventListenerInterface $listener */
58
            $listener->handle($event);
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param \Jellyfish\Event\EventInterface $event
66
     *
67
     * @return \Jellyfish\Event\EventDispatcherInterface
68
     */
69
    protected function dispatchAsync(EventInterface $event): EventDispatcherInterface
70
    {
71
        $this->eventQueueProducer->enqueue($event);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return \Jellyfish\Event\EventListenerProviderInterface
78
     */
79
    public function getEventListenerProvider(): EventListenerProviderInterface
80
    {
81
        return $this->eventListenerProvider;
82
    }
83
}
84