TimeBased   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
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 38
wmc 4
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A shouldCreateSnapshot() 0 13 3
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