Passed
Push — master ( 33ccda...6d1632 )
by Thorsten
01:57
created

AggregateRevision   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 82
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

14 Methods

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