IntervalBased::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Domain\Snapshotting\Policy;
4
5
use Domain\Aggregates\AggregateRoot;
6
use Domain\Snapshotting\SnapshottingPolicy;
7
8
/**
9
 * @author Sebastiaan Hilbers <[email protected]>
10
 */
11
class IntervalBased implements SnapshottingPolicy
12
{
13
    private $interval;
14
15
    public function __construct($interval = 3)
16
    {
17
        $this->interval = $interval;
18
    }
19
20
    public function shouldCreateSnapshot(AggregateRoot $root)
21
    {
22
        if ($root->hasChanges()) {
23
            $lastEvent = $root->getChanges()[count($root->getChanges()) -1];
24
            $lastVersion = $lastEvent->getVersion();
25
26
            for ($i = $root->getVersion(); $i <= $lastVersion; $i++) {
27
                if ($i % $this->interval == 0) {
28
                    return true;
29
                }
30
            }
31
        }
32
33
        return false;
34
    }
35
}
36