InMemorySnapshotStore   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 5 1
A load() 0 8 2
A getStreamId() 0 3 1
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Store\Snapshot;
15
16
use Gears\EventSourcing\Store\StoreStream;
17
18
/**
19
 * In memory Event Store implementation.
20
 */
21
final class InMemorySnapshotStore extends AbstractSnapshotStore
22
{
23
    /**
24
     * Serialized aggregate root.
25
     *
26
     * @var string[]
27
     */
28
    private $streams = [];
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function load(StoreStream $stream): ?Snapshot
34
    {
35
        $streamId = $this->getStreamId($stream);
36
        if (!isset($this->streams[$streamId])) {
37
            return null;
38
        }
39
40
        return new GenericSnapshot($this->deserializeAggregateRoot($this->streams[$streamId]));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function store(Snapshot $snapshot): void
47
    {
48
        $streamId = $this->getStreamId($snapshot->getStoreStream());
49
50
        $this->streams[$streamId] = $this->serializeAggregateRoot($snapshot->getAggregateRoot());
51
    }
52
53
    /**
54
     * Get stream identifier.
55
     *
56
     * @param StoreStream $stream
57
     *
58
     * @return string
59
     */
60
    private function getStreamId(StoreStream $stream): string
61
    {
62
        return $stream->getAggregateId()->getValue();
63
    }
64
}
65