IntervalBased   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
wmc 5
lcom 1
cbo 1
rs 10

2 Methods

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