Skip to content

Commit 0bcf7a4

Browse files
committed
fix event descriptions
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent b2d8b1c commit 0bcf7a4

File tree

4 files changed

+376
-20
lines changed

4 files changed

+376
-20
lines changed

.idea/php.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Events/EventDescription.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/*
34
* Copyright ©2024 Robert Landers
45
*
@@ -29,6 +30,8 @@
2930
use Bottledcode\DurablePhp\State\Ids\StateId;
3031
use Bottledcode\DurablePhp\State\Serializer;
3132
use DateTimeImmutable;
33+
use JsonException;
34+
use ReflectionClass;
3235

3336
readonly class EventDescription
3437
{
@@ -73,7 +76,7 @@ private function describe(Event $event): void
7376

7477
$targetOps = [];
7578
$sourceOps = [];
76-
while ($event instanceof HasInnerEventInterface) {
79+
while ($event instanceof HasInnerEventInterface) {
7780
if ($event instanceof ReplyToInterface) {
7881
$this->replyTo = $event->getReplyTo();
7982
}
@@ -93,14 +96,14 @@ private function describe(Event $event): void
9396
$this->meta = Serializer::serialize($event);
9497
}
9598

96-
$reflection = new \ReflectionClass($event);
97-
foreach($reflection->getAttributes(NeedsTarget::class) as $target) {
99+
$reflection = new ReflectionClass($event);
100+
foreach ($reflection->getAttributes(NeedsTarget::class) as $target) {
98101
/** @var NeedsTarget $attr */
99102
$attr = $target->newInstance();
100103
$targetOps[] = $attr->operation;
101104
}
102105

103-
foreach($reflection->getAttributes(NeedsSource::class) as $target) {
106+
foreach ($reflection->getAttributes(NeedsSource::class) as $target) {
104107
/** @var NeedsTarget $attr */
105108
$attr = $target->newInstance();
106109
$sourceOps[] = $attr->operation;
@@ -109,14 +112,24 @@ private function describe(Event $event): void
109112
$event = $event->getInnerEvent();
110113
}
111114

112-
$reflection = new \ReflectionClass($event);
113-
foreach($reflection->getAttributes(NeedsTarget::class) as $target) {
115+
if ($event instanceof PoisonPill) {
116+
$this->isPoisoned = true;
117+
}
118+
if ($event instanceof External) {
119+
$this->meta = Serializer::serialize($event);
120+
}
121+
if ($event instanceof ReplyToInterface) {
122+
$this->replyTo = $event->getReplyTo();
123+
}
124+
125+
$reflection = new ReflectionClass($event);
126+
foreach ($reflection->getAttributes(NeedsTarget::class) as $target) {
114127
/** @var NeedsTarget $attr */
115128
$attr = $target->newInstance();
116129
$targetOps[] = $attr->operation;
117130
}
118131

119-
foreach($reflection->getAttributes(NeedsSource::class) as $target) {
132+
foreach ($reflection->getAttributes(NeedsSource::class) as $target) {
120133
/** @var NeedsTarget $attr */
121134
$attr = $target->newInstance();
122135
$sourceOps[] = $attr->operation;
@@ -131,11 +144,12 @@ private function describe(Event $event): void
131144
$this->replyTo ??= null;
132145
$this->scheduledAt ??= null;
133146
$this->destination ??= null;
147+
$this->meta ??= [];
134148

135149
$this->targetType = match (true) {
136-
$this->destination->isActivityId() => TargetType::Activity,
137-
$this->destination->isOrchestrationId() => TargetType::Orchestration,
138-
$this->destination->isEntityId() => TargetType::Entity,
150+
$this->destination?->isActivityId() => TargetType::Activity,
151+
$this->destination?->isOrchestrationId() => TargetType::Orchestration,
152+
$this->destination?->isEntityId() => TargetType::Entity,
139153
default => TargetType::None,
140154
};
141155
}
@@ -150,22 +164,25 @@ public static function fromStream(string $data): self
150164
}
151165

152166
/**
153-
* @throws \JsonException
167+
* @throws JsonException
154168
*/
155169
public static function fromJson(string $json): EventDescription
156170
{
157-
return new EventDescription(Serializer::deserialize(json_decode($json, true, 512, JSON_THROW_ON_ERROR), Event::class));
171+
return new EventDescription(
172+
Serializer::deserialize(json_decode($json, true, 512, JSON_THROW_ON_ERROR), Event::class),
173+
);
158174
}
159175

160176
public function toStream(): string
161177
{
162-
$serialized = function_exists('igbinary_serialize') ? igbinary_serialize($this->event) : serialize($this->event);
178+
$serialized =
179+
function_exists('igbinary_serialize') ? igbinary_serialize($this->event) : serialize($this->event);
163180
$serialized = function_exists('gzencode') ? gzencode($serialized) : $serialized;
164181

165182
$event = base64_encode($serialized);
166183

167184
return json_encode([
168-
'destination' => $this->destination->id,
185+
'destination' => $this->destination?->id ?? null,
169186
'replyTo' => $this->replyTo?->id ?? '',
170187
'scheduleAt' => $this->scheduledAt?->format(DATE_ATOM) ?? gmdate(DATE_ATOM, time() - 30),
171188
'eventId' => $this->eventId,
@@ -179,7 +196,7 @@ public function toStream(): string
179196
}
180197

181198
/**
182-
* @throws \JsonException
199+
* @throws JsonException
183200
*/
184201
public function toJson(): string
185202
{

src/Events/WithLock.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
class WithLock extends Event implements HasInnerEventInterface
3535
{
3636
/**
37-
* @param string $eventId
38-
* @param StateId $owner
39-
* @param array<StateId> $participants
40-
* @param Event $innerEvent
37+
* @param array<StateId> $participants
4138
*/
4239
public function __construct(
4340
string $eventId,
@@ -46,7 +43,7 @@ public function __construct(
4643
public array $participants,
4744
public Event $innerEvent,
4845
) {
49-
parent::__construct($this->innerEvent ?: Uuid::uuid7());
46+
parent::__construct($this->innerEvent->eventId ?: Uuid::uuid7());
5047
}
5148

5249
public static function onEntity(StateId $owner, Event $innerEvent, StateId ...$targets): self

0 commit comments

Comments
 (0)