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

StreamRevision::decrement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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