/** * @param CommitId $commitId * @param Contract $aggregateType * @param string $aggregateId * @param int $expectedAggregateVersion * @param EventEnvelope[] $eventEnvelopes */ public function commit(CommitId $commitId, Contract $aggregateType, $aggregateId, $expectedAggregateVersion, array $eventEnvelopes) { $aggregateVersion = $this->versionFor($aggregateType, $aggregateId); if ($aggregateVersion !== $expectedAggregateVersion) { throw new OptimisticConcurrencyFailed(); } $utcCommittedTime = new \DateTimeImmutable('now'); foreach ($eventEnvelopes as $eventEnvelope) { $metadata = $eventEnvelope->getMetadataType() ? json_encode($this->metadataSerializer->serialize($eventEnvelope->getMetadataType(), $eventEnvelope->getMetadata())) : null; $values = ['commit_id' => $commitId, 'utc_committed_time' => $utcCommittedTime->format('Y-m-d H:i:s'), 'aggregate_type' => $aggregateType->getContractName(), 'aggregate_id' => $aggregateId, 'aggregate_version' => ++$aggregateVersion, 'event_type' => $eventEnvelope->getEventType()->getContractName(), 'event_id' => $eventEnvelope->getEventId(), 'event' => json_encode($this->eventSerializer->serialize($eventEnvelope->getEventType(), $eventEnvelope->getEvent())), '`when`' => $eventEnvelope->getWhen()->format('Y-m-d H:i:s'), 'metadata_type' => $eventEnvelope->getMetadataType() ? $eventEnvelope->getMetadataType()->getContractName() : null, 'metadata' => $metadata]; $this->connection->insert($this->tableName, $values); } }
/** * @param CommitId $commitId * @param Contract $aggregateType * @param string $aggregateId * @param int $expectedAggregateVersion * @param EventEnvelope[] $eventEnvelopes */ public function commit(CommitId $commitId, Contract $aggregateType, $aggregateId, $expectedAggregateVersion, array $eventEnvelopes) { $aggregateVersion = $this->versionFor($aggregateType, $aggregateId); if ($aggregateVersion !== $expectedAggregateVersion) { throw new OptimisticConcurrencyFailed(); } foreach ($eventEnvelopes as $eventEnvelope) { $metadata = $eventEnvelope->getMetadataType() ? $this->metadataSerializer->serialize($eventEnvelope->getMetadataType(), $eventEnvelope->getMetadata()) : null; $record = new InMemoryPersistenceRecord(); $record->commitId = $commitId; $record->utcCommittedTime = new \DateTimeImmutable('now'); $record->aggregateType = $aggregateType; $record->aggregateId = $aggregateId; $record->aggregateVersion = ++$aggregateVersion; $record->eventType = $eventEnvelope->getEventType(); $record->eventId = $eventEnvelope->getEventId(); $record->event = $this->eventSerializer->serialize($eventEnvelope->getEventType(), $eventEnvelope->getEvent()); $record->version = $eventEnvelope->getVersion(); $record->when = $eventEnvelope->getWhen(); $record->metadataType = $eventEnvelope->getMetadataType(); $record->metadata = $metadata; $this->records[] = $record; } }
private function deserializeCommittedEvent($row) { return new CommittedEvent(CommitId::fromString($row['commit_id']), DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row['utc_committed_time']), new Contract($row['aggregate_root_type'], str_replace('.', '\\', $row['aggregate_root_type'])), $row['aggregate_root_id'], (int) $row['aggregate_root_version'], new EventEnvelope($this->eventContractResolver->resolveFromContractName($row['event_type']), $row['event_id'], $this->eventSerializer->deserialize($this->eventContractResolver->resolveFromContractName($row['event_type']), json_decode($row['event'], true)), (int) $row['event_version'], DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row['when']))); }