-
Notifications
You must be signed in to change notification settings - Fork 55
/
CommandBus.php
113 lines (97 loc) · 3.7 KB
/
CommandBus.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* This file is part of prooph/service-bus.
* (c) 2014-2021 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2021 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\ServiceBus;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\ServiceBus\Exception\CommandDispatchException;
use Prooph\ServiceBus\Exception\RuntimeException;
/**
* A command bus is capable of dispatching a message to a command handler.
* Only one handler per message is allowed!
*/
class CommandBus extends MessageBus
{
/**
* @var array
*/
private $commandQueue = [];
/**
* @var bool
*/
private $isDispatching = false;
public function __construct(ActionEventEmitter $actionEventEmitter = null)
{
parent::__construct($actionEventEmitter);
$this->events->attachListener(
self::EVENT_DISPATCH,
function (ActionEvent $actionEvent): void {
$commandHandler = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER);
if (\is_callable($commandHandler)) {
$command = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE);
$commandHandler($command);
$actionEvent->setParam(self::EVENT_PARAM_MESSAGE_HANDLED, true);
}
},
self::PRIORITY_INVOKE_HANDLER
);
$this->events->attachListener(
self::EVENT_DISPATCH,
function (ActionEvent $actionEvent): void {
if ($actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER) === null) {
throw new RuntimeException(\sprintf(
'CommandBus was not able to identify a CommandHandler for command %s',
$this->getMessageName($actionEvent->getParam(self::EVENT_PARAM_MESSAGE))
));
}
},
self::PRIORITY_LOCATE_HANDLER
);
}
/**
* @param mixed $command
*
* @throws CommandDispatchException
*/
public function dispatch($command): void
{
$this->commandQueue[] = $command;
if (! $this->isDispatching) {
$this->isDispatching = true;
$actionEventEmitter = $this->events;
try {
while ($command = \array_shift($this->commandQueue)) {
$actionEvent = $actionEventEmitter->getNewActionEvent(
self::EVENT_DISPATCH,
$this,
[
self::EVENT_PARAM_MESSAGE => $command,
]
);
try {
$actionEventEmitter->dispatch($actionEvent);
if (! $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLED)) {
throw new RuntimeException(\sprintf('Command %s was not handled', $this->getMessageName($command)));
}
} catch (\Throwable $exception) {
$actionEvent->setParam(self::EVENT_PARAM_EXCEPTION, $exception);
} finally {
$actionEvent->stopPropagation(false);
$this->triggerFinalize($actionEvent);
}
}
$this->isDispatching = false;
} catch (\Throwable $e) {
$this->isDispatching = false;
throw CommandDispatchException::wrap($e, $this->commandQueue);
}
}
}
}