Exemplo n.º 1
0
 public function save(AggregateRoot $root)
 {
     $aggregateId = (string) $root->getIdentity();
     $versionFile = $aggregateId . '-' . $root->getVersion();
     if (file_exists($this->dataPath . '/' . $versionFile)) {
         throw new IdenticalSnapshot();
     }
     $stream = fopen($this->dataPath . '/' . $versionFile, 'w');
     fwrite($stream, serialize($root));
     fclose($stream);
 }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
 /**
  * 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);
 }
Exemplo n.º 5
0
 public function save(AggregateRoot $root)
 {
     return $this->mongo->aggregate_snapshots->insert(['identity' => (string) $root->getIdentity(), 'aggregate' => serialize($root), 'version' => $root->getVersion(), 'creation_date' => new \MongoDate()]);
 }
Exemplo n.º 6
0
 /**
  * Save given snapshot
  *
  * @param AggregateRoot $root
  */
 public function save(AggregateRoot $root)
 {
     $snapshot = new Snapshot($root, $root->getVersion(), new \DateTime());
     $this->snapshots[(string) $root->getIdentity()][] = $snapshot;
 }