InMemorySnapshotStore::getStreamId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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