Exemplo n.º 1
0
 /**
  * Snapshot constructor.
  *
  * @param string                             $aggregateType
  * @param EventSourcedAggregateRootInterface $aggregate
  * @param \DateTime                          $createdAt
  */
 public function __construct($aggregateType, EventSourcedAggregateRootInterface $aggregate, \DateTime $createdAt)
 {
     $this->aggregateType = $aggregateType;
     $this->aggregate = $aggregate;
     $this->version = $aggregate->version();
     $this->createdAt = $createdAt;
 }
 /**
  * Save the aggregate history.
  *
  * @param EventSourcedAggregateRootInterface $aggregateRoot
  */
 protected function saveHistory(EventSourcedAggregateRootInterface $aggregateRoot)
 {
     $recordedEvents = $aggregateRoot->recordedEvents();
     if (count($recordedEvents) > 0) {
         DomainEventPublisher::publish(new PrePersistEvent($aggregateRoot));
         // clear events
         $aggregateRoot->clearEvents();
         // create the eventStream and persist it
         $applicationVersion = VersionManager::currentApplicationVersion();
         $eventStream = new EventStream($this->streamName(), $aggregateRoot->id(), $recordedEvents);
         $this->eventStore->persist($eventStream, $aggregateRoot->version(), $applicationVersion);
         DomainEventPublisher::publish(new PostPersistEvent($aggregateRoot));
     }
 }