Passed
Push — master ( 7f7df8...6d2aba )
by Thorsten
01:53
created

Stream::detectConflictingEvents()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 8.8571
cc 5
eloc 8
nc 5
nop 2
crap 30
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"]),
0 ignored issues
show
Compatibility introduced by
\Daikon\EventSourcing\Ev...tate['commitStreamId']) of type object<Daikon\Entity\Val...t\ValueObjectInterface> is not a sub-type of object<Daikon\EventSourcing\EventStore\StreamId>. It seems like you assume a concrete implementation of the interface Daikon\Entity\ValueObject\ValueObjectInterface to be always present.

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.

Loading history...
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());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $prevCommit is correct as $this->commitSequence->g...evision()->decrement()) (which targets Daikon\EventSourcing\Eve...e\CommitSequence::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
123
        }
124
        return new CommitSequence(array_reverse($previousCommits));
125
    }
126
}
127