Completed
Push — master ( ba82d8...306a50 )
by Daniel
03:11
created

InMemoryEventStore::readAllStreams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace DDDominio\EventSourcing\EventStore;
4
5
use DDDominio\EventSourcing\Common\EventStream;
6
use DDDominio\EventSourcing\Common\EventStreamInterface;
7
use DDDominio\EventSourcing\Serialization\SerializerInterface;
8
use DDDominio\EventSourcing\Versioning\EventUpgrader;
9
use DDDominio\EventSourcing\Versioning\Version;
10
11
class InMemoryEventStore extends AbstractEventStore
12
{
13
    /**
14
     * @var StoredEventStream[]
15
     */
16
    private $streams;
17
18
    /**
19
     * @param SerializerInterface $serializer
20
     * @param EventUpgrader $eventUpgrader
21
     * @param StoredEventStream[] $streams
22
     */
23 29
    public function __construct(
24
        $serializer,
25
        $eventUpgrader,
26
        array $streams = []
27
    ) {
28 29
        parent::__construct($serializer, $eventUpgrader);
29 29
        $this->streams = $streams;
30 29
    }
31
32
    /**
33
     * @param string $streamId
34
     * @param StoredEvent[] $storedEvents
35
     * @param int $expectedVersion
36
     */
37 13
    protected function appendStoredEvents($streamId, $storedEvents, $expectedVersion)
38
    {
39 13
        if ($this->streamExists($streamId)) {
40 4
            $this->streams[$streamId] = $this->streams[$streamId]->append($storedEvents);
41
        } else {
42 10
            $this->streams[$streamId] = new StoredEventStream($streamId, $storedEvents);
43
        }
44 13
    }
45
46
    /**
47
     * @param string $streamId
48
     * @return EventStreamInterface
49
     */
50 18
    public function readFullStream($streamId)
51
    {
52 18
        if ($this->streamExists($streamId)) {
53 14
            return $this->domainEventStreamFromStoredEvents(
54 14
                $this->streams[$streamId]->events()
0 ignored issues
show
Documentation introduced by
$this->streams[$streamId]->events() is of type array<integer,object<DDD...Common\EventInterface>>, but the function expects a array<integer,object<DDD...ventStore\StoredEvent>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
            );
56
        } else {
57 4
            return EventStream::buildEmpty();
58
        }
59
    }
60
61
    /**
62
     * @param string $streamId
63
     * @param int $start
64
     * @param int $count
65
     * @return EventStreamInterface
66
     */
67 4
    public function readStreamEventsForward($streamId, $start = 1, $count = null)
68
    {
69 4
        if (!$this->streamExists($streamId)) {
70
            return EventStream::buildEmpty();
71
        }
72
73 4
        $storedEvents = $this->streams[$streamId]->events();
74
75 4
        if (isset($count)) {
76 1
            $filteredStoredEvents = array_splice($storedEvents, $start - 1, $count);
77
        } else {
78 3
            $filteredStoredEvents = array_splice($storedEvents, $start - 1);
79
        }
80 4
        return $this->domainEventStreamFromStoredEvents($filteredStoredEvents);
0 ignored issues
show
Documentation introduced by
$filteredStoredEvents is of type array<integer,object<DDD...Common\EventInterface>>, but the function expects a array<integer,object<DDD...ventStore\StoredEvent>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
    }
82
83
    /**
84
     * @return EventStreamInterface[]
85
     */
86 2
    public function readAllStreams()
87
    {
88 2
        $allStreams = [];
89 2
        foreach ($this->streams as $stream) {
90 1
            $allStreams[] = $this->domainEventStreamFromStoredEvents($stream->events());
0 ignored issues
show
Documentation introduced by
$stream->events() is of type array<integer,object<DDD...Common\EventInterface>>, but the function expects a array<integer,object<DDD...ventStore\StoredEvent>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        }
92 2
        return $allStreams;
93
    }
94
95
    /**
96
     * @return EventStreamInterface
97
     */
98 1
    public function readAllEvents()
99
    {
100 1
        $allEventsStream = EventStream::buildEmpty();
101 1
        foreach ($this->streams as $stream) {
102 1
            $streamEvents = $this->domainEventStreamFromStoredEvents($stream->events());
0 ignored issues
show
Documentation introduced by
$stream->events() is of type array<integer,object<DDD...Common\EventInterface>>, but the function expects a array<integer,object<DDD...ventStore\StoredEvent>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103 1
            $allEventsStream = $allEventsStream->append($streamEvents->events());
104
        }
105 1
        return $allEventsStream;
106
    }
107
108
    /**
109
     * @param string $streamId
110
     * @return int
111
     */
112 4
    protected function streamVersion($streamId)
113
    {
114 4
        return $this->streamExists($streamId) ?
115 4
            count($this->streams[$streamId]->events()) : 0;
116
    }
117
118
    /**
119
     * @param string $type
120
     * @param Version $version
121
     * @return EventStreamInterface
122
     */
123 1
    protected function readStoredEventsOfTypeAndVersion($type, $version)
124
    {
125 1
        $storedEvents = [];
126 1
        foreach ($this->streams as $stream) {
127
            /** @var StoredEvent $event */
128 1
            foreach ($stream as $event) {
129 1
                if ($event->type() === $type && $event->version()->equalTo($version)) {
130 1
                    $storedEvents[] = $event;
131
                }
132
            }
133
        }
134 1
        return new EventStream($storedEvents);
135
    }
136
137
    /**
138
     * @param string $streamId
139
     * @return bool
140
     */
141 28
    protected function streamExists($streamId)
142
    {
143 28
        return isset($this->streams[$streamId]);
144
    }
145
}
146