/** * Takes a snapshot * @param StreamName $streamName * @param AggregateRootInterface $aggregate * @param DomainMessage $message - The domain message * @return bool */ public function take(StreamName $streamName, AggregateRootInterface $aggregate, DomainMessage $message) { $id = $aggregate->getAggregateRootId(); if (!$this->strategy->isFulfilled($streamName, $aggregate)) { return false; } if (!$this->snapshotStore->has($id, $message->getVersion())) { $this->snapshotStore->save(Snapshot::take($id, $aggregate, $message->getVersion())); } return true; }
/** * {@inheritDoc} */ public function save(AggregateRootInterface $aggregate, StreamName $streamName = null) { $streamName = $this->determineStreamName($streamName); $eventStream = new EventStream($streamName, $aggregate->getUncommittedEvents()); $this->eventStore->append($eventStream); $eventStream->each(function (DomainMessage $domainMessage) { $this->eventBus->publish($domainMessage->getPayload()); })->each(function (DomainMessage $domainMessage) use($streamName, $aggregate) { if ($this->snapshotter) { $this->snapshotter->take($streamName, $aggregate, $domainMessage); } }); }
/** * @inheritdoc */ public function isFulfilled(StreamName $streamName, AggregateRootInterface $aggregate) { $countOfEvents = $this->eventStore->countEventsFor($streamName, $aggregate->getAggregateRootId()); return $countOfEvents && $countOfEvents % $this->count === 0; }