/** * {@inheritdoc} */ public function shouldCreateSnapshot(EventSourcedAggregateRootInterface $eventSourcedAggregateRoot) { $recordedEvents = $eventSourcedAggregateRoot->recordedEvents(); if (count($recordedEvents) > 0) { $lastSnapshot = $this->loadSnapshot($eventSourcedAggregateRoot->id()); $threshold = new \DateTime(date('c', strtotime('-' . $this->threshold))); if ($lastSnapshot !== null && $lastSnapshot->createdAt() < $threshold) { return true; } } return false; }
/** * 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)); } }
/** * {@inheritdoc} */ public function shouldCreateSnapshot(EventSourcedAggregateRootInterface $eventSourcedAggregateRoot) { $recordedEvents = $eventSourcedAggregateRoot->recordedEvents(); return count($recordedEvents) > 0; }
/** * @return IdInterface */ public function aggregateId() { return $this->aggregate->id(); }