Passed
Push — master ( a68ac1...afa93b )
by Frank
03:30 queued 01:05
created

InMemoryMessageRepository::lastCommit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace EventSauce\EventSourcing;
4
5
use Generator;
6
7
class InMemoryMessageRepository implements MessageRepository
8
{
9
    /**
10
     * @var Message[]
11
     */
12
    private $messages = [];
13
14
    /**
15
     * @var Event[]
16
     */
17
    private $lastCommit = [];
18
19
    /**
20
     * @return Event[]
21
     */
22 5
    public function lastCommit(): array
23
    {
24 5
        return $this->lastCommit;
25
    }
26
27 5
    public function purgeLastCommit()
28
    {
29 5
        $this->lastCommit = [];
30 5
    }
31
32 7
    public function persist(AggregateRootId $id, Message ... $messages)
33
    {
34 7
        $this->lastCommit = [];
35
36
        /** @var Message $event */
37 7
        foreach ($messages as $message) {
38 3
            $event = $message->event();
39 3
            $this->messages[$id->toString()][] = $message;
40 3
            $this->lastCommit[] = $event;
41
        }
42 7
    }
43
44 7
    public function retrieveAll(AggregateRootId $id): Generator
45
    {
46 7
        yield from $this->messages[$id->toString()] ?? [];
47
    }
48
}