Passed
Push — master ( 6d1632...ddd5df )
by Thorsten
01:48
created

StreamRevision   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 21.88%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 79
ccs 7
cts 32
cp 0.2188
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 4 1
A makeEmpty() 0 4 1
A toNative() 0 4 1
A decrement() 0 6 1
A equals() 0 5 1
A isEmpty() 0 4 1
A isInitial() 0 4 1
A __toString() 0 4 1
A __construct() 0 4 1
A increment() 0 6 1
A isWithinRange() 0 4 2
A isGreaterThanOrEqual() 0 4 1
A isLessThanOrEqual() 0 4 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 1
    public static function fromNative($revision): ValueObjectInterface
26
    {
27 1
        return new self($revision);
28
    }
29
30
    public static function makeEmpty(): ValueObjectInterface
31
    {
32
        return new static(self::NONE);
33
    }
34
35 1
    public function toNative(): int
36
    {
37 1
        return $this->revision;
38
    }
39
40
    public function increment(): StreamRevision
41
    {
42
        $copy = clone $this;
43
        $copy->revision++;
44
        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
    public function isWithinRange(StreamRevision $from, StreamRevision $to)
71
    {
72
        return $this->isGreaterThanOrEqual($from) && $this->isLessThanOrEqual($to);
73
    }
74
75
    public function isGreaterThanOrEqual(StreamRevision $revision)
76
    {
77
        return $this->revision >= $revision->toNative();
78
    }
79
80
    public function isLessThanOrEqual(StreamRevision $revision)
81
    {
82
        return $this->revision <= $revision->toNative();
83
    }
84
85
    public function __toString(): string
86
    {
87
        return (string)$this->revision;
88
    }
89
90 1
    private function __construct(int $revision)
91
    {
92 1
        $this->revision = $revision;
93 1
    }
94
}
95