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; |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
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..