|
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\EventSourcing; |
|
26
|
|
|
|
|
27
|
|
|
use Governor\Framework\Repository\LockingRepository; |
|
28
|
|
|
use Governor\Framework\Domain\AggregateRootInterface; |
|
29
|
|
|
use Governor\Framework\EventHandling\EventBusInterface; |
|
30
|
|
|
use Governor\Framework\Repository\LockManagerInterface; |
|
31
|
|
|
use Governor\Framework\EventStore\EventStoreInterface; |
|
32
|
|
|
use Governor\Framework\Repository\AggregateNotFoundException; |
|
33
|
|
|
use Governor\Framework\EventStore\EventStreamNotFoundException; |
|
34
|
|
|
use Governor\Framework\UnitOfWork\CurrentUnitOfWork; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Description of EventSourcingRepository |
|
38
|
|
|
* |
|
39
|
|
|
* @author "David Kalosi" <[email protected]> |
|
40
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
|
41
|
|
|
*/ |
|
42
|
|
|
class EventSourcingRepository extends LockingRepository |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var EventStoreInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
private $eventStore; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var AggregateFactoryInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $factory; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ConflictResolverInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
private $conflictResolver; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var EventStreamDecoratorInterface[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private $eventStreamDecorators = array(); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Creates a new EventSourcingRepository with the given parameters. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $className |
|
68
|
|
|
* @param EventBusInterface $eventBus |
|
69
|
|
|
* @param LockManagerInterface $lockManager |
|
70
|
|
|
* @param EventStoreInterface $eventStore |
|
71
|
|
|
* @param AggregateFactoryInterface $factory |
|
72
|
|
|
*/ |
|
73
|
13 |
|
public function __construct($className, EventBusInterface $eventBus, |
|
74
|
|
|
LockManagerInterface $lockManager, EventStoreInterface $eventStore, |
|
75
|
|
|
AggregateFactoryInterface $factory = null) |
|
76
|
|
|
{ |
|
77
|
13 |
|
$this->validateEventSourcedAggregate($className); |
|
78
|
|
|
|
|
79
|
13 |
|
parent::__construct($className, $eventBus, $lockManager); |
|
80
|
13 |
|
$this->eventStore = $eventStore; |
|
81
|
13 |
|
$this->factory = null === $factory ? new GenericAggregateFactory($className) : $factory; |
|
82
|
13 |
|
$this->conflictResolver = null; |
|
83
|
13 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function doDeleteWithLock(AggregateRootInterface $aggregate) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->doSaveWithLock($aggregate); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
10 |
|
protected function doLoad($id, $expectedVersion) |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
10 |
|
$events = $this->eventStore->readEvents($this->getTypeIdentifier(), |
|
94
|
10 |
|
$id); |
|
95
|
10 |
|
} catch (EventStreamNotFoundException $ex) { |
|
96
|
|
|
throw new AggregateNotFoundException($id, "The aggregate was not found", $ex); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
10 |
|
foreach ($this->eventStreamDecorators as $decorator) { |
|
100
|
|
|
$events = $decorator->decorateForRead($this->getTypeIdentifier(), |
|
101
|
|
|
$id, $events); |
|
102
|
10 |
|
} |
|
103
|
|
|
|
|
104
|
10 |
|
$aggregate = $this->factory->createAggregate($id, $events->peek()); |
|
105
|
10 |
|
$unseenEvents = array(); |
|
106
|
|
|
|
|
107
|
10 |
|
$aggregate->initializeState(new CapturingEventStream($events, |
|
108
|
10 |
|
$unseenEvents, $expectedVersion)); |
|
109
|
10 |
|
if ($aggregate->isDeleted()) { |
|
110
|
|
|
throw new AggregateDeletedException($id); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
10 |
|
CurrentUnitOfWork::get()->registerListener(new ConflictResolvingListener($aggregate, $unseenEvents, $this->conflictResolver)); |
|
114
|
|
|
|
|
115
|
10 |
|
return $aggregate; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
protected function doSaveWithLock(AggregateRootInterface $aggregate) |
|
119
|
|
|
{ |
|
120
|
7 |
|
$eventStream = $aggregate->getUncommittedEvents(); |
|
121
|
7 |
|
$iterator = new \ArrayIterator(array_reverse($this->eventStreamDecorators)); |
|
122
|
|
|
|
|
123
|
8 |
|
while ($iterator->valid()) { |
|
124
|
|
|
$eventStream = $iterator->current()->decorateForAppend($this->getTypeIdentifier(), |
|
125
|
|
|
$aggregate, $eventStream); |
|
126
|
|
|
$iterator->next(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
7 |
|
$this->eventStore->appendEvents($this->getTypeIdentifier(), $eventStream); |
|
130
|
7 |
|
} |
|
131
|
|
|
|
|
132
|
13 |
|
private function validateEventSourcedAggregate($className) |
|
133
|
|
|
{ |
|
134
|
13 |
|
$reflClass = new \ReflectionClass($className); |
|
135
|
|
|
|
|
136
|
13 |
|
if (!$reflClass->implementsInterface('Governor\Framework\EventSourcing\EventSourcedAggregateRootInterface')) { |
|
137
|
|
|
throw new \InvalidArgumentException(sprintf("EventSourcingRepository aggregates need to implements EventSourcedAggregateRootInterface, %s does not.", |
|
138
|
|
|
$className)); |
|
139
|
|
|
} |
|
140
|
13 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the type identifier of the aggregates in this repository. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
* @throws \RuntimeException |
|
147
|
|
|
*/ |
|
148
|
10 |
|
public function getTypeIdentifier() |
|
149
|
|
|
{ |
|
150
|
10 |
|
if (null === $this->factory) { |
|
151
|
|
|
throw new \RuntimeException("Either an aggregate factory must be configured (recommended), " . |
|
152
|
|
|
"or the getTypeIdentifier() method must be overridden."); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
10 |
|
return $this->factory->getTypeIdentifier(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Sets the conflict resolver to use for this repository. If not set (or <code>null</code>), the repository will |
|
160
|
|
|
* throw an exception if any unexpected changes appear in loaded aggregates. |
|
161
|
|
|
* |
|
162
|
|
|
* @param ConflictResolverInterface $conflictResolver The conflict resolver to use for this repository |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function setConflictResolver(ConflictResolverInterface $conflictResolver) |
|
165
|
|
|
{ |
|
166
|
2 |
|
$this->conflictResolver = $conflictResolver; |
|
167
|
2 |
|
} |
|
168
|
|
|
|
|
169
|
10 |
|
protected function validateOnLoad(AggregateRootInterface $object, |
|
170
|
|
|
$expectedVersion) |
|
171
|
|
|
{ |
|
172
|
10 |
|
if (null === $this->conflictResolver) { |
|
173
|
8 |
|
parent::validateOnLoad($object, $expectedVersion); |
|
174
|
6 |
|
} |
|
175
|
8 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.