Skip to content

Commit 97bc8eb

Browse files
committed
send from event to entity calls
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent e49d080 commit 97bc8eb

File tree

6 files changed

+90
-31
lines changed

6 files changed

+90
-31
lines changed

src/EntityContext.php

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace Bottledcode\DurablePhp;
2626

27+
use Bottledcode\DurablePhp\Events\Event;
2728
use Bottledcode\DurablePhp\Events\GiveOwnership;
2829
use Bottledcode\DurablePhp\Events\RaiseEvent;
2930
use Bottledcode\DurablePhp\Events\RevokeRole;
@@ -36,6 +37,7 @@
3637
use Bottledcode\DurablePhp\Events\TaskCompleted;
3738
use Bottledcode\DurablePhp\Events\WithDelay;
3839
use Bottledcode\DurablePhp\Events\WithEntity;
40+
use Bottledcode\DurablePhp\Events\WithFrom;
3941
use Bottledcode\DurablePhp\Events\WithOrchestration;
4042
use Bottledcode\DurablePhp\Exceptions\Unwind;
4143
use 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
}

src/Events/RevokeUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function completely(string $userId): self
4141
return new self($userId, null);
4242
}
4343

44-
public function __toString()
44+
public function __toString(): string
4545
{
4646
return sprintf('Revoke(user: %s)', $this->userId);
4747
}

src/Events/ShareOwnership.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525
namespace Bottledcode\DurablePhp\Events;
2626

27+
use Bottledcode\DurablePhp\Events\Shares\NeedsSource;
28+
use Bottledcode\DurablePhp\Events\Shares\Operation;
2729
use Ramsey\Uuid\Uuid;
2830

31+
#[NeedsSource(Operation::Owner)]
2932
class ShareOwnership extends Event implements External
3033
{
3134
private function __construct(public string $userId)
@@ -38,7 +41,7 @@ public static function withUser(string $userId): self
3841
return new self($userId);
3942
}
4043

41-
public function __toString()
44+
public function __toString(): string
4245
{
4346
return sprintf('ShareOwnership(%s)', $this->userId);
4447
}

src/Events/ShareWithRole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function For(string $role, Operation ...$allowedOperations): self
4242
return new self($role, $allowedOperations);
4343
}
4444

45-
public function __toString()
45+
public function __toString(): string
4646
{
4747
return sprintf('Share(role: %s, %s)', $this->role, implode(', ', $this->allowedOperations));
4848
}

src/Events/ShareWithUser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function For(string $userId, Operation ...$allowedOperations): sel
4242
return new self($userId, $allowedOperations);
4343
}
4444

45-
public function __toString()
45+
public function __toString(): string
4646
{
4747
return sprintf('Share(user: %s, %s)', $this->userId, implode(', ', $this->allowedOperations));
4848
}

src/Events/WithFrom.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bottledcode\DurablePhp\Events;
4+
5+
use Bottledcode\DurablePhp\State\Ids\StateId;
6+
7+
class WithFrom extends Event implements HasInnerEventInterface
8+
{
9+
public function __construct(string $eventId, public StateId $from, private readonly Event $innerEvent)
10+
{
11+
parent::__construct($eventId);
12+
}
13+
14+
public static function forEvent(StateId $from, Event $innerEvent): Event
15+
{
16+
return new self($innerEvent->eventId, $from, $innerEvent);
17+
}
18+
19+
public function __toString(): string
20+
{
21+
return sprintf('WithFrom(%s, %s)', $this->from, $this->innerEvent);
22+
}
23+
24+
public function getInnerEvent(): Event
25+
{
26+
return $this->innerEvent;
27+
}
28+
}

0 commit comments

Comments
 (0)