|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/cqrs project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Daikon\EventSourcing\EventStore; |
|
12
|
|
|
|
|
13
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateIdInterface; |
|
14
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateRevision; |
|
15
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateRootInterface; |
|
16
|
|
|
use Daikon\EventSourcing\Aggregate\DomainEventSequence; |
|
17
|
|
|
use Daikon\MessageBus\Metadata\Metadata; |
|
18
|
|
|
|
|
19
|
|
|
final class UnitOfWork implements UnitOfWorkInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $aggregateRootType; |
|
23
|
|
|
|
|
24
|
|
|
/** @var StreamStoreInterface */ |
|
25
|
|
|
private $streamStore; |
|
26
|
|
|
|
|
27
|
|
|
/** @var StreamProcessorInterface */ |
|
28
|
|
|
private $streamProcessor; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $streamImplementor; |
|
32
|
|
|
|
|
33
|
|
|
/** @var StreamMap */ |
|
34
|
|
|
private $trackedCommitStreams; |
|
35
|
|
|
|
|
36
|
2 |
|
public function __construct( |
|
37
|
|
|
string $aggregateRootType, |
|
38
|
|
|
StreamStoreInterface $streamStore, |
|
39
|
|
|
StreamProcessorInterface $streamProcessor = null, |
|
40
|
|
|
string $streamImplementor = Stream::class |
|
41
|
|
|
) { |
|
42
|
2 |
|
$this->aggregateRootType = $aggregateRootType; |
|
43
|
2 |
|
$this->streamStore = $streamStore; |
|
44
|
2 |
|
$this->streamProcessor = $streamProcessor; |
|
45
|
2 |
|
$this->streamImplementor = $streamImplementor; |
|
46
|
2 |
|
$this->trackedCommitStreams = StreamMap::makeEmpty(); |
|
|
|
|
|
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function commit(AggregateRootInterface $aggregateRoot, Metadata $metadata): CommitSequence |
|
50
|
|
|
{ |
|
51
|
2 |
|
$stream = $this->getStream($aggregateRoot)->appendEvents($aggregateRoot->getTrackedEvents(), $metadata); |
|
52
|
2 |
|
$knownHead = $stream->getStreamRevision(); |
|
53
|
2 |
|
$result = $this->streamStore->commit($stream, $knownHead); |
|
54
|
2 |
|
if ($result instanceof StoreError) { |
|
55
|
|
|
$this->trackedCommitStreams = $this->trackedCommitStreams->register($stream); |
|
56
|
|
|
throw new \Exception("Failed to store commit-stream with stream-id: ".$stream->getStreamId()); |
|
57
|
|
|
} |
|
58
|
2 |
|
return $stream->getCommitRange($knownHead, $stream->getStreamRevision()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function checkout(AggregateIdInterface $aggregateId, AggregateRevision $revision): AggregateRootInterface |
|
62
|
|
|
{ |
|
63
|
|
|
$streamId = StreamId::fromNative($aggregateId->toNative()); |
|
64
|
|
|
$stream = $this->streamStore->checkout($streamId, $revision); |
|
|
|
|
|
|
65
|
|
|
$aggregateRoot = call_user_func( |
|
66
|
|
|
[ $this->aggregateRootType, 'reconstituteFromHistory' ], |
|
67
|
|
|
$aggregateId, |
|
68
|
|
|
$this->buildEventHistory($stream) |
|
|
|
|
|
|
69
|
|
|
); |
|
70
|
|
|
$this->trackedCommitStreams = $this->trackedCommitStreams->register($stream); |
|
71
|
|
|
return $aggregateRoot; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
private function getStream(AggregateRootInterface $aggregateRoot): StreamInterface |
|
75
|
|
|
{ |
|
76
|
2 |
|
$streamId = StreamId::fromNative((string)$aggregateRoot->getIdentifier()); |
|
77
|
2 |
|
$tailRevision = $aggregateRoot->getTrackedEvents()->getTailRevision(); |
|
78
|
2 |
|
if ($this->trackedCommitStreams->has((string)$streamId)) { |
|
79
|
|
|
$stream = $this->trackedCommitStreams->get((string)$streamId); |
|
80
|
|
|
$this->trackedCommitStreams = $this->trackedCommitStreams->unregister($stream); |
|
81
|
2 |
|
} elseif ($tailRevision->isInitial()) { |
|
82
|
2 |
|
$stream = call_user_func([ $this->streamImplementor, 'fromStreamId' ], $streamId); |
|
83
|
|
|
} else { |
|
84
|
|
|
throw new \Exception("Existing aggregate-roots must be checked out before they may be comitted."); |
|
85
|
|
|
} |
|
86
|
2 |
|
return $stream; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function buildEventHistory(StreamInterface $stream, AggregateRevision $to): DomainEventSequence |
|
90
|
|
|
{ |
|
91
|
|
|
$history = DomainEventSequence::makeEmpty(); |
|
92
|
|
|
if ($this->streamProcessor) { |
|
93
|
|
|
$stream = $this->streamProcessor->process($stream); |
|
94
|
|
|
} |
|
95
|
|
|
foreach ($stream as $commit) { |
|
96
|
|
|
if (!$to->isEmpty() && $commit->getAggregateRevision()->isGreaterThan($to)) { |
|
97
|
|
|
break; |
|
98
|
|
|
} |
|
99
|
|
|
foreach ($commit->getEventLog() as $event) { |
|
100
|
|
|
$history = $history->push($event); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
return $history; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..