Prooph\EventStore\Aggregate\ConfigurableAggregateTranslator::extractPendingStreamEvents PHP Метод

extractPendingStreamEvents() публичный Метод

public extractPendingStreamEvents ( object $eventSourcedAggregateRoot ) : Prooph\Common\Messaging\Message[]
$eventSourcedAggregateRoot object
Результат Prooph\Common\Messaging\Message[]
    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;
    }

Usage Example

 /**
  * @test
  * @expectedException Prooph\EventStore\Aggregate\Exception\AggregateTranslationFailedException
  */
 public function it_fails_when_popped_recorded_events_are_not_an_array_or_traversable()
 {
     $ar = $this->prophesize(DefaultAggregateRootContract::class);
     $ar->popRecordedEvents()->willReturn('invalid');
     $translator = new ConfigurableAggregateTranslator();
     $translator->extractPendingStreamEvents($ar->reveal());
 }