Completed
Push — master ( 19228d...34c2b4 )
by David
03:14
created

InMemoryEventStore::eventLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rawkode\Eidetic\EventStore\InMemoryEventStore;
4
5
use Rawkode\Eidetic\EventStore\InvalidEventException;
6
use Rawkode\Eidetic\EventStore\EventStore;
7
use Rawkode\Eidetic\EventStore\NoEventsFoundForKeyException;
8
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity;
9
10
final class InMemoryEventStore extends EventStore
11
{
12
    /**
13
     * @var array
14
     */
15
    private $events = [];
16
17
    /**
18
     * @var array
19
     */
20
    private $transactionBackup = [];
21
22
    /**
23
     * @param EventSourcedEntity $eventSourcedEntity
24
     *
25
     * @throws InvalidEventException
26
     */
27 4
    protected function persist(EventSourcedEntity $eventSourcedEntity)
28
    {
29 4
        if (false === array_key_exists($eventSourcedEntity->identifier(), $this->events)) {
30 4
            $this->events[$eventSourcedEntity->identifier()] = [];
31 4
        }
32
33 4
        foreach ($eventSourcedEntity->stagedEvents() as $event) {
34 4
            $this->events[$eventSourcedEntity->identifier()][] = [
35 4
                'entity_identifier' => $eventSourcedEntity->identifier(),
36 4
                'serial_number' => count($this->events[$eventSourcedEntity->identifier()]) + 1,
37 4
                'entity_class' => get_class($eventSourcedEntity),
38 4
                'recorded_at' => new \DateTime('now', new \DateTimeZone('UTC')),
39 4
                'event_class' => get_class($event),
40 4
                'event' => $this->serialize($event),
41
            ];
42
43 4
            array_push($this->stagedEvents, $event);
44 4
        }
45 4
    }
46
47
    /**
48
     * @param string $entityIdentifier
49
     *
50
     * @throws NoEventsFoundForKeyException
51
     *
52
     * @return array
53
     */
54 4
    protected function eventLog($entityIdentifier)
55
    {
56 4
        $this->verifyEventExistsForKey($entityIdentifier);
57
58 3
        return array_map(function ($eventLogEntry) {
59 3
            $eventLogEntry['event'] = $this->unserialize($eventLogEntry['event']);
60
61 3
            return $eventLogEntry;
62 3
        }, $this->events[$entityIdentifier]);
63
    }
64
65
    /**
66
     */
67 4
    protected function startTransaction()
68
    {
69 4
        $this->transactionBackup = $this->events;
70 4
        $this->stagedEvents = [];
71 4
    }
72
73
    /**
74
     */
75
    protected function abortTransaction()
76
    {
77
        $this->events = $this->transactionBackup;
78
        $this->stagedEvents = [];
79
    }
80
81
    /**
82
     */
83 4
    protected function completeTransaction()
84
    {
85 4
        $this->transactionBackup = [];
86
87 4
        foreach ($this->stagedEvents as $event) {
88 4
            $this->publish(self::EVENT_STORED, $event);
89 4
        }
90
91 4
        $this->stagedEvents = [];
92 4
    }
93
94
    /**
95
     * @param string $entityIdentifier
96
     *
97
     * @throws NoEventsFoundForKeyException
98
     */
99 4
    protected function verifyEventExistsForKey($entityIdentifier)
100
    {
101 4
        if (false === array_key_exists($entityIdentifier, $this->events)) {
102 1
            throw new NoEventsFoundForKeyException();
103
        }
104 3
    }
105
}
106