Completed
Pull Request — master (#42)
by David
02:29
created

InMemoryEventStore::entityClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
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 5
    protected function persist(EventSourcedEntity $eventSourcedEntity)
28
    {
29 5
        if (false === array_key_exists($eventSourcedEntity->identifier(), $this->events)) {
30 5
            $this->events[$eventSourcedEntity->identifier()] = [];
31 5
        }
32
33 5
        foreach ($eventSourcedEntity->stagedEvents() as $event) {
34 5
            $this->events[$eventSourcedEntity->identifier()][] = [
35 5
                'entity_identifier' => $eventSourcedEntity->identifier(),
36 5
                'serial_number' => count($this->events[$eventSourcedEntity->identifier()]) + 1,
37 5
                'entity_class' => get_class($eventSourcedEntity),
38 5
                'recorded_at' => new \DateTime('now', new \DateTimeZone('UTC')),
39 5
                'event_class' => get_class($event),
40 5
                'event' => $this->serialize($event),
41
            ];
42
43 5
            array_push($this->stagedEvents, $event);
44 5
        }
45 5
    }
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 5
    protected function startTransaction()
68
    {
69 5
        $this->transactionBackup = $this->events;
70 5
        $this->stagedEvents = [];
71 5
    }
72
73
    /**
74
     */
75
    protected function abortTransaction()
76
    {
77
        $this->events = $this->transactionBackup;
78
        $this->stagedEvents = [];
79
    }
80
81
    /**
82
     */
83 5
    protected function completeTransaction()
84
    {
85 5
        $this->transactionBackup = [];
86
87 5
        foreach ($this->stagedEvents as $event) {
88 5
            $this->publish(self::EVENT_STORED, $event);
89 5
        }
90
91 5
        $this->stagedEvents = [];
92 5
    }
93
94
    /**
95
     * @param string $entityIdentifier
96
     *
97
     * @throws NoEventsFoundForKeyException
98
     *
99
     * @return int
100
     */
101 5
    public function countEntityEvents($entityIdentifier)
102
    {
103 5
        if (false === array_key_exists($entityIdentifier, $this->events)) {
104 2
            return 0;
105
        }
106
107 5
        return count($this->events[$entityIdentifier]);
108
    }
109
110
    /**
111
     * @param string $entityIdentifier
112
     *
113
     * @throws NoEventsFoundForKeyException
114
     *
115
     * @return string
116
     */
117
    protected function entityClass($entityIdentifier)
118
    {
119
        $this->verifyEventExistsForKey($entityIdentifier);
120
121
        return $this->events[$entityIdentifier][0]['entity_class'];
122
    }
123
}
124