Skip to content

Commit

Permalink
More code reaping (#61)
Browse files Browse the repository at this point in the history
* Remove the remainder of transactions
* Follow-up to #60
* Also addresses Islandora/documentation#581
  • Loading branch information
ruebot authored and dannylamb committed Apr 3, 2017
1 parent 9927a11 commit 8df2117
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 132 deletions.
55 changes: 16 additions & 39 deletions src/Chullo.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,16 @@ public function getBaseUri()
*
* @param string $uri Resource URI
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return mixed Full response if found. Null otherwise.
*/
public function getResource(
$uri = "",
$headers = [],
$transaction = ""
$headers = []
) {
$response = $this->api->getResource(
$uri,
$headers,
$transaction
$headers
);
if ($response->getStatusCode() != 200) {
return null;
Expand All @@ -83,17 +80,14 @@ public function getResource(
* Gets a Fedora resource's headers.
*
* @param string $uri Resource URI
* @param string $transaction Transaction id
*
* @return array Headers of a resource, null on failure
*/
public function getResourceHeaders(
$uri = "",
$transaction = ""
$uri = ""
) {
$response = $this->api->getResourceHeaders(
$uri,
$transaction
$uri
);

if ($response->getStatusCode() != 200) {
Expand Down Expand Up @@ -124,17 +118,15 @@ public function getResourceOptions($uri = "")
*
* @param string $uri Resource URI
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return EasyRdf_Graph EasyRdf_Graph if found, null otherwise
*/
public function getGraph(
$uri = "",
$headers = [],
$transaction = ""
$headers = []
) {
$headers['Accept'] = 'application/ld+json';
$rdf = $this->getResource($uri, $headers, $transaction);
$rdf = $this->getResource($uri, $headers);
if (empty($rdf)) {
return null;
}
Expand All @@ -150,21 +142,18 @@ public function getGraph(
* @param string $uri Resource URI
* @param string $content String or binary content
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return string Uri of newly created resource or null if failed
*/
public function createResource(
$uri = "",
$content = null,
$headers = [],
$transaction = ""
$headers = []
) {
$response = $this->api->createResource(
$uri,
$content,
$headers,
$transaction
$headers
);

if ($response->getStatusCode() != 201) {
Expand All @@ -182,21 +171,18 @@ public function createResource(
* @param string $uri Resource URI
* @param string $content String or binary content
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return boolean True if successful
*/
public function saveResource(
$uri,
$content = null,
$headers = [],
$transaction = ""
$headers = []
) {
$response = $this->api->saveResource(
$uri,
$content,
$headers,
$transaction
$headers
);

return $response->getStatusCode() == 204;
Expand All @@ -207,14 +193,12 @@ public function saveResource(
*
* @param string $uri Resource URI
* @param EasyRdf_Resource $graph Graph to save
* @param string $transaction Transaction id
*
* @return boolean True if successful
*/
public function saveGraph(
$uri,
\EasyRdf_Graph $graph,
$transaction = ""
\EasyRdf_Graph $graph
) {
// Serialze the rdf.
$turtle = $graph->serialise('turtle');
Expand All @@ -229,8 +213,7 @@ public function saveGraph(
[
'Content-Type' => 'text/turtle',
'digest' => 'sha1='.$checksum_value
],
$transaction
]
);
}

Expand All @@ -240,21 +223,18 @@ public function saveGraph(
* @param string $uri Resource URI
* @param string $sparql SPARQL Update query
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return boolean True if successful
*/
public function modifyResource(
$uri,
$sparql = "",
$headers = [],
$transaction = ""
$headers = []
) {
$response = $this->api->modifyResource(
$uri,
$sparql,
$headers,
$transaction
$headers
);

return $response->getStatusCode() == 204;
Expand All @@ -264,17 +244,14 @@ public function modifyResource(
* Issues a DELETE request to Fedora.
*
* @param string $uri Resource URI
* @param string $transaction Transaction id
*
* @return boolean True if successful
*/
public function deleteResource(
$uri,
$transaction = ""
$uri
) {
$response = $this->api->deleteResource(
$uri,
$transaction
$uri
);

return $response->getStatusCode() == 204;
Expand Down
76 changes: 7 additions & 69 deletions src/FedoraApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,16 @@ public function getBaseUri()
*
* @param string $uri Resource URI
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return ResponseInterface
*/
public function getResource(
$uri = "",
$headers = [],
$transaction = ""
$headers = []
) {
// Set headers
$options = ['http_errors' => false, 'headers' => $headers];

// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);

// Send the request.
return $this->client->request(
'GET',
Expand All @@ -91,18 +86,13 @@ public function getResource(
* Gets a Fedora resoure's headers.
*
* @param string $uri Resource URI
* @param string $transaction Transaction id
*
* @return ResponseInterface
*/
public function getResourceHeaders(
$uri = "",
$transaction = ""
$uri = ""
) {

// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);

// Send the request.
return $this->client->request(
'HEAD',
Expand Down Expand Up @@ -133,15 +123,13 @@ public function getResourceOptions($uri = "")
* @param string $uri Resource URI
* @param string $content String or binary content
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return ResponseInterface
*/
public function createResource(
$uri = "",
$content = null,
$headers = [],
$transaction = ""
$headers = []
) {
$options = ['http_errors' => false];

Expand All @@ -150,10 +138,7 @@ public function createResource(

// Set headers.
$options['headers'] = $headers;

// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);


return $this->client->request(
'POST',
$uri,
Expand All @@ -167,15 +152,13 @@ public function createResource(
* @param string $uri Resource URI
* @param string $content String or binary content
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return ResponseInterface
*/
public function saveResource(
$uri,
$content = null,
$headers = [],
$transaction = ""
$headers = []
) {
$options = ['http_errors' => false];

Expand All @@ -185,9 +168,6 @@ public function saveResource(
// Set headers.
$options['headers'] = $headers;

// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);

return $this->client->request(
'PUT',
$uri,
Expand All @@ -201,15 +181,13 @@ public function saveResource(
* @param string $uri Resource URI
* @param string $sparql SPARQL Update query
* @param array $headers HTTP Headers
* @param string $transaction Transaction id
*
* @return ResponseInterface
*/
public function modifyResource(
$uri,
$sparql = "",
$headers = [],
$transaction = ""
$headers = []
) {
$options = ['http_errors' => false];

Expand All @@ -220,8 +198,6 @@ public function modifyResource(
$options['headers'] = $headers;
$options['headers']['content-type'] = 'application/sparql-update';

// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);
return $this->client->request(
'PATCH',
$uri,
Expand All @@ -233,56 +209,18 @@ public function modifyResource(
* Issues a DELETE request to Fedora.
*
* @param string $uri Resource URI
* @param string $transaction Transaction id
*
* @return ResponseInterface
*/
public function deleteResource(
$uri,
$transaction = ""
$uri
) {
$options = ['http_errors' => false];

// Ensure uri takes transaction into account.
$uri = $this->prepareUri($uri, $transaction);

return $this->client->request(
'DELETE',
$uri,
$options
);
}

protected function prepareUri($uri, $transaction = "")
{
$base_uri = rtrim($this->getBaseUri(), '/');

if (empty($uri)) {
return "$base_uri/$transaction";
}

if (strpos($uri, $base_uri) !== 0) {
$uri = $base_uri . '/' . ltrim($uri, '/');
}

$uri = rtrim($uri, '/');

if (strcmp($uri, $base_uri) == 0) {
return "$base_uri/$transaction";
}

if (empty($transaction)) {
return $uri;
}

$exploded = explode($base_uri, $uri);
$relative_path = ltrim($exploded[1], '/');
$exploded = explode('/', $relative_path);

if (in_array($transaction, $exploded)) {
return $uri;
}

return implode([$base_uri, $transaction, $relative_path], '/');
}
}
Loading

0 comments on commit 8df2117

Please sign in to comment.