Esempio n. 1
0
 /**
  * Should a snapshot be created?
  *
  * @param AggregateRoot $root
  * @return bool
  */
 public function shouldCreateSnapshot(AggregateRoot $root)
 {
     if ($root->hasChanges()) {
         $lastSnapshot = $this->store->get($root->getIdentity());
         $threshold = new \DateTime(date('c', strtotime('-' . $this->threshold)));
         if ($lastSnapshot->getCreationDate() > $threshold) {
             return true;
         }
     }
     return false;
 }
 /**
  * Save the aggregate state as single snapshot instead of multiple history events
  *
  * @param \Domain\Aggregates\AggregateRoot $aggregate
  * @return type
  * @throws \Exception when no snapshotStore was attached
  */
 public function saveSnapshot(AggregateRoot $aggregate)
 {
     if (is_null($this->snapshotStore)) {
         throw new \Exception('Unable to create snapshot; No store attached');
     }
     if ($aggregate->hasChanges()) {
         $aggregate = $aggregate::reconstituteFrom(new CommittedEvents($aggregate->getIdentity(), $aggregate->getChanges()->getEvents()));
     }
     return $this->snapshotStore->save($aggregate);
 }