Completed
Pull Request — master (#41)
by David
12:31 queued 09:06
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
     * @param object             $event
0 ignored issues
show
Bug introduced by
There is no parameter named $event. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     *
26
     * @throws InvalidEventException
27
     */
28 4
    protected function persist(EventSourcedEntity $eventSourcedEntity)
29
    {
30 4
        if (false === array_key_exists($eventSourcedEntity->identifier(), $this->events)) {
31 4
            $this->events[$eventSourcedEntity->identifier()] = [];
32 4
        }
33
34
        array_map(function ($event) use ($eventSourcedEntity) {
35 4
            $this->events[$eventSourcedEntity->identifier()][] = [
36 4
                'entity_identifier' => $eventSourcedEntity->identifier(),
37 4
                'serial_number' => count($this->events[$eventSourcedEntity->identifier()]) + 1,
38 4
                'entity_class' => get_class($eventSourcedEntity),
39 4
                'recorded_at' => new \DateTime('now', new \DateTimeZone('UTC')),
40 4
                'event_class' => get_class($event),
41 4
                'event' => $this->serialize($event),
42
            ];
43
44 4
            array_push($this->stagedEvents, $event);
0 ignored issues
show
Bug introduced by
The property stagedEvents cannot be accessed from this context as it is declared private in class Rawkode\Eidetic\EventStore\EventStore.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
45 4
        }, $eventSourcedEntity->stagedEvents());
46 4
    }
47
48
    /**
49
     * @param string $entityIdentifier
50
     *
51
     * @throws NoEventsFoundForKeyException
52
     *
53
     * @return array
54
     */
55 4
    protected function eventLog($entityIdentifier)
56
    {
57 4
        $this->verifyEventExistsForKey($entityIdentifier);
58
59 3
        return array_map(function ($eventLogEntry) {
60 3
            $eventLogEntry['event'] = $this->unserialize($eventLogEntry['event']);
61
62 3
            return $eventLogEntry;
63 3
        }, $this->events[$entityIdentifier]);
64
    }
65
66
    /**
67
     */
68 4
    protected function startTransaction()
69
    {
70 4
        $this->transactionBackup = $this->events;
71 4
        $this->stagedEvents = [];
0 ignored issues
show
Bug introduced by
The property stagedEvents cannot be accessed from this context as it is declared private in class Rawkode\Eidetic\EventStore\EventStore.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
72 4
    }
73
74
    /**
75
     */
76
    protected function abortTransaction()
77
    {
78
        $this->events = $this->transactionBackup;
79
        $this->stagedEvents = [];
0 ignored issues
show
Bug introduced by
The property stagedEvents cannot be accessed from this context as it is declared private in class Rawkode\Eidetic\EventStore\EventStore.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
80
    }
81
82
    /**
83
     */
84 4
    protected function completeTransaction()
85
    {
86 4
        $this->transactionBackup = [];
87
88 4
        foreach ($this->stagedEvents as $event) {
0 ignored issues
show
Bug introduced by
The property stagedEvents cannot be accessed from this context as it is declared private in class Rawkode\Eidetic\EventStore\EventStore.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
89 4
            $this->publish(self::EVENT_STORED, $event);
90 4
        }
91
92 4
        $this->stagedEvents = [];
0 ignored issues
show
Bug introduced by
The property stagedEvents cannot be accessed from this context as it is declared private in class Rawkode\Eidetic\EventStore\EventStore.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
93 4
    }
94
95
    /**
96
     * @param string $entityIdentifier
97
     *
98
     * @throws NoEventsFoundForKeyException
99
     */
100 4
    protected function verifyEventExistsForKey($entityIdentifier)
101
    {
102 4
        if (false === array_key_exists($entityIdentifier, $this->events)) {
103 1
            throw new NoEventsFoundForKeyException();
104
        }
105 3
    }
106
}
107