/** * Compares aggregate's current version with the stored version. If thery are out os sync throws exception * * @param EntityDTO $entity * @return nothing or ConflictingVersion Exception * @author Francisco Iglesias Gómez */ protected function checkVersion(EntityDTO $entity) { $newVersion = $entity->getVersion(); $storedVersion = $this->getStoredVersion($entity); if ($newVersion <= $storedVersion) { throw new Exception\ConflictingVersion(sprintf('Stored version for %s found to be %s, trying to save version %s', $entity, $storedVersion, $newVersion), 1); } }
protected function getStoredVersion(EntityDTO $entity) { return $this->em->createQuery('SELECT MAX(events.version) FROM EventStore:EventDTO events WHERE events.entity_type = :entity AND events.entity_id = :id')->setParameter('entity', $entity->getType())->setParameter('id', $entity->getPlainId())->getSingleScalarResult(); }
/** * Apply and record an event * * @param Event $event * @return void * @author Francisco Iglesias Gómez */ protected function recordThat(Event $event) { if (!$this->canHandleEvent($event)) { return; } $this->apply($event); $this->initStream(); $this->events->recordThat(EventMessage::record($event, EntityDTO::fromEntity($this))); }
public static function fromEventDTO(EventDTO $dto) { return new static($dto->getEvent(), EntityDTO::fromEventDTO($dto), EventEnvelope::fromEventDTO($dto)); }