Completed
Push — master ( 7b9431...f9056b )
by Julián
01:23
created

AggregateVersion::getPrevious()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Aggregate;
15
16
use Gears\EventSourcing\Aggregate\Exception\AggregateException;
17
18
final class AggregateVersion
19
{
20
    /**
21
     * @var int
22
     */
23
    private $value;
24
25
    /**
26
     * Version constructor.
27
     *
28
     * @param int $value
29
     */
30
    public function __construct(int $value)
31
    {
32
        if ($value < 0) {
33
            throw new AggregateException(\sprintf('Version value should be higher than 0, %s given', $value));
34
        }
35
36
        $this->value = $value;
37
    }
38
39
    /**
40
     * Get version value.
41
     *
42
     * @return int
43
     */
44
    public function getValue(): int
45
    {
46
        return $this->value;
47
    }
48
49
    /**
50
     * Check equality.
51
     *
52
     * @param self $version
0 ignored issues
show
Documentation introduced by
Should the type for parameter $version not be \self?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
53
     *
54
     * @return bool
55
     */
56
    public function isEqualTo(self $version): bool
57
    {
58
        return $this->value === $version->getValue();
59
    }
60
61
    /**
62
     * Get next version.
63
     *
64
     * @return self
65
     */
66
    public function getNext(): self
67
    {
68
        $clone = clone $this;
69
        $clone->value = $this->value + 1;
70
71
        return $clone;
72
    }
73
74
    /**
75
     * Get previous version.
76
     *
77
     * @return AggregateVersion
78
     */
79
    public function getPrevious(): self
80
    {
81
        $clone = clone $this;
82
        $clone->value = $this->value - 1;
83
84
        return $clone;
85
    }
86
}
87