/**
  * {@inheritdoc}
  */
 public function initializeState(DomainEventStreamInterface $domainEventStream)
 {
     if (0 !== $this->getUncommittedEventCount()) {
         throw new \RuntimeException("Aggregate is already initialized");
     }
     $lastScn = -1;
     while ($domainEventStream->hasNext()) {
         $event = $domainEventStream->next();
         $lastScn = $event->getScn();
         $this->handleRecursively($event);
     }
     $this->initializeEventStream($lastScn);
 }
 public function appendEvents($type, DomainEventStreamInterface $events)
 {
     if (!$events->hasNext()) {
         return;
     }
     $next = $events->peek();
     if (0 === $next->getScn() && $this->fileResolver->eventFileExists($type, $next->getAggregateIdentifier())) {
         throw new ConflictingModificationException(sprintf("Could not create event stream for aggregate, such stream " . "already exists, type=%s, id=%s", $type, $next->getAggregateIdentifier()));
     }
     $file = $this->fileResolver->openEventFileForWriting($type, $next->getAggregateIdentifier());
     $eventMessageWriter = new FilesystemEventMessageWriter($file, $this->serializer);
     while ($events->hasNext()) {
         $eventMessageWriter->writeEventMessage($events->next());
     }
 }
 /**
  * Append the events in the given {@link DomainEventStreamInterface stream} to the event store.
  *
  * @param string $type The type descriptor of the object to store
  * @param DomainEventStreamInterface $events The event stream containing the events to store
  * @throws ConcurrencyException if an error occurs while storing the events in the event stream
  */
 public function appendEvents($type, DomainEventStreamInterface $events)
 {
     if (!$events->hasNext()) {
         return;
     }
     $messages = [];
     while ($events->hasNext()) {
         $messages[] = $events->next();
     }
     try {
         $this->mongoTemplate->domainEventCollection()->batchInsert($this->storageStrategy->createDocuments($type, $this->eventSerializer, $messages));
     } catch (\MongoDuplicateKeyException $ex) {
         throw new ConcurrencyException("Trying to insert an Event for an aggregate with a sequence " . "number that is already present in the Event Store", null, $ex);
     }
     $this->logger->debug("{num} events appended", ['num' => count($messages)]);
 }
Example #4
0
 public function appendEvents($type, DomainEventStreamInterface $events)
 {
     while ($events->hasNext()) {
         $event = $events->next();
         IdentifierValidator::validateIdentifier($event->getAggregateIdentifier());
         $serializedPayload = $this->serializer->serializePayload($event);
         $serializedMetaData = $this->serializer->serializeMetaData($event);
         $this->entryStore->persistEvent($type, $event, $serializedPayload, $serializedMetaData, $this->entityManager);
     }
     $this->entityManager->flush();
 }
 public function peek()
 {
     return $this->eventStream->peek();
 }
 public function appendEvents($type, DomainEventStreamInterface $events)
 {
     while ($events->hasNext()) {
         $next = $events->next();
         IdentifierValidator::validateIdentifier($next->getAggregateIdentifier());
         if (!empty($this->storedEvents)) {
             $lastEvent = end($this->storedEvents);
             if ($lastEvent->getAggregateIdentifier() !== $next->getAggregateIdentifier()) {
                 throw new EventStoreException("Writing events for an unexpected aggregate. This could " . "indicate that a wrong aggregate is being triggered.");
             } else {
                 if ($lastEvent->getScn() !== $next->getScn() - 1) {
                     throw new EventStoreException(sprintf("Unexpected sequence number on stored event. " . "Expected %s, but got %s.", $lastEvent->getScn() + 1, $next->getScn()));
                 }
             }
         }
         if (null === $this->aggregateIdentifier) {
             $this->aggregateIdentifier = $next->getAggregateIdentifier();
             $this->injectAggregateIdentifier();
         }
         $this->storedEvents[] = $next;
     }
 }
 public function appendEvents($type, \Governor\Framework\Domain\DomainEventStreamInterface $events)
 {
     while ($events->hasNext()) {
         $this->storedEvents[] = $events->next();
     }
 }