1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* The software is based on the Axon Framework project which is |
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
18
|
|
|
* see <http://www.axonframework.org/>. |
19
|
|
|
* |
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
21
|
|
|
* and is licensed under the MIT license. For more information, see |
22
|
|
|
* <http://www.governor-framework.org/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Governor\Framework\Test\Utils; |
26
|
|
|
|
27
|
|
|
use Governor\Framework\CommandHandling\CommandBusInterface; |
28
|
|
|
use Governor\Framework\CommandHandling\CommandCallbackInterface; |
29
|
|
|
use Governor\Framework\CommandHandling\CommandHandlerInterface; |
30
|
|
|
use Governor\Framework\CommandHandling\CommandMessageInterface; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class RecordingCommandBus implements CommandBusInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $subscriptions; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $dispatchedCommands = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var CallbackBehaviorInterface |
47
|
|
|
*/ |
48
|
|
|
private $callbackBehavior; |
49
|
|
|
|
50
|
5 |
|
function __construct() |
|
|
|
|
51
|
|
|
{ |
52
|
5 |
|
$this->callbackBehavior = new DefaultCallbackBehavior(); |
53
|
5 |
|
$this->subscriptions = []; |
54
|
5 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function dispatch( |
58
|
|
|
CommandMessageInterface $command, |
59
|
|
|
CommandCallbackInterface $callback = null |
60
|
|
|
) { |
61
|
|
|
$this->dispatchedCommands[] = $command; |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
if (null !== $callback) { |
65
|
|
|
$callback->onSuccess($this->callbackBehavior->handle($command->getPayload(), $command->getMetaData())); |
66
|
|
|
} |
67
|
|
|
} catch (\Exception $ex) { |
68
|
|
|
if (null !== $callback) { |
69
|
|
|
$callback->onFailure($ex); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function subscribe($commandName, CommandHandlerInterface $handler) |
78
|
|
|
{ |
79
|
|
|
if (!array_key_exists($commandName, $this->subscriptions)) { |
80
|
|
|
$this->subscriptions[$commandName] = $handler; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function unsubscribe($commandName, CommandHandlerInterface $handler) |
88
|
|
|
{ |
89
|
|
|
if (array_key_exists($commandName, $this->subscriptions)) { |
90
|
|
|
unset($this->subscriptions[$commandName]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Clears all the commands recorded by this Command Bus. |
96
|
|
|
*/ |
97
|
5 |
|
public function clearCommands() |
98
|
|
|
{ |
99
|
5 |
|
$this->dispatchedCommands = []; |
100
|
5 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Clears all subscribed handlers on this command bus. |
104
|
|
|
*/ |
105
|
|
|
public function clearSubscriptions() |
106
|
|
|
{ |
107
|
|
|
$this->subscriptions = array(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Indicates whether the given <code>commandHandler</code> is subscribed to this command bus. |
112
|
|
|
* |
113
|
|
|
* @param CommandHandlerInterface $commandHandler The command handler to verify the subscription for |
114
|
|
|
* @return boolean <code>true</code> if the handler is subscribed, otherwise <code>false</code>. |
115
|
|
|
*/ |
116
|
|
|
public function isSubscribed(CommandHandlerInterface $commandHandler) |
117
|
|
|
{ |
118
|
|
|
foreach ($this->subscriptions as $cmd => $handler) { |
119
|
|
|
if ($commandHandler == $handler) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns a Map will all Command Names and their Command Handler that have been subscribed to this command bus. |
130
|
|
|
* |
131
|
|
|
* @return array a Map will all Command Names and their Command Handler |
132
|
|
|
*/ |
133
|
|
|
public function getSubscriptions() |
134
|
|
|
{ |
135
|
|
|
return $this->subscriptions; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a list with all commands that have been dispatched by this command bus. |
140
|
|
|
* |
141
|
|
|
* @return array a list with all commands that have been dispatched |
142
|
|
|
*/ |
143
|
3 |
|
public function getDispatchedCommands() |
144
|
|
|
{ |
145
|
3 |
|
return $this->dispatchedCommands; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets the instance that defines the behavior of the Command Bus when a command is dispatched with a callback. |
151
|
|
|
* |
152
|
|
|
* @param CallbackBehaviorInterface $callbackBehavior The instance deciding to how the callback should be invoked. |
153
|
|
|
*/ |
154
|
|
|
public function setCallbackBehavior(CallbackBehaviorInterface $callbackBehavior) |
155
|
|
|
{ |
156
|
|
|
$this->callbackBehavior = $callbackBehavior; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.