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); }
/** * 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; }
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()]); }
/** * 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; }