Completed
Push — master ( 8fc86c...e62d6f )
by Frank
02:52
created

InMemoryMessageRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
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
    public function __construct()
20
    {
21
        $this->lastCommit = [];
22
    }
23
24
    /**
25
     * @return Event[]
26
     */
27
    public function lastCommit(): array
28
    {
29
        return $this->lastCommit;
30
    }
31
32
    public function purgeLastCommit()
33
    {
34
        $this->lastCommit = [];
35
    }
36
37
    public function persist(Message ... $messages)
38
    {
39
        $this->lastCommit = [];
40
41
        /** @var Message $event */
42
        foreach ($messages as $message) {
43
            $event = $message->event();
44
            $this->messages[$message->aggregateRootId()->toString()][] = $message;
45
            $this->lastCommit[] = $event;
46
        }
47
    }
48
49
    public function retrieveAll(AggregateRootId $id): Generator
50
    {
51
        yield from $this->messages[$id->toString()] ?? [];
52
    }
53
}