Passed
Push — master ( 81835c...bfb296 )
by Thorsten
03:09
created

AggregateRevision::toNative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/cqrs project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\EventSourcing\Aggregate;
12
13
use Assert\Assertion;
14
15
final class AggregateRevision
16
{
17
    private const INITIAL = 1;
18
19
    private const NONE = 0;
20
21
    /** @var int */
22
    private $revision;
23
24 14
    public static function fromNative(int $revision): AggregateRevision
25
    {
26 14
        return new self($revision);
27
    }
28
29 8
    public static function makeEmpty(): AggregateRevision
30
    {
31 8
        return new static(self::NONE);
32
    }
33
34 15
    public function toNative(): int
35
    {
36 15
        return $this->revision;
37
    }
38
39 4
    public function increment(): AggregateRevision
40
    {
41 4
        $copy = clone $this;
42 4
        $copy->revision++;
43 4
        return $copy;
44
    }
45
46 3
    public function equals(AggregateRevision $revision): bool
47
    {
48 3
        Assertion::isInstanceOf($revision, static::class);
49 3
        return $revision->toNative() === $this->revision;
50
    }
51
52 1
    public function isInitial(): bool
53
    {
54 1
        return $this->revision === self::INITIAL;
55
    }
56
57
    public function isEmpty(): bool
58
    {
59
        return $this->revision === self::NONE;
60
    }
61
62 1
    public function isWithinRange(AggregateRevision $from, AggregateRevision $to): bool
63
    {
64 1
        return $this->isGreaterThanOrEqual($from) && $this->isLessThanOrEqual($to);
65
    }
66
67 2
    public function isGreaterThanOrEqual(AggregateRevision $revision): bool
68
    {
69 2
        return $this->revision >= $revision->toNative();
70
    }
71
72 1
    public function isGreaterThan(AggregateRevision $revision): bool
73
    {
74 1
        return $this->revision > $revision->toNative();
75
    }
76
77 2
    public function isLessThanOrEqual(AggregateRevision $revision): bool
78
    {
79 2
        return $this->revision <= $revision->toNative();
80
    }
81
82 1
    public function isLessThan(AggregateRevision $revision): bool
83
    {
84 1
        return $this->revision < $revision->toNative();
85
    }
86
87 2
    public function __toString(): string
88
    {
89 2
        return (string)$this->revision;
90
    }
91
92 19
    private function __construct(int $revision)
93
    {
94 19
        $this->revision = $revision;
95 19
    }
96
}
97