1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daikon\CouchDb\Storage; |
4
|
|
|
|
5
|
|
|
use Daikon\EventSourcing\Aggregate\AggregateRevision; |
6
|
|
|
use Daikon\EventSourcing\EventStore\Commit\CommitInterface; |
7
|
|
|
use Daikon\EventSourcing\EventStore\Storage\StorageResultInterface; |
8
|
|
|
use Daikon\EventSourcing\EventStore\Storage\StorageSuccess; |
9
|
|
|
use Daikon\EventSourcing\EventStore\Storage\StreamStorageInterface; |
10
|
|
|
use Daikon\EventSourcing\EventStore\Stream\Stream; |
11
|
|
|
use Daikon\EventSourcing\EventStore\Stream\StreamIdInterface; |
12
|
|
|
use Daikon\EventSourcing\EventStore\Stream\StreamInterface; |
13
|
|
|
use Daikon\EventSourcing\EventStore\Stream\StreamRevision; |
14
|
|
|
|
15
|
|
|
final class CouchDbStreamStorage implements StreamStorageInterface |
16
|
|
|
{ |
17
|
|
|
private $storageAdapter; |
18
|
|
|
|
19
|
|
|
public function __construct(CouchDbStorageAdapter $storageAdapter) |
20
|
|
|
{ |
21
|
|
|
$this->storageAdapter = $storageAdapter; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function checkout( |
25
|
|
|
StreamIdInterface $streamId, |
26
|
|
|
AggregateRevision $from = null, |
27
|
|
|
AggregateRevision $to = null |
28
|
|
|
): StreamInterface { |
29
|
|
|
$commitSequence = $this->storageAdapter->read($streamId->toNative()); |
30
|
|
|
return new Stream($streamId, $commitSequence); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function commit(StreamInterface $stream, StreamRevision $knownHead): StorageResultInterface |
34
|
|
|
{ |
35
|
|
|
$commitSequence = $stream->getCommitRange($knownHead->increment(), $stream->getStreamRevision()); |
36
|
|
|
/** @var CommitInterface $commit */ |
37
|
|
|
foreach ($commitSequence as $commit) { |
38
|
|
|
$identifier = $stream->getStreamId()->toNative().'-'.$commit->getStreamRevision(); |
39
|
|
|
$this->storageAdapter->write($identifier, $commit->toArray()); |
40
|
|
|
} |
41
|
|
|
return new StorageSuccess; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|