Completed
Push — master ( c5ec2d...1e9b29 )
by Till
03:26
created

EventMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TillKruss\LaravelTactician\Middleware;
4
5
use Exception;
6
use Throwable;
7
use League\Tactician\Middleware;
8
use Illuminate\Contracts\Events\Dispatcher;
9
use TillKruss\LaravelTactician\Events\CommandFailed;
10
use TillKruss\LaravelTactician\Events\CommandHandled;
11
use TillKruss\LaravelTactician\Events\CommandReceived;
12
13
class EventMiddleware implements Middleware
14
{
15
    /**
16
     * The event dispatcher instance.
17
     *
18
     * @var Illuminate\Contracts\Events\Dispatcher
19
     */
20
    protected $dispatcher;
21
22
    /**
23
     * Create a new command events middleware.
24
     *
25
     * @param Illuminate\Contracts\Events\Dispatcher  $dispatcher
26
     */
27
    public function __construct(Dispatcher $dispatcher)
28
    {
29
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dispatcher of type object<Illuminate\Contracts\Events\Dispatcher> is incompatible with the declared type object<TillKruss\Laravel...acts\Events\Dispatcher> of property $dispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
    }
31
32
    /**
33
     * Dispatch an event whenever a command is received, handled or fails.
34
     *
35
     * @param  object    $command
36
     * @param  callable  $next
37
     * @return mixed
38
     *
39
     * @throws Exception
40
     * @throws Throwable
41
     */
42
    public function execute($command, callable $next)
43
    {
44
        try {
45
            $this->dispatcher->fire(new CommandReceived($command));
46
47
            $returnValue = $next($command);
48
49
            $this->dispatcher->fire(new CommandHandled($command));
50
51
            return $returnValue;
52
        } catch (Exception $exception) {
53
            $event = new CommandFailed($command, $exception);
54
55
            $this->dispatcher->fire($event);
56
57
            if (! $event->isExceptionCaught()) {
58
                throw $exception;
59
            }
60
        } catch (Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
61
            $event = new CommandFailed($command, $exception);
62
63
            $this->dispatcher->fire($event);
64
65
            if (! $event->isExceptionCaught()) {
66
                throw $exception;
67
            }
68
        }
69
    }
70
}
71