Passed
Push — v3.3 ( 1e1106...84677f )
by Masiukevich
03:38 queued 10s
created

SnapshotVersionTrigger   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
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 30
ccs 7
cts 7
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A snapshotMustBeCreated() 0 8 2
A __construct() 0 3 1
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
    /**
33
     * @param int $stepInterval
34
     */
35 8
    public function __construct($stepInterval = self::DEFAULT_VERSION_STEP)
36
    {
37 8
        $this->stepInterval = $stepInterval;
38 8
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 7
    public function snapshotMustBeCreated(Aggregate $aggregate, Snapshot $previousSnapshot = null): bool
44
    {
45 7
        if (null === $previousSnapshot)
46
        {
47 7
            return true;
48
        }
49
50 5
        return $this->stepInterval <= ($aggregate->version() - $previousSnapshot->version);
51
    }
52
}
53