Author: Alexander Miertsch (contact@prooph.de)
 /**
  * @param string $aggregateId
  * @return null|object
  */
 protected function loadFromSnapshotStore($aggregateId)
 {
     $snapshot = $this->snapshotStore->get($this->aggregateType, $aggregateId);
     if (!$snapshot) {
         return;
     }
     $aggregateRoot = $snapshot->aggregateRoot();
     $streamName = $this->determineStreamName($aggregateId);
     $streamEvents = $this->eventStore->loadEventsByMetadataFrom($streamName, ['aggregate_type' => $this->aggregateType->toString(), 'aggregate_id' => $aggregateId], $snapshot->lastVersion() + 1);
     if (!$streamEvents->valid()) {
         return $aggregateRoot;
     }
     $this->aggregateTranslator->replayStreamEvents($aggregateRoot, $streamEvents);
     return $aggregateRoot;
 }
Example #2
0
 /**
  * Returns null if no stream events can be found for aggregate root otherwise the reconstituted aggregate root
  *
  * @param string $aggregateId
  * @return null|object
  */
 public function getAggregateRoot($aggregateId)
 {
     Assertion::string($aggregateId, 'AggregateId needs to be string');
     if (isset($this->identityMap[$aggregateId])) {
         return $this->identityMap[$aggregateId];
     }
     $streamEvents = $this->streamStrategy->read($this->aggregateType, $aggregateId);
     if (count($streamEvents) === 0) {
         return;
     }
     $aggregateType = $this->streamStrategy->getAggregateRootType($this->aggregateType, $streamEvents);
     $eventSourcedAggregateRoot = $this->aggregateTranslator->reconstituteAggregateFromHistory($aggregateType, $streamEvents);
     $this->identityMap[$aggregateId] = $eventSourcedAggregateRoot;
     return $eventSourcedAggregateRoot;
 }