SnapshotVersionTrigger   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 27
ccs 7
cts 7
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A snapshotMustBeCreated() 0 8 2
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