/**
  * @param Contract    $aggregateRootType
  * @param EventStream $eventStream
  *
  * @throws \Exception
  */
 public function append(Contract $aggregateRootType, EventStream $eventStream)
 {
     $statement = $this->insertQuery->prepare();
     try {
         while ($eventStream->hasNext()) {
             $event = $eventStream->next();
             $statement->bindValue(':aggregate_id', $event->aggregateRootIdentifier()->toString());
             $statement->bindValue(':aggregate_type', $aggregateRootType->toString());
             $statement->bindValue(':aggregate_version', $event->sequenceNumber());
             $statement->bindValue(':event_id', $event->identifier()->toString());
             $statement->bindValue(':event_payload', $this->eventSerializer->serializePayload($event));
             $statement->bindValue(':event_payload_type', $event->payloadType()->toString());
             $statement->bindValue(':event_metadata', $this->eventSerializer->serializeMetadata($event));
             $statement->bindValue(':event_metadata_type', $event->metadataType()->toString());
             $statement->execute();
             $statement->closeCursor();
         }
     } catch (\PDOException $exception) {
         // 23000 being the primary key duplicate entry error code.
         if ($exception->errorInfo[0] === '23000') {
             throw new ConcurrencyException('Concurrent modification detected.');
         }
         throw $exception;
     }
 }
 /**
  * @param array $record
  *
  * @return Event
  */
 private function translateRecordIntoEventMessage(array $record)
 {
     $identifier = new MessageIdentifier($record['event_id']);
     $payload = $this->eventSerializer->deserializePayload($record['event_payload'], Contract::createFromClassName($record['event_payload_type']));
     $metadata = $this->eventSerializer->deserializeMetadata($record['event_metadata'], Contract::createFromClassName($record['event_metadata_type']));
     $sequenceNumber = (int) $record['aggregate_version'];
     $eventMessage = new GenericEvent($this->aggregateRootIdentifier, $sequenceNumber, $identifier, $payload, $metadata);
     return $eventMessage;
 }
 /**
  * @param array $record
  *
  * @return Event
  */
 private function translateRecordIntoEventMessage(array $record)
 {
     $identifier = $this->messageIdentifierFactory->create($record['event_id']);
     $payload = $this->eventSerializer->deserializePayload($record['event_payload'], $this->contractFactory->createFromClassName($record['event_payload_type']));
     $metadata = $this->eventSerializer->deserializeMetadata($record['event_metadata'], $this->contractFactory->createFromClassName($record['event_metadata_type']));
     $sequenceNumber = (int) $record['aggregate_version'];
     //        $commitIdentifier = new CommitIdentifier($record['commit_id']);
     //        $committedAt = new \DateTime($record['committed_at']);
     $eventMessage = $this->eventFactory->create($this->aggregateRootIdentifier, $sequenceNumber, $identifier, $payload, $metadata, $this->contractFactory);
     //        $eventMessage->setCommitIdentifier($commitIdentifier);
     //        $eventMessage->setCommittedAt($committedAt);
     return $eventMessage;
 }
 /**
  * @param Contract    $aggregateType
  * @param EventStream $eventStream
  *
  * @throws \Exception
  */
 public function append(Contract $aggregateType, EventStream $eventStream)
 {
     $statement = $this->insertQuery->prepare();
     $commitIdentifier = CommitIdentifier::generate()->toString();
     $committedAt = new \DateTime();
     while ($eventStream->hasNext()) {
         $event = $eventStream->next();
         $statement->bindValue(':aggregate_id', $event->aggregateRootIdentifier()->toString());
         $statement->bindValue(':aggregate_type', $aggregateType->toString());
         $statement->bindValue(':aggregate_version', $event->sequenceNumber());
         $statement->bindValue(':event_id', $event->identifier()->toString());
         $statement->bindValue(':event_payload', $this->eventSerializer->serializePayload($event));
         $statement->bindValue(':event_payload_type', $event->payloadType()->toString());
         $statement->bindValue(':event_metadata', $this->eventSerializer->serializeMetadata($event));
         $statement->bindValue(':event_metadata_type', $event->metadataType()->toString());
         $statement->bindValue(':commit_id', $commitIdentifier);
         $statement->bindValue(':committed_at', $committedAt->format('UTC'));
         $statement->execute();
         $statement->closeCursor();
     }
 }