Skip to content

Commit

Permalink
Merge pull request #1 from TCB13/master
Browse files Browse the repository at this point in the history
CosmosDB triggers via addTrigger()
  • Loading branch information
Nuno Chaves authored Dec 10, 2018
2 parents fd0154a + 7ed5a33 commit 9224659
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
14 changes: 10 additions & 4 deletions src/CosmosDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,13 @@ public function getDocument($rid_id, $rid_col, $rid_doc)
* @param string $rid_id Resource ID
* @param string $rid_col Resource Collection ID
* @param string $json JSON request
* @param array $headers Optional headers to send along with the request
* @return string JSON response
*/
public function createDocument($rid_id, $rid_col, $json)
public function createDocument($rid_id, $rid_col, $json, array $headers = [])
{
$headers = $this->getAuthHeaders('POST', 'docs', $rid_col);
$authHeaders = $this->getAuthHeaders('POST', 'docs', $rid_col);
$headers = \array_merge($headers, $authHeaders);
$headers['Content-Length'] = strlen($json);
return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs", "POST", $headers, $json);
}
Expand All @@ -471,11 +473,13 @@ public function createDocument($rid_id, $rid_col, $json)
* @param string $rid_col Resource Collection ID
* @param string $rid_doc Resource Doc ID
* @param string $json JSON request
* @param array $headers Optional headers to send along with the request
* @return string JSON response
*/
public function replaceDocument($rid_id, $rid_col, $rid_doc, $json)
public function replaceDocument($rid_id, $rid_col, $rid_doc, $json, array $headers = [])
{
$headers = $this->getAuthHeaders('PUT', 'docs', $rid_doc);
$headers = \array_merge($headers, $authHeaders);
$headers['Content-Length'] = strlen($json);
return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs/" . $rid_doc, "PUT", $headers, $json);
}
Expand All @@ -488,11 +492,13 @@ public function replaceDocument($rid_id, $rid_col, $rid_doc, $json)
* @param string $rid_id Resource ID
* @param string $rid_col Resource Collection ID
* @param string $rid_doc Resource Doc ID
* @param array $headers Optional headers to send along with the request
* @return string JSON response
*/
public function deleteDocument($rid_id, $rid_col, $rid_doc)
public function deleteDocument($rid_id, $rid_col, $rid_doc, array $headers = [])
{
$headers = $this->getAuthHeaders('DELETE', 'docs', $rid_doc);
$headers = \array_merge($headers, $authHeaders);
$headers['Content-Length'] = '0';
return $this->request("/dbs/" . $rid_id . "/colls/" . $rid_col . "/docs/" . $rid_doc, "DELETE", $headers);
}
Expand Down
15 changes: 9 additions & 6 deletions src/CosmosDbCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ public function query($query, $params = [])
*
* @access public
* @param string $json JSON formatted document
* @param array $headers Optional headers to send along with the request
* @return string JSON strings
*/
public function createDocument($json)
public function createDocument($json, array $headers = [])
{
return $this->document_db->createDocument($this->rid_db, $this->rid_col, $json);
return $this->document_db->createDocument($this->rid_db, $this->rid_col, $json, $headers);
}

/**
Expand All @@ -87,23 +88,25 @@ public function createDocument($json)
* @access public
* @param string $rid document ResourceID (_rid)
* @param string $json JSON formatted document
* @param array $headers Optional headers to send along with the request
* @return string JSON strings
*/
public function replaceDocument($rid, $json)
public function replaceDocument($rid, $json, array $headers = [])
{
return $this->document_db->replaceDocument($this->rid_db, $this->rid_col, $rid, $json);
return $this->document_db->replaceDocument($this->rid_db, $this->rid_col, $rid, $json, $headers);
}

/**
* deleteDocument
*
* @access public
* @param string $rid document ResourceID (_rid)
* @param array $headers Optional headers to send along with the request
* @return string JSON strings
*/
public function deleteDocument($rid)
public function deleteDocument($rid, array $headers = [])
{
return $this->document_db->deleteDocument($this->rid_db, $this->rid_col, $rid);
return $this->document_db->deleteDocument($this->rid_db, $this->rid_col, $rid, $headers);
}

/*
Expand Down
52 changes: 47 additions & 5 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class QueryBuilder
private $where = "";
private $order = null;
private $limit = null;
private $triggers = [];
private $params = [];

private $response = null;
Expand Down Expand Up @@ -211,9 +212,9 @@ public function save($document)
}

$col = $this->connection->selectCollection($this->collection);
$result = $rid ?
$col->replaceDocument($rid, $document) :
$col->createDocument($document);
$result = $rid ?
$col->replaceDocument($rid, $document, $this->triggersAsHeaders("replace")) :
$col->createDocument($document, $this->triggersAsHeaders("create"));
$resultObj = json_decode($result);

if (isset($resultObj->code) && isset($resultObj->message)) {
Expand All @@ -223,6 +224,47 @@ public function save($document)
return $resultObj->_rid ?? null;
}

public function addTrigger(string $operation, string $type, string $id): self
{
$operation = \strtolower($operation);
if (!\in_array($operation, ["all", "create", "delete", "replace"]))
throw new \Exception("Trigger: Invalid operation \"{$operation}\"");

$type = \strtolower($type);
if (!\in_array($type, ["post", "pre"]))
throw new \Exception("Trigger: Invalid type \"{$type}\"");

if (!isset($this->triggers[$operation][$type]))
$this->triggers[$operation][$type] = [];

$this->triggers[$operation][$type][] = $id;
return $this;
}

protected function triggersAsHeaders(string $operation): array
{
$headers = [];

// Add headers for the current operation type at $operation (create|detete!replace)
if (isset($this->triggers[$operation])) {
foreach ($this->triggers[$operation] as $name => $ids) {
$ids = \is_array($ids) ? $ids : [$ids];
$headers["x-ms-documentdb-{$name}-trigger-include"] = \implode(",", $ids);
}
}

// Add headers for the special "all" operations type that should always run
if (isset($this->triggers["all"])) {
foreach ($this->triggers["all"] as $name => $ids) {
$headerKey = "x-ms-documentdb-{$name}-trigger-include";
$ids = \implode(",", \is_array($ids) ? $ids : [$ids]);
$headers[$headerKey] = isset($headers[$headerKey]) ? $headers[$headerKey] .= "," . $ids : $headers[$headerKey] = $ids;
}
}

return $headers;
}

/* DELETE */

/**
Expand All @@ -235,7 +277,7 @@ public function delete()

if ($doc) {
$col = $this->connection->selectCollection($this->collection);
$this->response = $col->deleteDocument($doc->_rid);
$this->response = $col->deleteDocument($doc->_rid, $this->triggersAsHeaders("delete"));
}

return $this;
Expand All @@ -252,7 +294,7 @@ public function deleteAll()

$response = [];
foreach ((array)$this->findAll()->toObject() as $doc) {
$response[] = $col->deleteDocument($doc->_rid);
$response[] = $col->deleteDocument($doc->_rid, $this->triggersAsHeaders("delete"));
}

$this->response = $response;
Expand Down

0 comments on commit 9224659

Please sign in to comment.