TimeBased::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Domain\Snapshotting;
4
5
use Domain\Aggregates\AggregateRoot;
6
use Domain\Snapshotting\SnapshottingPolicy;
7
use Domain\Snapshotting\SnapshotStore;
8
9
/**
10
 * @author Sebastiaan Hilbers <[email protected]>
11
 */
12
class TimeBased implements SnapshottingPolicy
13
{
14
    /**
15
     * @var SnapshotStore
16
     */
17
    private $store;
18
19
    /**
20
     * @var string
21
     */
22
    private $threshold;
23
24
    public function __construct(SnapshotStore $store, $threshold = '1 hour')
25
    {
26
        $this->store = $store;
27
        $this->threshold = $threshold;
28
    }
29
30
    /**
31
     * Should a snapshot be created?
32
     *
33
     * @param AggregateRoot $root
34
     * @return bool
35
     */
36
    public function shouldCreateSnapshot(AggregateRoot $root)
37
    {
38
        if ($root->hasChanges()) {
39
            $lastSnapshot = $this->store->get($root->getIdentity());
40
            $threshold = new \DateTime(date('c', strtotime('-' . $this->threshold)));
41
42
            if ($lastSnapshot->getCreationDate() > $threshold) {
43
                return true;
44
            }
45
        }
46
47
        return false;
48
    }
49
}
50