AggregateVersion::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\AggregateVersionException;
17
18
final class AggregateVersion implements \Serializable
19
{
20
    /**
21
     * @var int
22
     */
23
    private $value;
24
25
    /**
26
     * Version constructor.
27
     *
28
     * @param int $value
29
     *
30
     * @throws AggregateVersionException
31
     */
32
    public function __construct(int $value)
33
    {
34
        if ($value < 0) {
35
            throw new AggregateVersionException(\sprintf('Version value should be higher than 0, "%s" given', $value));
36
        }
37
38
        $this->value = $value;
39
    }
40
41
    /**
42
     * Get version value.
43
     *
44
     * @return int
45
     */
46
    public function getValue(): int
47
    {
48
        return $this->value;
49
    }
50
51
    /**
52
     * Check equality.
53
     *
54
     * @param self $version
55
     *
56
     * @return bool
57
     */
58
    public function isEqualTo(self $version): bool
59
    {
60
        return $this->value === $version->getValue();
61
    }
62
63
    /**
64
     * Get next version.
65
     *
66
     * @return self
67
     */
68
    public function getNext(): self
69
    {
70
        $clone = clone $this;
71
        $clone->value = $this->value + 1;
72
73
        return $clone;
74
    }
75
76
    /**
77
     * Get previous version.
78
     *
79
     * @throws AggregateVersionException
80
     *
81
     * @return AggregateVersion
82
     */
83
    public function getPrevious(): self
84
    {
85
        if ($this->value === 0) {
86
            throw new AggregateVersionException('Version value cannot be lowered below 0');
87
        }
88
89
        $clone = clone $this;
90
        $clone->value = $this->value - 1;
91
92
        return $clone;
93
    }
94
95
    /**
96
     * @return array<string, mixed>
97
     */
98
    public function __serialize(): array
99
    {
100
        return ['value' => $this->value];
101
    }
102
103
    /**
104
     * @param array<string, mixed> $data
105
     */
106
    public function __unserialize(array $data): void
107
    {
108
        $this->value = $data['value'];
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function serialize(): string
115
    {
116
        return \serialize($this->value);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     *
122
     * @param mixed $serialized
123
     */
124
    public function unserialize($serialized): void
125
    {
126
        $this->value = \unserialize($serialized, ['allowed_classes' => false]);
127
    }
128
}
129