-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceable.php
More file actions
95 lines (90 loc) · 3.3 KB
/
Sourceable.php
File metadata and controls
95 lines (90 loc) · 3.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace ComplexHeart\Domain\Contracts\Events;
/**
* Interface Sourceable
*
* Extends events with explicit aggregate context for event sourcing.
* Use when events are sourced from aggregates and stored in event stores.
*
* This interface provides explicit aggregate identification and versioning,
* eliminating the need for convention-based extraction from payload.
*
* Enables:
* - Event sourcing and event stores
* - Aggregate reconstruction from events
* - Schema evolution and versioning
* - Event store stream organization
*
* @author Unay Santisteban <usantisteban@othercode.io>
*/
interface Sourceable
{
/**
* The unique identifier of the aggregate that emitted this event.
*
* This method provides explicit aggregate identification rather than
* relying on convention-based extraction from the payload, offering:
* - Type safety (guaranteed string, not array lookup)
* - IDE support (autocomplete, type hints, refactoring)
* - Contract enforcement at compile time
* - Clear event store stream naming
*
* Event stores typically use this to build stream names:
* Stream name = aggregateType() . "-" . aggregateId()
*
* Example:
* For an Order aggregate with ID "order-123", this returns "order-123"
* Stream name becomes: "Order-order-123"
*
* @return string The aggregate identifier (UUID, integer, or custom ID)
*/
public function aggregateId(): string;
/**
* The type of aggregate that emitted this event.
*
* Identifies which aggregate class/type this event belongs to.
* Used by event stores for stream organization and aggregate reconstruction.
*
* Typical values (choose what works for your event store):
* - Fully qualified class name: "App\\Domain\\Order\\Order"
* - Short class name: "Order"
* - Type identifier: "order"
* - Custom identifier: "sales.order"
*
* Consistency is key - use the same format across all events.
*
* @return string The aggregate type identifier
*/
public function aggregateType(): string;
/**
* The schema version of this event.
*
* Critical for event sourcing systems where events are stored permanently
* and aggregates must be rebuilt from historical events over time.
*
* Versioning strategies:
* - Semantic versioning: "1.0.0", "1.1.0", "2.0.0"
* - Simple incrementing: "1", "2", "3"
* - Date-based: "2024-01-15", "2025-10-14"
*
* When to increment the version:
* - Adding new fields: minor version (1.0 → 1.1)
* - Removing fields: major version (1.0 → 2.0)
* - Changing field types: major version (1.0 → 2.0)
* - Renaming fields: major version (1.0 → 2.0)
*
* Event stores use this for:
* - Upcasting old events to new schema
* - Maintaining multiple versions simultaneously
* - Migration strategies and backward compatibility
*
* Example evolution:
* - OrderPlaced v1.0: {orderId, total}
* - OrderPlaced v1.1: {orderId, total, customerId} (added field)
* - OrderPlaced v2.0: {orderId, total, customerId, items[]} (added array)
*
* @return string Version identifier (semantic, numeric, or date-based)
*/
public function eventVersion(): string;
}