/**
  * @todo lazy loading
  *
  * @param string $eventStream
  * @return DomainEvent[]
  * @throws EventStreamNotFoundException
  */
 public function load($eventStream)
 {
     try {
         $feed = $this->eventStore->openStreamFeed($eventStream, EntryEmbedMode::RICH());
     } catch (StreamNotFoundException $e) {
         throw new EventStreamNotFoundException($e->getMessage(), 1427104251);
     }
     if ($feed->hasLink(LinkRelation::LAST())) {
         $feed = $this->eventStore->navigateStreamFeed($feed, LinkRelation::LAST());
     } else {
         $feed = $this->eventStore->navigateStreamFeed($feed, LinkRelation::FIRST());
     }
     $rel = LinkRelation::PREVIOUS();
     $domainEvents = [];
     while ($feed !== NULL) {
         /** @var Entry[] $entries */
         $entries = array_reverse($feed->getEntries());
         foreach ($entries as $entry) {
             $event = $this->eventStore->readEvent($entry->getEventUrl());
             if ($event === NULL) {
                 continue;
             }
             $domainEvent = $this->eventTranslator->fromEvent($event, $entry->getType());
             $domainEvent->version = $entry->getVersion();
             $domainEvents[] = $domainEvent;
         }
         $feed = $this->eventStore->navigateStreamFeed($feed, $rel);
     }
     return $domainEvents;
 }
 /**
  * @param mixed $id
  *
  * @return Contact
  */
 public function findById($id)
 {
     $events = $this->store->fetchEventsById($id);
     $entity = $this->eventity->replay($events);
     Assertion::isInstanceOf($entity, Contact::class);
     return $entity;
 }
/**
 * @param  int    $length
 * @param  array  $metadata
 * @return string
 */
function prepare_test_stream(EventStore $es, $length = 1, $metadata = [])
{
    $streamName = uniqid();
    $events = [];
    for ($i = 0; $i < $length; ++$i) {
        $events[] = WritableEvent::newInstance('Foo', ['foo' => 'bar'], $metadata);
    }
    $collection = new WritableEventCollection($events);
    $es->writeToStream($streamName, $collection);
    return $streamName;
}