Skip to content

Commit 115cc76

Browse files
committed
EventProcessors typically ingest multiple event types that they need to retrieve in onComplete or onTimeout. The helper method getEventOfType(string) has been implemented to allow returning either the singular Event or an array of Events that match the requested event type. Null is returned if the requested type has not yet been consumer
1 parent 633232b commit 115cc76

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/StateMachine/AEventProcessor.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function complete() : bool
241241

242242
/**
243243
* Get the current chain of events that have been consumed
244-
* @return array
244+
* @return IEvent[]
245245
*/
246246
public function getEventChain() : array
247247
{
@@ -272,6 +272,28 @@ public function getLastEvent() : ?Event {
272272
return $this->consumedEvents[array_key_last($this->consumedEvents)];
273273
}
274274

275+
/**
276+
* Return one or more events matching the specified type in the order they were captured
277+
* @param string $type
278+
* @return IEvent|IEvent[]|null
279+
*/
280+
public function getEventOfType(string $type) {
281+
$matched = [];
282+
foreach($this->consumedEvents as $event) {
283+
if ($event->getEventName() === $type) {
284+
$matched[] = $event;
285+
}
286+
}
287+
$cnt = count($matched);
288+
if (0 === $cnt) {
289+
return null;
290+
} elseif (1 === $cnt) {
291+
return $matched[0];
292+
} else {
293+
return $matched;
294+
}
295+
}
296+
275297
/**
276298
* Trim the number of events we have consumed, retaining the most recent $length number of events
277299
* @param int $length
@@ -482,16 +504,14 @@ public function resolveEvents(array $events) : void
482504
/** Completion Callback handlers */
483505
public function fire(): void
484506
{
485-
$this->onProgress();
486-
487507
if ($this->complete()) {
488508
$this->onComplete();
489509
$this->onDone();
490-
}
491-
492-
if ($this->isTimedOut()) {
510+
} elseif ($this->isTimedOut()) {
493511
$this->onTimeout();
494512
$this->onDone();
513+
} else {
514+
$this->onProgress();
495515
}
496516
}
497517

0 commit comments

Comments
 (0)