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\Exception\EventException; |
17
|
|
|
use Gears\EventSourcing\Aggregate\AggregateBehaviour; |
18
|
|
|
use Gears\EventSourcing\Aggregate\AggregateVersion; |
19
|
|
|
use Gears\Identity\Identity; |
20
|
|
|
|
21
|
|
|
use function DeepCopy\deep_copy; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract immutable aggregate event. |
25
|
|
|
*/ |
26
|
|
|
trait AggregateEventBehaviour |
27
|
|
|
{ |
28
|
|
|
use AggregateBehaviour { |
29
|
|
|
AggregateBehaviour::getIdentity as private; |
30
|
|
|
AggregateBehaviour::getVersion as private; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \DateTimeImmutable |
35
|
|
|
*/ |
36
|
|
|
private $createdAt; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
final public function getAggregateId(): Identity |
42
|
|
|
{ |
43
|
|
|
return $this->identity; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
final public function getAggregateVersion(): AggregateVersion |
50
|
|
|
{ |
51
|
|
|
return $this->version; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
final public function withAggregateVersion(AggregateVersion $aggregateVersion) |
58
|
|
|
{ |
59
|
|
|
if (!$this->version->isEqualTo(new AggregateVersion(0))) { |
60
|
|
|
throw new EventException(\sprintf( |
61
|
|
|
'Only new events can get a new version, event %s already at version %s', |
62
|
|
|
\get_class($this), |
63
|
|
|
$this->version->getValue() |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($aggregateVersion->isEqualTo(new AggregateVersion(0))) { |
|
|
|
|
68
|
|
|
throw new EventException(\sprintf( |
69
|
|
|
'Aggregate events can not get version 0 set, version 0 given to event %s', |
70
|
|
|
\get_class($this) |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/* @var self $self */ |
75
|
|
|
$self = deep_copy($this); |
76
|
|
|
$self->version = $aggregateVersion; |
77
|
|
|
|
78
|
|
|
return $self; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
final public function getCreatedAt(): \DateTimeImmutable |
85
|
|
|
{ |
86
|
|
|
return $this->createdAt; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: