Exemple #1
0
 /**
  * Constructs StoredEvent
  *
  * @param EventMessage $message The event message
  */
 public function __construct(EventMessage $message)
 {
     $this->aggregateId = $message->aggregateId()->toString();
     $this->aggregateIdType = Type::create($message->aggregateId())->toString();
     $this->aggregateType = $message->aggregateType()->toString();
     $this->messageId = $message->messageId()->toString();
     $this->timestamp = $message->timestamp()->toString();
     $this->payload = serialize($message->payload());
     $this->payloadType = $message->payloadType()->toString();
     $this->metaData = serialize($message->metaData());
     $this->sequence = $message->sequence();
 }
 /**
  * {@inheritdoc}
  */
 public function appendEvent(EventMessage $eventMessage)
 {
     $id = $eventMessage->aggregateId()->toString();
     $type = $eventMessage->aggregateType()->toString();
     if (!isset($this->streamData[$type])) {
         $this->streamData[$type] = [];
     }
     if (!isset($this->streamData[$type][$id])) {
         $this->streamData[$type][$id] = new StreamData();
     }
     $version = $eventMessage->sequence();
     if ($version === 0) {
         $expected = null;
     } else {
         $expected = $version - 1;
     }
     if ($this->streamData[$type][$id]->getVersion() !== $expected) {
         $found = $this->streamData[$type][$id]->getVersion();
         $message = sprintf('Expected v%s; found v%s in stream [%s]{%s}', $expected, $found, $type, $id);
         throw ConcurrencyException::create($message);
     }
     $event = new StoredEvent($eventMessage);
     $this->streamData[$type][$id]->addEvent($event);
     $this->streamData[$type][$id]->setVersion($version);
 }