Test Failed
Push — master ( d032ff...5c688f )
by Mr
02:06
created

CouchDbStreamStorage::append()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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 load(
25
        StreamIdInterface $streamId,
26
        AggregateRevision $from = null,
27
        AggregateRevision $to = null
28
    ): StreamInterface {
29
        $commitSequence = $this->storageAdapter->load($streamId->toNative());
30
        return new Stream($streamId, $commitSequence);
31
    }
32
33
    public function append(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->append($identifier, $commit->toArray());
40
        }
41
        return new StorageSuccess;
42
    }
43
}
44