Sequence   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 21.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 71
ccs 6
cts 28
cp 0.2143
rs 10
wmc 13

12 Methods

Rating   Name   Duplication   Size   Complexity  
A isInitial() 0 3 1
A isGreaterThanOrEqual() 0 3 1
A isLessThanOrEqual() 0 3 1
A isWithinRange() 0 3 2
A increment() 0 5 1
A toNative() 0 3 1
A fromNative() 0 3 1
A __construct() 0 3 1
A decrement() 0 5 1
A makeInitial() 0 3 1
A equals() 0 3 1
A __toString() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/event-sourcing 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
namespace Daikon\EventSourcing\EventStore\Stream;
10
11
use Daikon\Interop\FromNativeInterface;
12
use Daikon\Interop\ToNativeInterface;
13
14
final class Sequence implements FromNativeInterface, ToNativeInterface
15
{
16
    private const INITIAL = 1;
17
18
    private const NONE = 0;
19
20
    private int $seqNumber;
21
22
    /** @param int $seqNumber */
23 1
    public static function fromNative($seqNumber): self
24
    {
25 1
        return new self($seqNumber);
26
    }
27
28
    public static function makeInitial(): self
29
    {
30
        return new self(self::NONE);
31
    }
32
33 1
    public function toNative(): int
34
    {
35 1
        return $this->seqNumber;
36
    }
37
38
    public function increment(): self
39
    {
40
        $copy = clone $this;
41
        $copy->seqNumber++;
42
        return $copy;
43
    }
44
45
    public function decrement(): self
46
    {
47
        $copy = clone $this;
48
        $copy->seqNumber--;
49
        return $copy;
50
    }
51
52
    public function equals(Sequence $seqNumber): bool
53
    {
54
        return $this->seqNumber === $seqNumber->toNative();
55
    }
56
57
    public function isInitial(): bool
58
    {
59
        return $this->seqNumber === self::INITIAL;
60
    }
61
62
    public function isWithinRange(Sequence $from, Sequence $to): bool
63
    {
64
        return $this->isGreaterThanOrEqual($from) && $this->isLessThanOrEqual($to);
65
    }
66
67
    public function isGreaterThanOrEqual(Sequence $seqNumber): bool
68
    {
69
        return $this->seqNumber >= $seqNumber->toNative();
70
    }
71
72
    public function isLessThanOrEqual(Sequence $seqNumber): bool
73
    {
74
        return $this->seqNumber <= $seqNumber->toNative();
75
    }
76
77
    public function __toString(): string
78
    {
79
        return (string)$this->seqNumber;
80
    }
81
82 1
    private function __construct(int $seqNumber)
83
    {
84 1
        $this->seqNumber = $seqNumber;
85 1
    }
86
}
87