EventBusMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 15 4
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