Dispatcher   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A dispatch() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Th3Mouk\ReactiveEventDispatcher;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\EventDispatcher\ListenerProviderInterface;
9
use Rx\Observable;
10
11
final class Dispatcher implements EventDispatcherInterface
12
{
13
    private ListenerProviderInterface $listenerProvider;
14
15
    public function __construct(ListenerProviderInterface $listenerProvider)
16
    {
17
        $this->listenerProvider = $listenerProvider;
18
    }
19
20
    /**
21
     * @return Observable
22
     *
23
     * @inheritDoc
24
     */
25
    public function dispatch(object $event)
26
    {
27
        $listeners = $this->listenerProvider->getListenersForEvent($event);
28
29
        $observable = Observable::of($event);
30
31
        foreach ($listeners as $listener) {
32
            \assert(\is_callable($listener));
33
            $observable = $observable
34
                ->flatMap($listener)
35
                ->mapTo($event);
36
        }
37
38
        return $observable;
39
    }
40
}
41