fkooman\RemoteStorage\MetadataStorage::updateDocument PHP Method

updateDocument() public method

..
public updateDocument ( Path $p, $contentType )
$p Path
    public function updateDocument(Path $p, $contentType)
    {
        $currentVersion = $this->getVersion($p);
        if (null === $currentVersion) {
            $newVersion = '1:' . $this->io->getRandom();
            $stmt = $this->db->prepare(sprintf('INSERT INTO %s (path, content_type, version) VALUES(:path, :content_type, :version)', $this->prefix . 'md'));
        } else {
            $explodedData = explode(':', $currentVersion);
            $newVersion = sprintf('%d:%s', $explodedData[0] + 1, $this->io->getRandom());
            $stmt = $this->db->prepare(sprintf('UPDATE %s SET version = :version, content_type = :content_type WHERE path = :path', $this->prefix . 'md'));
        }
        $stmt->bindValue(':path', $p->getPath(), PDO::PARAM_STR);
        $stmt->bindValue(':content_type', $contentType, PDO::PARAM_STR);
        $stmt->bindValue(':version', $newVersion, PDO::PARAM_STR);
        $stmt->execute();
        if (1 !== $stmt->rowCount()) {
            throw new MetadataStorageException('unable to update node');
        }
    }

Usage Example

 public function putDocument(Path $p, $contentType, $documentData, array $ifMatch = null, array $ifNoneMatch = null)
 {
     if (null !== $ifMatch && !in_array($this->md->getVersion($p), $ifMatch)) {
         throw new PreconditionFailedException('version mismatch');
     }
     if (null !== $ifNoneMatch && in_array('*', $ifNoneMatch) && null !== $this->md->getVersion($p)) {
         throw new PreconditionFailedException('document already exists');
     }
     $updatedEntities = $this->d->putDocument($p, $documentData);
     $this->md->updateDocument($p, $contentType);
     foreach ($updatedEntities as $u) {
         $this->md->updateFolder(new Path($u));
     }
 }