-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventRecord.php
More file actions
61 lines (55 loc) · 2.06 KB
/
Copy pathEventRecord.php
File metadata and controls
61 lines (55 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace TinyBlocks\BuildingBlocks\Event;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use TinyBlocks\BuildingBlocks\Aggregate\AggregateVersion;
use TinyBlocks\BuildingBlocks\Entity\Identity;
use TinyBlocks\Time\Instant;
use TinyBlocks\Vo\ValueObject;
use TinyBlocks\Vo\ValueObjectBehavior;
final readonly class EventRecord implements ValueObject
{
use ValueObjectBehavior;
public function __construct(
public UuidInterface $id,
public DomainEvent $event,
public Revision $revision,
public EventType $eventType,
public Instant $occurredAt,
public Identity $aggregateId,
public string $aggregateType,
public AggregateVersion $aggregateVersion
) {
}
/**
* Creates an EventRecord from a domain event and its required envelope fields.
*
* @param DomainEvent $event The event being recorded.
* @param Identity $aggregateId The aggregate identity that produced the event.
* @param string $aggregateType The short class name of the aggregate.
* @param AggregateVersion $aggregateVersion The aggregate version assigned to this envelope.
* @param UuidInterface|null $id Optional explicit identifier. Defaults to a fresh UUIDv4.
* @param Instant|null $occurredAt Optional explicit occurrence timestamp. Defaults to now.
* @return EventRecord The constructed envelope.
*/
public static function of(
DomainEvent $event,
Identity $aggregateId,
string $aggregateType,
AggregateVersion $aggregateVersion,
?UuidInterface $id = null,
?Instant $occurredAt = null
): EventRecord {
return new EventRecord(
id: $id ?? Uuid::uuid4(),
event: $event,
revision: $event->revision(),
eventType: EventType::fromDomainEvent(event: $event),
occurredAt: $occurredAt ?? Instant::now(),
aggregateId: $aggregateId,
aggregateType: $aggregateType,
aggregateVersion: $aggregateVersion
);
}
}