Passed
Push — master ( 33b14d...e7440c )
by Thorsten
01:45
created

StreamRevision::increment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
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\EventStore;
12
13
use Assert\Assertion;
14
use Daikon\Entity\ValueObject\ValueObjectInterface;
15
16
final class StreamRevision implements ValueObjectInterface
17
{
18
    private const INITIAL = 1;
19
20
    private const NONE = 0;
21
22
    /** @var int */
23
    private $revision;
24
25 2
    public static function fromNative($revision): ValueObjectInterface
26
    {
27 2
        return new self($revision);
28
    }
29
30
    public static function makeEmpty(): ValueObjectInterface
31
    {
32
        return new static(self::NONE);
33
    }
34
35 2
    public function toNative(): int
36
    {
37 2
        return $this->revision;
38
    }
39
40 2
    public function increment(): StreamRevision
41
    {
42 2
        $copy = clone $this;
43 2
        $copy->revision++;
44 2
        return $copy;
45
    }
46
47
    public function decrement(): StreamRevision
48
    {
49
        $copy = clone $this;
50
        $copy->revision--;
51
        return $copy;
52
    }
53
54
    public function equals(ValueObjectInterface $otherValue): bool
55
    {
56
        Assertion::isInstanceOf($otherValue, static::class);
57
        return $otherValue->toNative() === $this->revision;
58
    }
59
60
    public function isEmpty(): bool
61
    {
62
        return $this->revision === self::NONE;
63
    }
64
65
    public function isInitial(): bool
66
    {
67
        return $this->revision === self::INITIAL;
68
    }
69
70 2
    public function isWithinRange(StreamRevision $from, StreamRevision $to)
71
    {
72 2
        return $this->isGreaterThanOrEqual($from) && $this->isLessThanOrEqual($to);
73
    }
74
75 2
    public function isGreaterThanOrEqual(StreamRevision $revision)
76
    {
77 2
        return $this->revision >= $revision->toNative();
78
    }
79
80 2
    public function isLessThanOrEqual(StreamRevision $revision)
81
    {
82 2
        return $this->revision <= $revision->toNative();
83
    }
84
85
    public function __toString(): string
86
    {
87
        return (string)$this->revision;
88
    }
89
90 2
    private function __construct(int $revision)
91
    {
92 2
        $this->revision = $revision;
93 2
    }
94
}
95