Skip to content

Commit c7d4508

Browse files
committed
update entity history to accept property hooks
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent be73d47 commit c7d4508

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

src/State/EntityHistory.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ class EntityHistory extends AbstractHistory
6868

6969
private LockStateMachine $lockQueue;
7070

71-
public function __construct(public StateId $id, #[Field(exclude: true)] public ?DurableLogger $logger, private Provenance $user)
72-
{
71+
public function __construct(
72+
public StateId $id,
73+
#[Field(exclude: true)]
74+
public ?DurableLogger $logger,
75+
private Provenance $user,
76+
) {
7377
$this->entityId = $id->toEntityId();
7478
}
7579

@@ -118,10 +122,12 @@ public function applyRaiseEvent(RaiseEvent $event, Event $original): Generator
118122
// reply to the lock request
119123
$reply = $this->getReplyTo($original);
120124
foreach ($reply as $nextEvent) {
121-
yield WithPriority::high(With::id(
122-
$nextEvent,
123-
RaiseEvent::forLock('locked', $event->eventData['owner'], $event->eventData['target']),
124-
));
125+
yield WithPriority::high(
126+
With::id(
127+
$nextEvent,
128+
RaiseEvent::forLock('locked', $event->eventData['owner'], $event->eventData['target']),
129+
),
130+
);
125131
}
126132
break;
127133
case '__unlock':
@@ -156,11 +162,19 @@ public function init(): void
156162
}
157163

158164
$this->lockQueue ??= new LockStateMachine($this->id);
159-
$this->state ??= new class () extends EntityState {};
165+
$this->state ??= new class extends EntityState {};
160166

161167
$this->name = $this->id->toEntityId()->name;
162168
$now = MonotonicClock::current()->now();
163-
$this->status = new Status($now, '', SerializedArray::fromArray([]), $this->id, $now, SerializedArray::fromArray([]), RuntimeStatus::Running);
169+
$this->status = new Status(
170+
$now,
171+
'',
172+
SerializedArray::fromArray([]),
173+
$this->id,
174+
$now,
175+
SerializedArray::fromArray([]),
176+
RuntimeStatus::Running,
177+
);
164178

165179
$this->state = $this->container->get($this->name);
166180
}
@@ -197,6 +211,16 @@ private function execute(Event $original, string $operation, array $input): Gene
197211
}
198212
}
199213
try {
214+
if (str_contains($operation, '::')) {
215+
[$property, $operation] = explode('::', $operation);
216+
$result = match ($operation) {
217+
'get' => $this->state->{$property},
218+
'set' => $this->state->{$property} = $input[0],
219+
default => throw new ReflectionException('Unknown operation'),
220+
};
221+
goto finalize;
222+
}
223+
200224
$operationReflection = $reflector->getMethod($operation);
201225
} catch (ReflectionException) {
202226
// search attributes for matching operation
@@ -239,9 +263,13 @@ private function execute(Event $original, string $operation, array $input): Gene
239263
}
240264
}
241265

266+
finalize:
267+
242268
if ($replyTo) {
243269
foreach ($replyTo as $reply) {
244-
yield WithPriority::high(WithOrchestration::forInstance($reply, TaskCompleted::forId($original->eventId, $result ?? null)));
270+
yield WithPriority::high(
271+
WithOrchestration::forInstance($reply, TaskCompleted::forId($original->eventId, $result ?? null)),
272+
);
245273
}
246274
}
247275
}
@@ -257,11 +285,10 @@ private function finalize(Event $event): Generator
257285
$now = time();
258286
$cutoff = $now - 3600; // 1 hour
259287
$this->history[$event->eventId] = $this->debugHistory ? $event : $now;
260-
$this->history =
261-
array_filter(
262-
$this->history,
263-
static fn(int|bool|Event $value) => is_int($value) ? $value > $cutoff : $value,
264-
);
288+
$this->history = array_filter(
289+
$this->history,
290+
static fn(int|bool|Event $value) => is_int($value) ? $value > $cutoff : $value,
291+
);
265292
$this->status = $this->status->with(lastUpdated: MonotonicClock::current()->now());
266293

267294
yield null;

0 commit comments

Comments
 (0)