InMemoryEventStore   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 44
wmc 3
lcom 1
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A commit() 0 10 2
A getAggregateHistoryFor() 0 12 1
1
<?php
2
3
namespace Domain\Eventing;
4
5
use Domain\Identity\Identity;
6
7
/**
8
 * @author Sebastiaan Hilbers <[email protected]>
9
 */
10
final class InMemoryEventStore implements EventStore
11
{
12
    /**
13
     * Events in memory
14
     * @var array
15
     */
16
    protected $events = [];
17
18
    /**
19
     * @param UncommittedEvents $stream
20
     * @return CommittedEvents
21
     */
22
    public function commit(UncommittedEvents $stream)
23
    {
24
        $aggregateId = $stream->first()->getAggregateIdentity();
25
26
        foreach ($stream as $event) {
27
            $this->events[(string) $aggregateId][] = $event;
28
        }
29
30
        return new CommittedEvents($aggregateId, $stream->getEvents());
31
    }
32
33
    /**
34
     * Gets the events stored as memory and wraps it in \CommittedEvents
35
     *
36
     * @param Identity $id
37
     * @param int $offset
38
     * @param null $max
39
     * @return CommittedEvents
40
     */
41
    public function getAggregateHistoryFor(Identity $id, $offset = 0, $max = null)
42
    {
43
        return new CommittedEvents(
44
            $id,
45
            array_filter(
46
                array_slice($this->events[(string)$id], $offset, $max, true),
47
                function (DomainEvent $event) use ($id) {
48
                    return $event->getAggregateIdentity()->equals($id);
49
                }
50
            )
51
        );
52
    }
53
}
54