2424
2525namespace Bottledcode \DurablePhp ;
2626
27+ use Bottledcode \DurablePhp \Events \Event ;
2728use Bottledcode \DurablePhp \Events \GiveOwnership ;
2829use Bottledcode \DurablePhp \Events \RaiseEvent ;
2930use Bottledcode \DurablePhp \Events \RevokeRole ;
3637use Bottledcode \DurablePhp \Events \TaskCompleted ;
3738use Bottledcode \DurablePhp \Events \WithDelay ;
3839use Bottledcode \DurablePhp \Events \WithEntity ;
40+ use Bottledcode \DurablePhp \Events \WithFrom ;
3941use Bottledcode \DurablePhp \Events \WithOrchestration ;
4042use Bottledcode \DurablePhp \Exceptions \Unwind ;
4143use Bottledcode \DurablePhp \Glue \Provenance ;
@@ -59,6 +61,8 @@ class EntityContext implements EntityContextInterface
5961{
6062 private static ?EntityContextInterface $ current = null ;
6163
64+ private readonly StateId $ from ;
65+
6266 public function __construct (
6367 private readonly EntityId $ id ,
6468 private readonly string $ operation ,
@@ -72,6 +76,7 @@ public function __construct(
7276 private readonly Provenance $ user ,
7377 ) {
7478 self ::$ current = $ this ;
79+ $ this ->from = StateId::fromEntityId ($ this ->id );
7580 }
7681
7782 public static function current (): static
@@ -112,16 +117,23 @@ public function signalEntity(
112117 array $ input = [],
113118 ?DateTimeImmutable $ scheduledTime = null ,
114119 ): void {
115- $ event = WithEntity::forInstance (
116- StateId::fromEntityId ($ entityId ),
117- RaiseEvent::forOperation ($ operation , $ input ),
120+ $ event = $ this ->addFrom (
121+ WithEntity::forInstance (
122+ StateId::fromEntityId ($ entityId ),
123+ RaiseEvent::forOperation ($ operation , $ input ),
124+ ),
118125 );
119126 if ($ scheduledTime ) {
120127 $ event = WithDelay::forEvent ($ scheduledTime , $ event );
121128 }
122129 $ this ->eventDispatcher ->fire ($ event );
123130 }
124131
132+ private function addFrom (Event $ event ): Event
133+ {
134+ return WithFrom::forEvent ($ this ->from , $ event );
135+ }
136+
125137 public function getId (): EntityId
126138 {
127139 return $ this ->id ;
@@ -140,9 +152,11 @@ public function startNewOrchestration(string $orchestration, array $input = [],
140152
141153 $ instance = StateId::fromInstance (OrchestrationInstance ($ orchestration , $ id ));
142154 $ this ->eventDispatcher ->fire (
143- WithOrchestration::forInstance (
144- $ instance ,
145- StartExecution::asParent ($ input , []),
155+ $ this ->addFrom (
156+ WithOrchestration::forInstance (
157+ $ instance ,
158+ StartExecution::asParent ($ input , []),
159+ ),
146160 ),
147161 );
148162 }
@@ -184,9 +198,11 @@ public function delayUntil(
184198 DateTimeInterface $ until = new DateTimeImmutable (),
185199 ): void {
186200 $ this ->eventDispatcher ->fire (
187- WithDelay::forEvent (
188- $ until ,
189- WithEntity::forInstance (StateId::fromEntityId ($ this ->id ), RaiseEvent::forOperation ($ operation , $ args )),
201+ $ this ->addFrom (
202+ WithDelay::forEvent (
203+ $ until ,
204+ WithEntity::forInstance (StateId::fromEntityId ($ this ->id ), RaiseEvent::forOperation ($ operation , $ args )),
205+ ),
190206 ),
191207 );
192208 }
@@ -199,59 +215,71 @@ public function currentUserId(): string
199215 public function shareOwnership (string $ withUser ): void
200216 {
201217 $ this ->eventDispatcher ->fire (
202- WithEntity::forInstance (
203- StateId::fromEntityId ($ this ->id ),
204- ShareOwnership::withUser ($ withUser ),
218+ $ this ->addFrom (
219+ WithEntity::forInstance (
220+ StateId::fromEntityId ($ this ->id ),
221+ ShareOwnership::withUser ($ withUser ),
222+ ),
205223 ),
206224 );
207225 }
208226
209227 public function giveOwnership (string $ withUser ): void
210228 {
211229 $ this ->eventDispatcher ->fire (
212- WithEntity::forInstance (
213- StateId::fromEntityId ($ this ->id ),
214- GiveOwnership::withUser ($ withUser ),
230+ $ this ->addFrom (
231+ WithEntity::forInstance (
232+ StateId::fromEntityId ($ this ->id ),
233+ GiveOwnership::withUser ($ withUser ),
234+ ),
215235 ),
216236 );
217237 }
218238
219239 public function grantUser (string $ withUser , Operation ...$ operation ): void
220240 {
221241 $ this ->eventDispatcher ->fire (
222- WithEntity::forInstance (
223- StateId::fromEntityId ($ this ->id ),
224- ShareWithUser::For ($ withUser , ...$ operation ),
242+ $ this ->addFrom (
243+ WithEntity::forInstance (
244+ StateId::fromEntityId ($ this ->id ),
245+ ShareWithUser::For ($ withUser , ...$ operation ),
246+ ),
225247 ),
226248 );
227249 }
228250
229251 public function grantRole (string $ withRole , Operation ...$ operation ): void
230252 {
231253 $ this ->eventDispatcher ->fire (
232- WithEntity::forInstance (
233- StateId::fromEntityId ($ this ->id ),
234- ShareWithRole::For ($ withRole , ...$ operation ),
254+ $ this ->addFrom (
255+ WithEntity::forInstance (
256+ StateId::fromEntityId ($ this ->id ),
257+ ShareWithRole::For ($ withRole , ...$ operation ),
258+ ),
235259 ),
236260 );
237261 }
238262
239263 public function revokeUser (string $ user ): void
240264 {
241265 $ this ->eventDispatcher ->fire (
242- WithEntity::forInstance (
243- StateId::fromEntityId ($ this ->id ),
244- RevokeUser::completely ($ user ),
266+ $ this ->addFrom (
267+ WithEntity::forInstance (
268+ StateId::fromEntityId ($ this ->id ),
269+ RevokeUser::completely ($ user ),
270+ ),
245271 ),
246272 );
247273 }
248274
249275 public function revokeRole (string $ role ): void
250276 {
251277 $ this ->eventDispatcher ->fire (
252- WithEntity::forInstance (
253- StateId::fromEntityId ($ this ->id ),
254- RevokeRole::completely ($ role ),
278+ $ this ->addFrom (
279+ WithEntity::forInstance (
280+ StateId::fromEntityId ($ this ->id ),
281+ RevokeRole::completely ($ role ),
282+ ),
255283 ),
256284 );
257285 }
0 commit comments