/** * @param Uuid $id * @return AggregateRoot * @throws AggregateNotFoundException */ public function findById(Uuid $id) { $events = $this->storage->getEventsForAggregate($id); $book = new Book($id); $book->loadFromHistory($events); return $book; }
/** * @param Uuid $id * @return User * @throws AggregateNotFoundException */ public function findById(Uuid $id) { $events = $this->storage->getEventsForAggregate($id); $user = new User($id); $user->loadFromHistory($events); return $user; }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln("Reload read store from event store."); $this->documents->clear(); $output->write("Loading aggregate identities... "); $ids = $this->events->getAggregateIds(); $output->writeln("done."); $total = count($ids); $current = 0; foreach ($ids as $id) { /* @var $id Uuid */ $eventStream = $this->events->getEventsForAggregate($id); $aggregate = new UserReplay($id); $aggregate->loadFromHistory($eventStream); $document = $this->createDocumentFromAggregate($aggregate, $eventStream->size()); $this->documents->upsert($id, $document); $output->write(sprintf("%d/%d\r", ++$current, $total)); } $output->writeln("\nDone."); }
public function testSaveEventsIncreasesPlayhead() { $id = Uuid::createNew(); $events = new Events([new FirstEvent(), new SecondEvent()]); $eventBus = $this->getEventBus(); $serializer = $this->getSerializer(); $serializer->expects(self::any())->method('serialize')->will(self::returnCallback(function ($data, $_) { return json_encode(['version' => $data->getVersion()]); })); $serializer->expects(self::any())->method('deserialize')->will(self::returnCallback(function ($data, $type, $_) { switch ($type) { case FirstEvent::class: return new FirstEvent(json_decode($data, true)); case SecondEvent::class: return new SecondEvent(json_decode($data, true)); } })); $classMap = new EventClassMap([FirstEvent::class, SecondEvent::class]); $store = new EventStore($eventBus, new MemoryEventStorage(), $serializer, $classMap); $store->save($id, $events, -1); $recordedEvents = $store->getEventsForAggregate($id); /* @var $recorded Event[] */ $recorded = iterator_to_array($recordedEvents->getIterator()); self::assertCount(2, $recorded); self::assertEquals(0, $recorded[0]->getVersion()); self::assertEquals(1, $recorded[1]->getVersion()); }