/** * Save the aggregate history. * * @param EventSourcedAggregateRootInterface $aggregateRoot */ protected function saveHistory(EventSourcedAggregateRootInterface $aggregateRoot) { $recordedEvents = $aggregateRoot->recordedEvents(); if (count($recordedEvents) > 0) { DomainEventPublisher::publish(new PrePersistEvent($aggregateRoot)); // clear events $aggregateRoot->clearEvents(); // create the eventStream and persist it $applicationVersion = VersionManager::currentApplicationVersion(); $eventStream = new EventStream($this->streamName(), $aggregateRoot->id(), $recordedEvents); $this->eventStore->persist($eventStream, $aggregateRoot->version(), $applicationVersion); DomainEventPublisher::publish(new PostPersistEvent($aggregateRoot)); } }
/** * Load a aggregate snapshot from the storage. * * @param IdInterface $id * * @return Snapshot */ protected function loadSnapshot(IdInterface $id) { $applicationVersion = VersionManager::currentApplicationVersion(); $aggregateVersion = VersionManager::versionOfClass($this->aggregateClassName, $applicationVersion); return $this->snapshotStore->load($this->streamName(), $id, $aggregateVersion, $applicationVersion); }
/** * @param Snapshot $snapshot * * @return EventSourcedAggregateRootInterface */ protected function snapshotToAggregateRoot(Snapshot $snapshot) { $applicationVersion = VersionManager::currentApplicationVersion(); $history = $this->eventStore->load($this->streamName(), $snapshot->aggregateId(), $snapshot->version(), $applicationVersion); $aggregateRoot = $snapshot->aggregate(); $aggregateRoot->setVersion($snapshot->version()); $aggregateRoot->replay($history); return $aggregateRoot; }
/** * @return bool */ public function migrate() { $nextMigration = $this->migrationManager()->nextMigrationToExecute(); $currentApplicationVersion = VersionManager::currentApplicationVersion(); if ($nextMigration !== null) { foreach ($nextMigration->aggregates() as $aggregateMigrationClass) { // -- start current application context -- /** @var MigrationInterface $migrationClass */ $migrationClass = new $aggregateMigrationClass(); $aggregateClassName = $migrationClass->aggregateClassName(); $currentAggregateVersion = VersionManager::versionOfClass($aggregateClassName, $currentApplicationVersion); // get all event streams for this aggregate class name $eventStreams = $this->eventStore->loadAll($this->streamName($aggregateClassName), $currentAggregateVersion, $currentApplicationVersion); // -- end current application context -- // -- start new version context -- $nextApplicationVersion = $nextMigration->version(); // iterate for every aggregateRoot event stream foreach ($eventStreams as $aggregateRootEventStream) { // migrate the current aggregate event stream $newAggregateRootEventStream = $migrationClass->migrate($aggregateRootEventStream); if ($newAggregateRootEventStream === null || $newAggregateRootEventStream !== null && !$newAggregateRootEventStream instanceof EventStream) { throw new \RuntimeException(sprintf('Invalid migration class %s. The migration method should return the new EventStream', $aggregateMigrationClass)); } // calculate the new version for every event $pathVersion = 0; foreach ($newAggregateRootEventStream->events() as $event) { $event->setVersion(++$pathVersion); } // and the new version for this aggregateRoot $newAggregateRootVersion = Version::fromString($nextMigration->version()->__toString()); $newAggregateRootVersion->setPatch($pathVersion); // persist the new event stream for this aggregateRoot. $this->migrateAggregateRoot($aggregateClassName, $newAggregateRootEventStream, $newAggregateRootVersion, $nextApplicationVersion); } // persist the new version of this aggregate class in the VersionManager VersionManager::persistVersionOfClass($aggregateClassName, $nextMigration->version(), $nextApplicationVersion); // -- end new version context -- } // persist the new migration in the store $this->migrationManager->persistMigration($nextMigration); return true; } return false; }
/** * Test setCurrentApplicationVersion method. */ public function testSetCurrentApplicationVersion() { $this->given($applicationVersion = Version::fromString('3.2.0'))->and(VersionManager::setCurrentApplicationVersion($applicationVersion))->then()->object(VersionManager::currentApplicationVersion())->isEqualTo($applicationVersion); }