|
3 | 3 | namespace TangibleDDD\Application\Process; |
4 | 4 |
|
5 | 5 | use DateTimeImmutable; |
| 6 | +use TangibleDDD\Domain\Events\IIntegrationEvent; |
6 | 7 | use TangibleDDD\Domain\Shared\Aggregate; |
7 | 8 | use TangibleDDD\Domain\Shared\JsonLifecycleValue; |
8 | 9 |
|
|
16 | 17 | * - commands: execute these commands (they send() themselves) |
17 | 18 | * - await: suspend until an integration event fires |
18 | 19 | * |
19 | | - * Example: |
| 20 | + * ## DI Registration |
| 21 | + * |
| 22 | + * Register your process in services.yaml with the 'ddd.long_process' tag. |
| 23 | + * If your process uses AwaitEvent, declare the awaited event classes: |
| 24 | + * |
| 25 | + * ```yaml |
| 26 | + * App\Process\GenerateLearningPath: |
| 27 | + * tags: |
| 28 | + * - name: 'ddd.long_process' |
| 29 | + * awaits: |
| 30 | + * - App\Events\UserApprovedPath |
| 31 | + * ``` |
| 32 | + * |
| 33 | + * ## Example |
| 34 | + * |
20 | 35 | * ```php |
21 | 36 | * class GenerateLearningPath extends LongProcess { |
22 | 37 | * public function __construct( |
|
34 | 49 | * await: new AwaitEvent(UserApprovedPath::class, ['user_id' => $this->user_id]) |
35 | 50 | * ); |
36 | 51 | * } |
| 52 | + * |
| 53 | + * // Next step receives the event via $this->resume_event() |
| 54 | + * protected function process_approval(): Result { |
| 55 | + * $event = $this->resume_event(UserApprovedPath::class); |
| 56 | + * // ... use event data |
| 57 | + * return new Result(); |
| 58 | + * } |
37 | 59 | * } |
38 | 60 | * ``` |
39 | 61 | */ |
@@ -63,6 +85,17 @@ abstract class LongProcess extends Aggregate { |
63 | 85 | */ |
64 | 86 | protected ?ProcessSteps $steps = null; |
65 | 87 |
|
| 88 | + // ───────────────────────────────────────────────────────────────────────── |
| 89 | + // Transient state (NOT persisted - only valid during current execution) |
| 90 | + // ───────────────────────────────────────────────────────────────────────── |
| 91 | + |
| 92 | + /** |
| 93 | + * Event that triggered resume from suspension. |
| 94 | + * Available to the step immediately after an await. |
| 95 | + * Cleared after the step executes. |
| 96 | + */ |
| 97 | + private ?IIntegrationEvent $resume_event = null; |
| 98 | + |
66 | 99 | // ───────────────────────────────────────────────────────────────────────── |
67 | 100 | // Accessors (framework state) |
68 | 101 | // ───────────────────────────────────────────────────────────────────────── |
@@ -99,6 +132,50 @@ public function updated_at(): ?DateTimeImmutable { |
99 | 132 | return $this->updated_at; |
100 | 133 | } |
101 | 134 |
|
| 135 | + // ───────────────────────────────────────────────────────────────────────── |
| 136 | + // Transient state accessors |
| 137 | + // ───────────────────────────────────────────────────────────────────────── |
| 138 | + |
| 139 | + /** |
| 140 | + * Get the event that triggered resume from suspension. |
| 141 | + * Only available to the step immediately after an await. |
| 142 | + * |
| 143 | + * @template T of IIntegrationEvent |
| 144 | + * @param class-string<T>|null $expected_class Optional type check |
| 145 | + * @return T|IIntegrationEvent|null |
| 146 | + */ |
| 147 | + public function resume_event(?string $expected_class = null): ?IIntegrationEvent { |
| 148 | + if ($this->resume_event === null) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + if ($expected_class !== null && !($this->resume_event instanceof $expected_class)) { |
| 153 | + throw new \RuntimeException(sprintf( |
| 154 | + 'Expected resume event of type %s, got %s', |
| 155 | + $expected_class, |
| 156 | + get_class($this->resume_event) |
| 157 | + )); |
| 158 | + } |
| 159 | + |
| 160 | + return $this->resume_event; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Set the resume event (called by ProcessRunner). |
| 165 | + * @internal |
| 166 | + */ |
| 167 | + public function set_resume_event(IIntegrationEvent $event): void { |
| 168 | + $this->resume_event = $event; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Clear the resume event after step execution. |
| 173 | + * @internal |
| 174 | + */ |
| 175 | + public function clear_resume_event(): void { |
| 176 | + $this->resume_event = null; |
| 177 | + } |
| 178 | + |
102 | 179 | // ───────────────────────────────────────────────────────────────────────── |
103 | 180 | // Step state accessors (delegates to ProcessSteps, hides internal structure) |
104 | 181 | // ───────────────────────────────────────────────────────────────────────── |
|
0 commit comments