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

EventStore::verifyEventExistsForKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Rawkode\Eidetic\EventStore;
4
5
use Rawkode\Eidetic\EventSourcing\EventSourcedEntity;
6
7
abstract class EventStore implements Serializer
8
{
9
    use EventPublisherMixin;
10
11
    // Subscriber hooks
12
    const EVENT_STORED = 'eidetic.eventstore.event_stored';
13
14
    // Implement these in your concretion
15
16
    /**
17
     * @param EventSourcedEntity $eventSourcedEntity
18
     *
19
     * @throws InvalidEventException
20
     */
21
    abstract protected function persist(EventSourcedEntity $eventSourcedEntity);
22
23
    /**
24
     * Return all event log entries for $entityIdentifier.
25
     *
26
     * @param string $entityIdentifier
27
     *
28
     * @return array
29
     */
30
    abstract protected function eventLog($entityIdentifier);
31
32
    abstract protected function startTransaction();
33
    abstract protected function abortTransaction();
34
    abstract protected function completeTransaction();
35
36
    abstract protected function countEntityEvents($entityIdentifier);
37
38
    /**
39
     * Returns the class associated with an entity identifier.
40
     *
41
     * @param string $entityIdentifier
42
     *
43
     * @return string
44
     */
45
    abstract protected function entityClass($entityIdentifier);
46
47
    /** @var array */
48
    protected $stagedEvents = [];
49
50
    /** @var Serializer */
51
    protected $serializer;
52
53
    /**
54
     * Store an EventSourcedEntity's staged events.
55
     *
56
     * @param EventSourcedEntity $eventSourcedEntity
57
     */
58 12
    public function store(EventSourcedEntity $eventSourcedEntity)
59
    {
60
        try {
61 12
            $this->startTransaction();
62 12
            $this->persist($eventSourcedEntity);
63 12
        } catch (\Exception $exception) {
64
            $this->abortTransaction();
65
            throw $exception;
66
        }
67
68 12
        $this->completeTransaction();
69 12
    }
70
71
    /**
72
     * Returns all events for $entityIdentifier.
73
     *
74
     * @param string $entityIdentifier
75
     *
76
     * @return array
77
     */
78 4
    public function retrieve($entityIdentifier)
79
    {
80 4
        $eventLog = $this->eventLog($entityIdentifier);
81
82 2
        return array_map(function ($eventLogEntry) {
83 2
            return $eventLogEntry['event'];
84 2
        }, $eventLog);
85
    }
86
87
    /**
88
     * Returns all the log entries for $entityIdentifier.
89
     *
90
     * @param string $entityIdentifier
91
     *
92
     * @return array
93
     */
94 4
    public function retrieveLog($entityIdentifier)
95
    {
96 4
        return $this->eventLog($entityIdentifier);
97
    }
98
99
    /**
100
     * @param string $entityIdentifier
101
     *
102
     * @throws NoEventsFoundForKeyException
103
     */
104 8
    protected function verifyEventExistsForKey($entityIdentifier)
105
    {
106 8
        if (0 === $this->countEntityEvents($entityIdentifier)) {
107 3
            throw new NoEventsFoundForKeyException();
108
        }
109 5
    }
110
111
    /**
112
     * @param object $object
113
     *
114
     * @return string
115
     */
116 12
    public function serialize($object)
117
    {
118 12
        if (false === is_null($this->serializer)) {
119
            return $this->serializer->serialize($object);
120
        }
121
122 12
        return base64_encode(serialize($object));
123
    }
124
125
    /**
126
     * @param string $serializedObject
127
     *
128
     * @return object
129
     */
130 6
    public function unserialize($serializedObject)
131
    {
132 6
        if (false === is_null($this->serializer)) {
133
            return $this->serializer->serialize($serializedObject);
134
        }
135
136 6
        return unserialize(base64_decode($serializedObject));
137
    }
138
}
139