public function extractPendingStreamEvents($eventSourcedAggregateRoot)
{
if (!is_object($eventSourcedAggregateRoot)) {
throw new AggregateTranslationFailedException('Event sourced Aggregate Root needs to be an object. Got ' . gettype($eventSourcedAggregateRoot));
}
if (!method_exists($eventSourcedAggregateRoot, $this->popRecordedEventsMethodName)) {
throw new AggregateTranslationFailedException(sprintf('Can not pop recorded events from aggregate root %s. The AR is missing a method with name %s!', get_class($eventSourcedAggregateRoot), $this->popRecordedEventsMethodName));
}
$recordedEvents = $eventSourcedAggregateRoot->{$this->popRecordedEventsMethodName}();
if (!is_array($recordedEvents) && !$recordedEvents instanceof \Traversable) {
throw new AggregateTranslationFailedException(sprintf('Failed to pop recorded events from aggregate root %s. The AR method %s returned a non traversable result!', get_class($eventSourcedAggregateRoot), $this->popRecordedEventsMethodName));
}
$callback = $this->eventToMessageCallback;
foreach ($recordedEvents as $i => $recordedEvent) {
if ($callback) {
$recordedEvent = $callback($recordedEvent);
$recordedEvents[$i] = $recordedEvent;
}
if (!$recordedEvent instanceof Message) {
throw new AggregateTranslationFailedException(sprintf('A recorded event of the aggregate root %s has the wrong type. Expected Prooph\\Common\\Messaging\\Message. Got %s', get_class($eventSourcedAggregateRoot), is_object($recordedEvent) ? get_class($recordedEvent) : gettype($recordedEvent)));
}
}
return $recordedEvents;
}