/** * Get the aggregate root if it exists otherwise null * * @param AggregateType $aggregateType * @param string $aggregateId * @return null|object */ public function get(AggregateType $aggregateType, $aggregateId) { if (!isset($this->map[$aggregateType->toString()][$aggregateId])) { return; } return $this->map[$aggregateType->toString()][$aggregateId]; }
/** * @param AggregateType $repositoryAggregateType * @param string $aggregateId * @param Message[] $streamEvents * @param object $aggregateRoot * @throws Exception\InvalidArgumentException * @return void */ public function appendEvents(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot) { $arType = AggregateType::fromAggregateRoot($aggregateRoot); if (!$repositoryAggregateType->equals($arType)) { throw new Exception\InvalidArgumentException(sprintf('aggregate root mismatch between repository type %s and object type %s', $repositoryAggregateType->toString(), $arType->toString())); } $this->eventStore->appendTo($this->buildStreamName($repositoryAggregateType, $aggregateId), $streamEvents); }
/** * @param AggregateType $aggregateType * @param \Iterator $historyEvents * @return object reconstructed AggregateRoot */ public function reconstituteAggregateFromHistory(AggregateType $aggregateType, \Iterator $historyEvents) { return $this->getAggregateRootDecorator()->fromHistory($aggregateType->toString(), $historyEvents); }
/** * @param AggregateType $aggregateType * @return string */ private function getShortAggregateTypeName(AggregateType $aggregateType) { $aggregateTypeName = str_replace('-', '_', $aggregateType->toString()); return implode('', array_slice(explode('\\', $aggregateTypeName), -1)); }
/** * Add aggregate_id and aggregate_type as metadata to $domainEvent * Override this method in an extending repository to add more or different metadata. * * @param Message $domainEvent * @param string $aggregateId * @return Message */ protected function enrichEventMetadata(Message $domainEvent, $aggregateId) { $domainEvent = $domainEvent->withAddedMetadata('aggregate_id', $aggregateId); return $domainEvent->withAddedMetadata('aggregate_type', $this->aggregateType->toString()); }
/** * @param AggregateType $aggregateType * @param Iterator $historyEvents * @throws Exception\AggregateTranslationFailedException * @return object reconstructed EventSourcedAggregateRoot */ public function reconstituteAggregateFromHistory(AggregateType $aggregateType, Iterator $historyEvents) { if ($this->messageToEventCallback) { $historyEvents = new MapIterator($historyEvents, $this->messageToEventCallback); } $aggregateClass = $aggregateType->toString(); if (!class_exists($aggregateClass)) { throw new AggregateTranslationFailedException(sprintf('Can not reconstitute aggregate of type %s. Class was not found', $aggregateClass)); } if (!method_exists($aggregateClass, $this->staticReconstituteFromHistoryMethodName)) { throw new AggregateTranslationFailedException(sprintf('Cannot reconstitute aggregate of type %s. Class is missing a static %s method!', $aggregateClass, $this->staticReconstituteFromHistoryMethodName)); } $method = $this->staticReconstituteFromHistoryMethodName; $aggregate = $aggregateClass::$method($historyEvents); if (!$aggregate instanceof $aggregateClass) { throw new AggregateTranslationFailedException(sprintf('Failed to reconstitute aggregate of type %s. Static method %s does not return an instance of the aggregate type!', $aggregateClass, $this->staticReconstituteFromHistoryMethodName)); } return $aggregate; }
/** * @param AggregateType $aggregateType * @return StreamName */ protected function buildStreamName(AggregateType $aggregateType) { if (isset($this->aggregateTypeStreamMap[$aggregateType->toString()])) { return new StreamName($this->aggregateTypeStreamMap[$aggregateType->toString()]); } return new StreamName($aggregateType->toString()); }