Passed
Push — master ( ffdae0...f582fe )
by Thorsten
01:49
created

CommitSequence::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
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 Ds\Vector;
14
use Iterator;
15
16
final class CommitSequence implements CommitSequenceInterface
17
{
18
    /** @var Vector */
19
    private $compositeVector;
20
21
    public static function fromArray(array $commitsArray): CommitSequenceInterface
22
    {
23
        return new static(array_map(function (array $commitState) {
24
            return Commit::fromArray($commitState);
25
        }, $commitsArray));
26
    }
27
28
    public static function makeEmpty(): CommitSequenceInterface
29
    {
30
        return new self;
31
    }
32
33
    public function __construct(array $commits = [])
34
    {
35
        $this->compositeVector = (function (CommitInterface ...$commits): Vector {
36
            return new Vector($commits);
37
        })(...$commits);
38
    }
39
40
    public function push(CommitInterface $commit): CommitSequenceInterface
41
    {
42
        if (!$this->isEmpty()) {
43
            $nextRevision = $this->getHead()->getAggregateRevision()->increment();
44
            if (!$nextRevision->equals($commit->getAggregateRevision())) {
45
                throw new \Exception(sprintf(
46
                    "Trying to add unexpected revision %s to event-sequence. Expected revision is $nextRevision",
47
                    $commit->getAggregateRevision()
48
                ));
49
            }
50
        }
51
        $commitSequence = clone $this;
52
        $commitSequence->compositeVector->push($commit);
53
        return $commitSequence;
54
    }
55
56
    public function toNative(): array
57
    {
58
        return array_map(function (CommitInterface $commit): array {
59
            $nativeRep = $commit->toArray();
60
            $nativeRep["@type"] = get_class($commit);
61
            return $nativeRep;
62
        }, $this->compositeVector->toArray());
63
    }
64
65
    public function getTail(): ?CommitInterface
66
    {
67
        return $this->isEmpty() ? null : $this->compositeVector->first();
68
    }
69
70
    public function getHead(): ?CommitInterface
71
    {
72
        return $this->isEmpty() ? null : $this->compositeVector->last();
73
    }
74
75
    public function get(StreamRevision $streamRevision): ?CommitInterface
76
    {
77
        if ($this->compositeVector->offsetExists($streamRevision->toNative() - 1)) {
78
            $this->compositeVector->get($streamRevision->toNative() - 1);
79
        }
80
        return null;
81
    }
82
83
    public function getSlice(StreamRevision $start, StreamRevision $end): CommitSequenceInterface
84
    {
85
        return $this->compositeVector->reduce(
86
            function (CommitSequence $commits, CommitInterface $commit) use ($start, $end): CommitSequence {
87
                if ($commit->getStreamRevision()->isWithinRange($start, $end)) {
88
                    $commits = $commits->push($commit); /* @var CommitSequence $commits */
89
                    return $commits;
90
                }
91
                return $commits;
92
            },
93
            new static
94
        );
95
    }
96
97
    public function isEmpty(): bool
98
    {
99
        return $this->compositeVector->isEmpty();
100
    }
101
102
    public function revisionOf(CommitInterface $commit): StreamRevision
103
    {
104
        return StreamRevision::fromNative($this->compositeVector->find($commit));
105
    }
106
107
    public function getLength(): int
108
    {
109
        return $this->count();
110
    }
111
112
    public function count(): int
113
    {
114
        return $this->compositeVector->count();
115
    }
116
117
    public function getIterator(): Iterator
118
    {
119
        return $this->compositeVector->getIterator();
120
    }
121
122
    private function __clone()
123
    {
124
        $this->compositeVector = clone $this->compositeVector;
125
    }
126
}
127