SnapshotVersionTrigger::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * Event Sourcing implementation.
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcing\Snapshots\Triggers;
14
15
use ServiceBus\EventSourcing\Aggregate;
16
use ServiceBus\EventSourcing\Snapshots\Snapshot;
17
18
/**
19
 * Generation of snapshots every N versions.
20
 */
21
class SnapshotVersionTrigger implements SnapshotTrigger
22
{
23
    private const DEFAULT_VERSION_STEP = 10;
24
25
    /**
26
     * Version step interval.
27
     *
28
     * @var int
29
     */
30
    private $stepInterval;
31
32 14
    public function __construct(int $stepInterval = self::DEFAULT_VERSION_STEP)
33
    {
34 14
        $this->stepInterval = $stepInterval;
35 14
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 7
    public function snapshotMustBeCreated(Aggregate $aggregate, Snapshot $previousSnapshot = null): bool
41
    {
42 7
        if (null === $previousSnapshot)
43
        {
44 7
            return true;
45
        }
46
47 5
        return $this->stepInterval <= ($aggregate->version() - $previousSnapshot->version);
48
    }
49
}
50