EventMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
wmc 4
lcom 0
cbo 5
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 18 3
1
<?php
2
3
namespace League\Tactician\CommandEvents;
4
5
use League\Event\EmitterAwareInterface;
6
use League\Event\EmitterInterface;
7
use League\Event\EmitterTrait;
8
use League\Tactician\Middleware;
9
10
/**
11
 * Provides an event-driven middleware functionality
12
 */
13
final class EventMiddleware implements Middleware, EmitterAwareInterface
14
{
15
    use EmitterTrait;
16
17
    /**
18
     * @param EmitterInterface $emitter
19
     */
20 6
    public function __construct(EmitterInterface $emitter = null)
21
    {
22 6
        $this->setEmitter($emitter);
23 6
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 3
    public function execute($command, callable $next)
29
    {
30
        try {
31 3
            $this->getEmitter()->emit(new Event\CommandReceived($command));
32
33 1
            $returnValue = $next($command);
34
35 1
            $this->getEmitter()->emit(new Event\CommandHandled($command));
36
37 1
            return $returnValue;
38 2
        } catch (\Exception $e) {
39 2
            $this->getEmitter()->emit($event = new Event\CommandFailed($command, $e));
40
41 2
            if (!$event->isExceptionCaught()) {
42 1
                throw $e;
43
            }
44
        }
45 1
    }
46
}
47