|
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\Saga\Annotation; |
|
26
|
|
|
|
|
27
|
|
|
use Ramsey\Uuid\Uuid; |
|
28
|
|
|
use JMS\Serializer\Annotation\Type; |
|
29
|
|
|
use JMS\Serializer\Annotation\Exclude; |
|
30
|
|
|
use JMS\Serializer\Annotation\PostDeserialize; |
|
31
|
|
|
use Governor\Framework\Domain\EventMessageInterface; |
|
32
|
|
|
use Governor\Framework\Saga\SagaInterface; |
|
33
|
|
|
use Governor\Framework\Saga\AssociationValue; |
|
34
|
|
|
use Governor\Framework\Saga\Annotation\AssociationValuesImpl; |
|
35
|
|
|
use Governor\Framework\Saga\AssociationValuesInterface; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Implementation of the {@link Saga interface} that delegates incoming events to SagaEventHandler annotated methods. |
|
39
|
|
|
* |
|
40
|
|
|
* @author "David Kalosi" <[email protected]> |
|
41
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract class AbstractAnnotatedSaga implements SagaInterface |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Type ("Governor\Framework\Saga\Annotation\AssociationValuesImpl") |
|
48
|
|
|
* @var AssociationValuesInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $associationValues; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @Type ("string") |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $identifier; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Type ("boolean") |
|
60
|
|
|
* @var boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
private $isActive = true; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @Exclude |
|
66
|
|
|
* @var SagaMethodMessageHandlerInspector |
|
67
|
|
|
*/ |
|
68
|
|
|
private $inspector; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Initialize the saga. If an identifier is provided it will be used, otherwise a random UUID will be generated. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $identifier |
|
74
|
|
|
*/ |
|
75
|
36 |
|
public function __construct($identifier = null) |
|
76
|
|
|
{ |
|
77
|
36 |
|
$this->identifier = (null === $identifier) ? Uuid::uuid1()->toString() : $identifier; |
|
78
|
36 |
|
$this->associationValues = new AssociationValuesImpl(); |
|
79
|
36 |
|
$this->inspector = new SagaMethodMessageHandlerInspector($this); |
|
80
|
36 |
|
$this->associationValues->add(new AssociationValue('sagaIdentifier', |
|
81
|
36 |
|
$this->identifier)); |
|
82
|
36 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @PostDeserialize |
|
86
|
|
|
*/ |
|
87
|
14 |
|
public function postDeserialize() |
|
88
|
|
|
{ |
|
89
|
14 |
|
$this->inspector = new SagaMethodMessageHandlerInspector($this); |
|
90
|
14 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
30 |
|
public function getSagaIdentifier() |
|
96
|
|
|
{ |
|
97
|
30 |
|
return $this->identifier; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return AssociationValuesInterface |
|
102
|
|
|
*/ |
|
103
|
35 |
|
public function getAssociationValues() |
|
104
|
|
|
{ |
|
105
|
35 |
|
return $this->associationValues; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
16 |
|
public final function handle(EventMessageInterface $event) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
16 |
|
if ($this->isActive()) { |
|
111
|
|
|
// find and invoke handler |
|
112
|
16 |
|
$handler = $this->inspector->findHandlerMethod($this, $event); |
|
113
|
16 |
|
$handler->invoke($this, $event); |
|
114
|
|
|
|
|
115
|
16 |
|
if ($handler->isEndingHandler()) { |
|
116
|
5 |
|
$this->end(); |
|
117
|
5 |
|
} |
|
118
|
16 |
|
} |
|
119
|
16 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
35 |
|
public function isActive() |
|
125
|
|
|
{ |
|
126
|
35 |
|
return $this->isActive; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Marks the saga as ended. Ended saga's may be cleaned up by the repository when they are committed. |
|
131
|
|
|
*/ |
|
132
|
10 |
|
protected function end() |
|
133
|
|
|
{ |
|
134
|
10 |
|
$this->isActive = false; |
|
135
|
10 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Registers a AssociationValue with the given saga. When the saga is committed, it can be found using the |
|
139
|
|
|
* registered property. |
|
140
|
|
|
* |
|
141
|
|
|
* @param AssociationValue $associationValue The value to associate this saga with. |
|
142
|
|
|
*/ |
|
143
|
20 |
|
public function associateWith(AssociationValue $associationValue) |
|
144
|
|
|
{ |
|
145
|
20 |
|
$this->associationValues->add($associationValue); |
|
146
|
20 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Removes the given association from this Saga. When the saga is committed, it can no longer be found using the |
|
150
|
|
|
* given association. If the given property wasn't registered with the saga, nothing happens. |
|
151
|
|
|
* |
|
152
|
|
|
* @param AssociationValue $associationValue the association value to remove from the saga. |
|
153
|
|
|
*/ |
|
154
|
3 |
|
public function removeAssociationWith(AssociationValue $associationValue) |
|
155
|
|
|
{ |
|
156
|
3 |
|
$this->associationValues->remove($associationValue); |
|
157
|
3 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|