-
Notifications
You must be signed in to change notification settings - Fork 56
/
CommandBus.php
127 lines (105 loc) · 3.79 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014 - 2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 09/14/14 - 16:32
*/
namespace Prooph\ServiceBus;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\ServiceBus\Exception\CommandDispatchException;
use Prooph\ServiceBus\Exception\RuntimeException;
/**
* Class CommandBus
*
* A command bus is capable of dispatching a message to a command handler.
* Only one handler per message is allowed!
*
* @package Prooph\ServiceBus
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class CommandBus extends MessageBus
{
/**
* @var array
*/
private $commandQueue = [];
/**
* @var bool
*/
private $isDispatching = false;
/**
* Inject an ActionEventDispatcher instance
*
* @param ActionEventEmitter $actionEventDispatcher
* @return void
*/
public function setActionEventEmitter(ActionEventEmitter $actionEventDispatcher)
{
$actionEventDispatcher->attachListener(self::EVENT_INVOKE_HANDLER, function (ActionEvent $actionEvent) {
$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);
}
});
$this->events = $actionEventDispatcher;
}
/**
* @param mixed $command
* @throws CommandDispatchException
* @return void
*/
public function dispatch($command)
{
$this->commandQueue[] = $command;
if (! $this->isDispatching) {
$this->isDispatching = true;
try {
while ($command = array_shift($this->commandQueue)) {
$this->processCommand($command);
}
$this->isDispatching = false;
} catch (\Exception $e) {
$this->isDispatching = false;
throw CommandDispatchException::wrap($e, $this->commandQueue);
}
}
}
protected function processCommand($command)
{
$actionEvent = $this->getActionEventEmitter()->getNewActionEvent();
$actionEvent->setTarget($this);
try {
$this->initialize($command, $actionEvent);
if ($actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER) === null) {
$actionEvent->setName(self::EVENT_ROUTE);
$this->trigger($actionEvent);
}
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($command)
));
}
$handler = $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLER);
if (is_string($handler) && ! is_callable($handler)) {
$actionEvent->setName(self::EVENT_LOCATE_HANDLER);
$this->trigger($actionEvent);
}
$actionEvent->setName(self::EVENT_INVOKE_HANDLER);
$this->trigger($actionEvent);
if (! $actionEvent->getParam(self::EVENT_PARAM_MESSAGE_HANDLED)) {
throw new RuntimeException(sprintf('Command %s was not handled', $this->getMessageName($command)));
}
$this->triggerFinalize($actionEvent);
} catch (\Exception $ex) {
$this->handleException($actionEvent, $ex);
}
}
}