fkooman\RemoteStorage\RemoteStorage::putDocument PHP Method

putDocument() public method

public putDocument ( Path $p, $contentType, $documentData, array $ifMatch = null, array $ifNoneMatch = null )
$p Path
$ifMatch array
$ifNoneMatch array
    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));
        }
    }

Usage Example

 public function putDocument(Request $request, TokenInfo $tokenInfo)
 {
     $path = new Path($request->getUrl()->getPathInfo());
     if ($path->getUserId() !== $tokenInfo->getUserId()) {
         throw new ForbiddenException('path does not match authorized subject');
     }
     if (!$this->hasWriteScope($tokenInfo->getScope(), $path->getModuleName())) {
         throw new ForbiddenException('path does not match authorized scope');
     }
     $ifMatch = $this->stripQuotes($request->getHeader('If-Match'));
     $ifNoneMatch = $this->stripQuotes($request->getHeader('If-None-Match'));
     $documentVersion = $this->remoteStorage->getVersion($path);
     if (null !== $ifMatch && !in_array($documentVersion, $ifMatch)) {
         throw new PreconditionFailedException('version mismatch');
     }
     if (null !== $ifNoneMatch && in_array('*', $ifNoneMatch) && null !== $documentVersion) {
         throw new PreconditionFailedException('document already exists');
     }
     $x = $this->remoteStorage->putDocument($path, $request->getHeader('Content-Type'), $request->getBody(), $ifMatch, $ifNoneMatch);
     // we have to get the version again after the PUT
     $documentVersion = $this->remoteStorage->getVersion($path);
     $rsr = new Response();
     $rsr->setHeader('ETag', '"' . $documentVersion . '"');
     $rsr->setBody($x);
     return $rsr;
 }