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

CouchDbStreamStorage   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 29
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 8 1
A append() 0 10 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