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

InMemoryMessageRepository::persist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 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
}