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

InMemoryMessageRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A lastCommit() 0 4 1
A purgeLastCommit() 0 4 1
A persist() 0 11 2
A retrieveAll() 0 4 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
}