public function store(ModelInterface $model, array $options = array()) { // determine metadata and data from model $metadata = $this->metadataManager->loadByModel($model); $data = $this->serializer->serialize($model, array_merge($metadata->getDataFields(), $metadata->getMetadataFields())); // generate hash if necessary if ($metadata->isHashGenerated()) { $hash = $this->factory->createHash(json_encode($data)); } elseif (!($hash = $model->getHash())) { throw new \Exception('Hash not specified'); } $this->accessor->setValue($model, 'hash', $hash); $isNew = !$this->contains($hash, $model->getClass()); // dispatch event $event = new DatabaseStoreEvent($model, $data, $isNew, $metadata, $options); $this->eventDispatcher->dispatch(DatabaseEvent::STORE_EVENT, $event); // possibility to cancel store in a event-handler if ($event->isCanceled()) { return; } // possibility to change model in the event-handler to a stub $model = $event->getModel(); $data = $event->getData(); // prepare object $object = array('data' => $data, 'class' => $model->getClass()); // append existing policies if ($model instanceof DistributedModelInterface) { $object['policies'] = serialize($model->getPolicyCollection()); } // store it $this->storageAdapter->store($hash, $object, $metadata->getContext()); $this->searchAdapter->index($hash, $model, $metadata); return $model; }
/** * @param $data * * @return ChunkInterface */ public function upload($data) { $chunk = new Chunk(); $chunk->setData($data); $chunk->setLength(strlen($data)); $chunk->setPolicyCollection(new PolicyCollection()); $chunk->setHash($this->factory->createHash($data)); if ($this->database->contains($chunk->getHash(), Chunk::class)) { return $chunk; } return $this->database->store($chunk, array(ReplicatorInterface::OPTION_NAME => ReplicatorInterface::TYPE_FULL)); }
/** * {@inheritdoc} */ public function createHash(UserInterface $user, $name) { return $this->factory->createHash(sprintf('%s@%s/%s', $user->getUsername(), $this->hostName, $name)); }