示例#1
0
 public function shouldCreateSnapshot(AggregateRoot $root)
 {
     if ($root->hasChanges()) {
         $lastEvent = $root->getChanges()[count($root->getChanges()) - 1];
         $lastVersion = $lastEvent->getVersion();
         for ($i = $root->getVersion(); $i <= $lastVersion; $i++) {
             if ($i % $this->interval == 0) {
                 return true;
             }
         }
     }
     return false;
 }
 /**
  * To store the updates made to an Aggregate, we only need to
  * commit the latest recorded events to the EventStore.
  *
  * An eventbus can handle eventual or direct consistency.
  *
  * Snapsnots could be made if the policy allows it and this repo is
  * constructed with a storage for storing snapshots.
  *
  * @param AggregateRoot $aggregate
  * @return CommittedEvents
  */
 public function save(AggregateRoot $aggregate)
 {
     $uncommitted = $aggregate->getChanges();
     $committedStream = $this->eventStore->commit($uncommitted);
     // consider eventual consistency
     if (!is_null($this->eventBus)) {
         $this->eventBus->publish($committedStream);
     }
     // do we need to create a snapshot at this point?
     if (!is_null($this->snapshotStore) && $this->snapshottingPolicy->shouldCreateSnapshot($aggregate)) {
         $this->saveSnapshot($aggregate);
     }
     // remove changes on aggregate from memory
     $aggregate->clearChanges();
     return $committedStream;
 }