|
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\AggregateRevision; |
|
14
|
|
|
use Daikon\EventSourcing\Aggregate\DomainEventSequence; |
|
15
|
|
|
use Daikon\MessageBus\Metadata\Metadata; |
|
16
|
|
|
|
|
17
|
|
|
final class Stream implements StreamInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var StreamId */ |
|
20
|
|
|
private $streamId; |
|
21
|
|
|
|
|
22
|
|
|
/** @var CommitSequence */ |
|
23
|
|
|
private $commitSequence; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $commitImplementor; |
|
27
|
|
|
|
|
28
|
2 |
|
public static function fromStreamId(StreamId $streamId, string $commitImplementor = Commit::class): StreamInterface |
|
29
|
|
|
{ |
|
30
|
2 |
|
return new static($streamId); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public static function fromArray(array $streamState): Stream |
|
34
|
|
|
{ |
|
35
|
|
|
return new static( |
|
36
|
|
|
StreamId::fromNative($streamState["commitStreamId"]), |
|
|
|
|
|
|
37
|
|
|
CommitSequence::fromArray($streamState["commitStreamSequence"]), |
|
38
|
|
|
$streamState["commitImplementor"] |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function __construct( |
|
43
|
|
|
StreamId $streamId, |
|
44
|
|
|
CommitSequence $commitSequence = null, |
|
45
|
|
|
string $commitImplementor = Commit::class |
|
46
|
|
|
) { |
|
47
|
2 |
|
$this->streamId = $streamId; |
|
48
|
2 |
|
$this->commitSequence = $commitSequence ?? new CommitSequence; |
|
49
|
2 |
|
$this->commitImplementor = $commitImplementor; |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function getStreamId(): StreamId |
|
53
|
|
|
{ |
|
54
|
2 |
|
return $this->streamId; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
public function getStreamRevision(): StreamRevision |
|
58
|
|
|
{ |
|
59
|
2 |
|
return StreamRevision::fromNative($this->commitSequence->getLength()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function getAggregateRevision(): AggregateRevision |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->commitSequence->getHead()->getAggregateRevision(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function appendEvents(DomainEventSequence $eventLog, Metadata $metadata): StreamInterface |
|
68
|
|
|
{ |
|
69
|
2 |
|
return $this->appendCommit( |
|
70
|
2 |
|
call_user_func( |
|
71
|
2 |
|
[ $this->commitImplementor, 'make' ], |
|
72
|
2 |
|
$this->streamId, |
|
73
|
2 |
|
$this->getStreamRevision()->increment(), |
|
74
|
2 |
|
$eventLog, |
|
75
|
2 |
|
$metadata |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function appendCommit(CommitInterface $commit): StreamInterface |
|
81
|
|
|
{ |
|
82
|
2 |
|
$stream = clone $this; |
|
83
|
2 |
|
$stream->commitSequence = $this->commitSequence->push($commit); |
|
84
|
2 |
|
return $stream; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getHead(): ?CommitInterface |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->commitSequence->isEmpty() ? null : $this->commitSequence->getHead(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public function getCommitRange(StreamRevision $fromRev, StreamRevision $toRev = null): CommitSequence |
|
93
|
|
|
{ |
|
94
|
2 |
|
return $this->commitSequence->getSlice($fromRev, $toRev ?? $this->getStreamRevision()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function count(): int |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->commitSequence->count(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function toNative(): array |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
"commitSequence" => $this->commitSequence->toNative(), |
|
106
|
|
|
"streamId" => $this->streamId->toNative(), |
|
107
|
|
|
"commitImplementor" => $this->commitImplementor |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getIterator(): \Iterator |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->commitSequence->getIterator(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function findCommitsSince(AggregateRevision $incomingRevision): CommitSequence |
|
117
|
|
|
{ |
|
118
|
|
|
$previousCommits = []; |
|
119
|
|
|
$prevCommit = $this->getHead(); |
|
120
|
|
|
while ($prevCommit && $incomingRevision->isLessThan($prevCommit->getAggregateRevision())) { |
|
121
|
|
|
$previousCommits[] = $prevCommit; |
|
122
|
|
|
$prevCommit = $this->commitSequence->get($prevCommit->getStreamRevision()->decrement()); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
return new CommitSequence(array_reverse($previousCommits)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.