11<?php
2+
23/*
34 * Copyright ©2024 Robert Landers
45 *
2930use Bottledcode \DurablePhp \State \Ids \StateId ;
3031use Bottledcode \DurablePhp \State \Serializer ;
3132use DateTimeImmutable ;
33+ use JsonException ;
34+ use ReflectionClass ;
3235
3336readonly 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 {
0 commit comments