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\Saga; |
26
|
|
|
|
27
|
|
|
use Governor\Framework\Domain\EventMessageInterface; |
28
|
|
|
use Governor\Framework\Domain\GenericDomainEventMessage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @author "David Kalosi" <[email protected]> |
33
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
34
|
|
|
*/ |
35
|
|
|
class AggregateEventPublisherImpl implements GivenAggregateEventPublisherInterface, WhenAggregateEventPublisherInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $aggregateIdentifier; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $scn; |
46
|
|
|
|
47
|
|
|
private $fixture; |
48
|
|
|
|
49
|
3 |
|
function __construct(AnnotatedSagaTestFixture $fixture, $aggregateIdentifier) |
|
|
|
|
50
|
|
|
{ |
51
|
3 |
|
$this->aggregateIdentifier = $aggregateIdentifier; |
52
|
3 |
|
$this->scn = 0; |
53
|
3 |
|
$this->fixture = $fixture; |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register the given <code>events</code> as being published somewhere in the past. These events are used to |
59
|
|
|
* prepare |
60
|
|
|
* the state of Sagas listening to them. Any commands or events sent out by the saga as reaction to these events is |
61
|
|
|
* ignored. |
62
|
|
|
* |
63
|
|
|
* @param array $events The events published by the aggregate |
64
|
|
|
* @return ContinuedGivenStateInterface a reference to the fixture to support a fluent interface |
65
|
|
|
*/ |
66
|
2 |
|
public function published(array $events) |
67
|
|
|
{ |
68
|
2 |
|
$this->publish($events); |
69
|
|
|
|
70
|
2 |
|
return $this->fixture; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Register the given <code>event</code> to be published on behalf of an aggregate. Activity caused by this event |
76
|
|
|
* on |
77
|
|
|
* the CommandBus and EventBus is monitored and can be checked in the FixtureExecutionResult. |
78
|
|
|
* |
79
|
|
|
* @param mixed $event The event published by the aggregate |
80
|
|
|
* @return FixtureExecutionResultInterface a reference to the test results for the validation phase |
81
|
|
|
*/ |
82
|
3 |
|
public function publishes($event) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
3 |
|
$this->publish(array($event)); |
86
|
3 |
|
} finally { |
87
|
|
|
//FixtureResourceParameterResolverFactory.clear(); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return $this->fixture->fixtureExecutionResult; |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
private function publish(array $events) |
95
|
|
|
{ |
96
|
|
|
//DateTimeUtils.setCurrentMillisFixed(currentTime().getMillis()); |
|
|
|
|
97
|
|
|
|
98
|
|
|
try { |
99
|
3 |
|
foreach ($events as $event) { |
100
|
3 |
|
if ($event instanceof EventMessageInterface) { |
101
|
|
|
//eventMessage = (EventMessage) event; |
102
|
1 |
|
$this->fixture->sagaManager->handle( |
103
|
1 |
|
new GenericDomainEventMessage( |
104
|
1 |
|
$this->aggregateIdentifier, |
105
|
1 |
|
$this->scn++, |
106
|
1 |
|
$event->getPayload(), |
107
|
1 |
|
$event->getMetaData(), |
108
|
1 |
|
$event->getIdentifier(), |
109
|
1 |
|
$event->getTimestamp() |
110
|
1 |
|
) |
111
|
1 |
|
); |
112
|
1 |
|
} else { |
113
|
2 |
|
$this->fixture->sagaManager->handle( |
114
|
2 |
|
new GenericDomainEventMessage( |
115
|
2 |
|
$this->aggregateIdentifier, |
116
|
2 |
|
$this->scn++, |
117
|
|
|
$event |
118
|
2 |
|
) |
119
|
2 |
|
); |
120
|
|
|
} |
121
|
3 |
|
} |
122
|
3 |
|
} finally { |
123
|
|
|
//DateTimeUtils.setCurrentMillisSystem(); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
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.