EventBusMiddleware::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 2
1
<?php
2
3
namespace NilPortugues\MessageBus\EventBus;
4
5
use NilPortugues\MessageBus\EventBus\Contracts\Event;
6
use NilPortugues\MessageBus\EventBus\Contracts\EventBusMiddleware as EventBusMiddlewareInterface;
7
use NilPortugues\MessageBus\EventBus\Contracts\EventHandlerResolver;
8
use NilPortugues\MessageBus\EventBus\Contracts\EventTranslator;
9
10
/**
11
 * Class EventBusMiddleware.
12
 */
13
class EventBusMiddleware implements EventBusMiddlewareInterface
14
{
15
    /** @var EventTranslator */
16
    protected $eventTranslator;
17
18
    /** @var EventHandlerResolver */
19
    protected $handlerResolver;
20
21
    /**
22
     * @param EventTranslator      $eventTranslator
23
     * @param EventHandlerResolver $handlerResolver
24
     */
25
    public function __construct(EventTranslator $eventTranslator, EventHandlerResolver $handlerResolver)
26
    {
27
        $this->eventTranslator = $eventTranslator;
28
        $this->handlerResolver = $handlerResolver;
29
    }
30
31
    /**
32
     * @param Event         $event
33
     * @param callable|null $next
34
     */
35
    public function __invoke(Event $event, callable $next = null)
36
    {
37
        $handlerNames = $this->eventTranslator->handlerName($event);
38
39
        foreach ($handlerNames as $handlerLists) {
40
            foreach ($handlerLists as $handlerName) {
0 ignored issues
show
Bug introduced by
The expression $handlerLists of type string is not traversable.
Loading history...
41
                $handlerInstance = $this->handlerResolver->instantiate($handlerName);
42
                $handlerInstance($event);
43
            }
44
        }
45
46
        if ($next) {
47
            $next($event);
48
        }
49
    }
50
}
51