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\Event; |
15
|
|
|
|
16
|
|
|
use Gears\Event\EventBehaviour; |
17
|
|
|
use Gears\Event\Exception\EventException; |
18
|
|
|
use Gears\EventSourcing\Aggregate\AggregateBehaviour; |
19
|
|
|
use Gears\EventSourcing\Aggregate\AggregateVersion; |
20
|
|
|
use Gears\Identity\Identity; |
21
|
|
|
|
22
|
|
|
use function DeepCopy\deep_copy; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Aggregate event behaviour. |
26
|
|
|
*/ |
27
|
|
|
trait AggregateEventBehaviour |
28
|
|
|
{ |
29
|
|
|
use EventBehaviour, AggregateBehaviour { |
30
|
|
|
AggregateBehaviour::getIdentity as private; |
31
|
|
|
AggregateBehaviour::getVersion as private; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get aggregate id. |
36
|
|
|
* |
37
|
|
|
* @return Identity |
38
|
|
|
*/ |
39
|
|
|
final public function getAggregateId(): Identity |
40
|
|
|
{ |
41
|
|
|
return $this->identity; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get aggregate version. |
46
|
|
|
* |
47
|
|
|
* @return AggregateVersion |
48
|
|
|
*/ |
49
|
|
|
final public function getAggregateVersion(): AggregateVersion |
50
|
|
|
{ |
51
|
|
|
return $this->version; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get event with new aggregate version. |
56
|
|
|
* |
57
|
|
|
* @param AggregateVersion $aggregateVersion |
58
|
|
|
* |
59
|
|
|
* @throws \Gears\Event\Exception\EventException |
60
|
|
|
* |
61
|
|
|
* @return mixed|self |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
final public function withAggregateVersion(AggregateVersion $aggregateVersion) |
64
|
|
|
{ |
65
|
|
|
if (!$this->version->isEqualTo(new AggregateVersion(0))) { |
66
|
|
|
throw new EventException(\sprintf( |
67
|
|
|
'Only new events can get a new version, event %s already at version %s', |
68
|
|
|
\get_class($this), |
69
|
|
|
$this->version->getValue() |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($aggregateVersion->isEqualTo(new AggregateVersion(0))) { |
|
|
|
|
74
|
|
|
throw new EventException(\sprintf( |
75
|
|
|
'Aggregate events can not get version 0 set, version 0 given to event %s', |
76
|
|
|
\get_class($this) |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* @var self $self */ |
81
|
|
|
$self = deep_copy($this); |
82
|
|
|
$self->version = $aggregateVersion; |
83
|
|
|
|
84
|
|
|
return $self; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.