Completed
Pull Request — master (#41)
by David
03:27
created

EventStore::unserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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
    /** @var array */
37
    protected $stagedEvents = [];
38
39
    /** @var Serializer */
40
    protected $serializer;
41
42
    /**
43
     * Store an EventSourcedEntity's staged events.
44
     *
45
     * @param EventSourcedEntity $eventSourcedEntity
46
     */
47 8
    public function store(EventSourcedEntity $eventSourcedEntity)
48
    {
49
        try {
50 8
            $this->startTransaction();
51 8
            $this->persist($eventSourcedEntity);
52 8
        } catch (Exception $exception) {
0 ignored issues
show
Bug introduced by
The class Rawkode\Eidetic\EventStore\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
53
            $this->abortTransaction();
54
            throw $exception;
55
        }
56
57 8
        $this->completeTransaction();
58 8
    }
59
60
    /**
61
     * Returns all events for $entityIdentifier.
62
     *
63
     * @param string $entityIdentifier
64
     *
65
     * @return array
66
     */
67 4
    public function retrieve($entityIdentifier)
68
    {
69 4
        $eventLog = $this->eventLog($entityIdentifier);
70
71 2
        return array_map(function ($eventLogEntry) {
72 2
            return $eventLogEntry['event'];
73 2
        }, $eventLog);
74
    }
75
76
    /**
77
     * Returns all the log entries for $entityIdentifier.
78
     *
79
     * @param string $entityIdentifier
80
     *
81
     * @return array
82
     */
83 4
    public function retrieveLog($entityIdentifier)
84
    {
85 4
        return $this->eventLog($entityIdentifier);
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function stagedEvents()
92
    {
93
        return $this->stagedEvents;
94
    }
95
96
    /**
97
     * @param object $object
98
     *
99
     * @return string
100
     */
101 8
    public function serialize($object)
102
    {
103 8
        if (false === is_null($this->serializer)) {
104
            return $this->serializer->serialize($object);
105
        }
106
107 8
        return base64_encode(serialize($object));
108
    }
109
110
    /**
111
     * @param string $serializedObject
112
     *
113
     * @return object
114
     */
115 6
    public function unserialize($serializedObject)
116
    {
117 6
        if (false === is_null($this->serializer)) {
118
            return $this->serializer->serialize($serializedObject);
119
        }
120
121 6
        return unserialize(base64_decode($serializedObject));
122
    }
123
}
124