MongoSnapshotStore::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Domain\Snapshotting;
4
5
use Domain\Aggregates\AggregateRoot;
6
use Domain\Identity\Identity;
7
8
/**
9
 * @author Sebastiaan Hilbers <[email protected]>
10
 */
11
class MongoSnapshotStore implements SnapshotStore
12
{
13
    private $mongo;
14
15
    public function __construct(\MongoDb $mongo)
16
    {
17
        $this->mongo = $mongo;
18
    }
19
20
    public function get(Identity $id, $criteria = null)
21
    {
22
        $data = $this->mongo->aggregate_snapshots
23
            ->find(['identity' => (string) $id])
24
            ->sort(['creation_date' => -1])
25
            ->limit(1)
26
            ->getNext();
27
28
        $snapshot = new Snapshot(
29
            unserialize($data['aggregate']),
30
            $data['version'],
31
            $data['creation_date']
32
        );
33
34
        return $snapshot;
35
    }
36
37
    public function save(AggregateRoot $root)
38
    {
39
        return $this->mongo->aggregate_snapshots->insert([
40
            'identity'      => (string) $root->getIdentity(),
41
            'aggregate'     => serialize($root),
42
            'version'       => $root->getVersion(),
43
            'creation_date' => new \MongoDate
44
        ]);
45
    }
46
}
47