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\DTO\ScalarPayloadBehaviour; |
17
|
|
|
use Gears\Event\Time\SystemTimeProvider; |
18
|
|
|
use Gears\Event\Time\TimeProvider; |
19
|
|
|
use Gears\EventSourcing\Aggregate\AggregateVersion; |
20
|
|
|
use Gears\Identity\Identity; |
21
|
|
|
use Gears\Immutability\ImmutabilityBehaviour; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract immutable aggregate event. |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractAggregateEvent implements AggregateEvent |
27
|
|
|
{ |
28
|
|
|
use ImmutabilityBehaviour, ScalarPayloadBehaviour, AggregateEventBehaviour { |
29
|
|
|
ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Prevent aggregate event direct instantiation. |
34
|
|
|
* |
35
|
|
|
* @param Identity $aggregateId |
36
|
|
|
* @param AggregateVersion $aggregateVersion |
37
|
|
|
* @param array<string, mixed> $payload |
38
|
|
|
* @param \DateTimeImmutable $createdAt |
39
|
|
|
*/ |
40
|
|
|
final protected function __construct( |
41
|
|
|
Identity $aggregateId, |
42
|
|
|
AggregateVersion $aggregateVersion, |
43
|
|
|
array $payload, |
44
|
|
|
\DateTimeImmutable $createdAt |
45
|
|
|
) { |
46
|
|
|
$this->checkImmutability(); |
47
|
|
|
|
48
|
|
|
$this->identity = $aggregateId; |
49
|
|
|
$this->version = $aggregateVersion; |
50
|
|
|
$this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC')); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$this->setPayload($payload); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Instantiate new aggregate event. |
57
|
|
|
* |
58
|
|
|
* @param Identity $aggregateId |
59
|
|
|
* @param array<string, mixed> $payload |
60
|
|
|
* @param TimeProvider|null $timeProvider |
61
|
|
|
* |
62
|
|
|
* @return mixed|self |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
final protected static function occurred(Identity $aggregateId, array $payload, ?TimeProvider $timeProvider = null) |
65
|
|
|
{ |
66
|
|
|
$timeProvider = $timeProvider ?? new SystemTimeProvider(); |
67
|
|
|
|
68
|
|
|
return new static( |
69
|
|
|
$aggregateId, |
70
|
|
|
new AggregateVersion(0), |
71
|
|
|
$payload, |
72
|
|
|
$timeProvider->getCurrentTime() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
* |
79
|
|
|
* @return mixed|self |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
final public static function reconstitute(array $payload, array $attributes = []) |
82
|
|
|
{ |
83
|
|
|
return new static( |
84
|
|
|
$attributes['aggregateId'], |
85
|
|
|
$attributes['aggregateVersion'], |
86
|
|
|
$payload, |
87
|
|
|
$attributes['createdAt'] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
* |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
|
|
final protected function getAllowedInterfaces(): array |
97
|
|
|
{ |
98
|
|
|
return [AggregateEvent::class]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.