InMemorySnapshotStore   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 40
wmc 3
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 2
A save() 0 10 1
1
<?php
2
3
namespace Domain\Snapshotting;
4
5
use Domain\Identity\Identity;
6
use Domain\Aggregates\AggregateRoot;
7
8
/**
9
 * Class for testing snapshot generation.
10
 *
11
 * @author Sebastiaan Hilbers <[email protected]>
12
 */
13
class InMemorySnapshotStore implements SnapshotStore
14
{
15
    /**
16
     * Snapshots by identity
17
     * @var array
18
     */
19
    private $snapshots = [];
20
21
    /**
22
     * Get the latest snapshot for given $id
23
     *
24
     * @param Identity $id
25
     * @param null $criteria
26
     * @return Snapshot
27
     */
28
    public function get(Identity $id, $criteria = null)
29
    {
30
        if (!array_key_exists((string) $id, $this->snapshots)) {
31
            return null;
32
        }
33
34
        return end($this->snapshots[(string) $id]);
35
    }
36
37
    /**
38
     * Save given snapshot
39
     *
40
     * @param AggregateRoot $root
41
     */
42
    public function save(AggregateRoot $root)
43
    {
44
        $snapshot = new Snapshot(
45
            $root,
46
            $root->getVersion(),
47
            new \DateTime
48
        );
49
50
        $this->snapshots[(string) $root->getIdentity()][] = $snapshot;
51
    }
52
}
53