From 9aa820c5382dd4c2d8e4d99ffdc0de10e840479c Mon Sep 17 00:00:00 2001 From: nruest Date: Sun, 8 Nov 2015 13:56:50 -0500 Subject: [PATCH 1/7] Implement copyResource -- Address #10. --- src/Chullo.php | 27 ++++++++++++++ src/IFedoraClient.php | 10 +++++ test/CopyResourceTest.php | 78 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 test/CopyResourceTest.php diff --git a/src/Chullo.php b/src/Chullo.php index 964d7fa..0b42cf6 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -310,6 +310,33 @@ protected function prepareUri($uri, $transaction = "") { return implode([$base_uri, $transaction, $relative_path], '/'); } + /** + * Issues a COPY request to Fedora. + * + * @param string $uri Resource URI + * @param array $destination Destination URI + * + * @return string + */ + public function copyResource($uri, + $destination) { + // Create destinsation URI + $destination_uri = "Destination: " . $destination; + // Create destination array + $destination = array( + 'Destination' => $destination_uri, + 'Overwrite' => 'T' + ); + $response = $this->client->request( + 'COPY', + $uri, + $destination + ); + + // Return the value of the location header + $locations = $response->getHeader('Location'); + return reset($locations); + } /** * Creates a new transaction. diff --git a/src/IFedoraClient.php b/src/IFedoraClient.php index 3ea46cb..68a33da 100644 --- a/src/IFedoraClient.php +++ b/src/IFedoraClient.php @@ -133,6 +133,16 @@ public function modifyResource($uri, */ public function deleteResource($uri, $transaction = ""); + /** + * Issues a COPY request to Fedora. + * + * @param string $uri Resource URI + * @param array $destination Destination URI + * + * @return string + */ + public function copyResource($uri, + $destination); /** * Creates a new transaction. diff --git a/test/CopyResourceTest.php b/test/CopyResourceTest.php new file mode 100644 index 0000000..f092f5b --- /dev/null +++ b/test/CopyResourceTest.php @@ -0,0 +1,78 @@ + "http://localhost:8080/fcrepo/rest/SOME_URI"]), + ]); + + $handler = HandlerStack::create($mock); + $guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']); + $client = new Chullo($guzzle); + + $result = $client->copyResource("",""); + $this->assertSame($result, "http://localhost:8080/fcrepo/rest/SOME_URI"); + } + + /** + * @covers Islandora\Fedora\Chullo::copyResource + * @uses GuzzleHttp\Client + * @expectedException GuzzleHttp\Exception\ClientException + */ + public function testThrowsExceptionOn404() { + $mock = new MockHandler([ + new Response(404), + ]); + + $handler = HandlerStack::create($mock); + $guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']); + $client = new Chullo($guzzle); + + $result = $client->copyResource("",""); + } + + /** + * @covers Islandora\Fedora\Chullo::copyResource + * @uses GuzzleHttp\Client + * @expectedException GuzzleHttp\Exception\ClientException + */ + public function testThrowsExceptionOn409() { + $mock = new MockHandler([ + new Response(409), + ]); + + $handler = HandlerStack::create($mock); + $guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']); + $client = new Chullo($guzzle); + + $result = $client->copyResource("",""); + } + + /** + * @covers Islandora\Fedora\Chullo::copyResource + * @uses GuzzleHttp\Client + * @expectedException GuzzleHttp\Exception\ServerException + */ + public function testThrowsExceptionOn502() { + $mock = new MockHandler([ + new Response(502), + ]); + + $handler = HandlerStack::create($mock); + $guzzle = new Client(['handler' => $handler, 'base_uri' => 'http://localhost:8080/fcrepo/rest']); + $client = new Chullo($guzzle); + + $result = $client->copyResource("",""); + } +} From fa4126b7753d6b2b551d6b6bf63a65a7c45d38da Mon Sep 17 00:00:00 2001 From: nruest Date: Mon, 9 Nov 2015 09:50:20 -0500 Subject: [PATCH 2/7] Add transactions back in. --- src/Chullo.php | 6 +++++- src/IFedoraClient.php | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Chullo.php b/src/Chullo.php index 0b42cf6..cea6d7f 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -315,11 +315,15 @@ protected function prepareUri($uri, $transaction = "") { * * @param string $uri Resource URI * @param array $destination Destination URI + * @param string $transaction Transaction id * * @return string */ public function copyResource($uri, - $destination) { + $destination, + $transaction = "") { + // Ensure uri takes transaction into account. + $uri = $this->prepareUri($uri, $transaction); // Create destinsation URI $destination_uri = "Destination: " . $destination; // Create destination array diff --git a/src/IFedoraClient.php b/src/IFedoraClient.php index 68a33da..b339a84 100644 --- a/src/IFedoraClient.php +++ b/src/IFedoraClient.php @@ -138,11 +138,13 @@ public function deleteResource($uri, * * @param string $uri Resource URI * @param array $destination Destination URI + * @param string $transaction Transaction id * * @return string */ public function copyResource($uri, - $destination); + $destination, + $transaction = ""); /** * Creates a new transaction. From 7df296121b79df42085308663c08bbece14a5cef Mon Sep 17 00:00:00 2001 From: nruest Date: Thu, 3 Dec 2015 12:46:20 -0500 Subject: [PATCH 3/7] Update for code review. --- src/Chullo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chullo.php b/src/Chullo.php index cea6d7f..09dae82 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -325,7 +325,7 @@ public function copyResource($uri, // Ensure uri takes transaction into account. $uri = $this->prepareUri($uri, $transaction); // Create destinsation URI - $destination_uri = "Destination: " . $destination; + $destination_uri = "Destination: " . $this->prepareUri($destination, $transaction); // Create destination array $destination = array( 'Destination' => $destination_uri, From 6f017f738712a40eaa938db0f1d7cd6c61e122f2 Mon Sep 17 00:00:00 2001 From: nruest Date: Mon, 7 Dec 2015 11:03:30 -0500 Subject: [PATCH 4/7] Update for code review. --- src/Chullo.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Chullo.php b/src/Chullo.php index 09dae82..25dea03 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -325,16 +325,18 @@ public function copyResource($uri, // Ensure uri takes transaction into account. $uri = $this->prepareUri($uri, $transaction); // Create destinsation URI - $destination_uri = "Destination: " . $this->prepareUri($destination, $transaction); + $destination_uri = $this->prepareUri($destination, $transaction); // Create destination array - $destination = array( - 'Destination' => $destination_uri, - 'Overwrite' => 'T' - ); + $options = [ + 'headers' => [ + 'Destination' => $destination_uri, + 'Overwrite' => 'T' + ], + ]; $response = $this->client->request( 'COPY', $uri, - $destination + $options ); // Return the value of the location header From 0198354a991720dd0354cc52ab683f6e9c00a21e Mon Sep 17 00:00:00 2001 From: nruest Date: Mon, 7 Dec 2015 11:11:45 -0500 Subject: [PATCH 5/7] Clean up merge facepalms. --- src/Chullo.php | 16 ++++++---------- src/IFedoraClient.php | 4 ++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Chullo.php b/src/Chullo.php index c04ec63..874f79e 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -349,8 +349,6 @@ public function copyResource($uri, // Ensure uri takes transaction into account. $uri = $this->prepareUri($uri, $transaction); // Create destinsation URI - $destination_uri = $this->prepareUri($destination, $transaction); - // Create destinsation URI $destination_uri = $this->prepareUri($destination, $transaction); // Create destination array $options = [ @@ -360,9 +358,9 @@ public function copyResource($uri, ], ]; $response = $this->client->request( - 'COPY', - $uri, - $options + 'COPY', + $uri, + $options ); // Return the value of the location header @@ -385,8 +383,6 @@ public function moveResource($uri, // Ensure uri takes transaction into account. $uri = $this->prepareUri($uri, $transaction); // Create destinsation URI - $destination_uri = $this->prepareUri($destination, $transaction); - // Create destinsation URI $destination_uri = $this->prepareUri($destination, $transaction); // Create destination array $options = [ @@ -396,9 +392,9 @@ public function moveResource($uri, ], ]; $response = $this->client->request( - 'MOVE', - $uri, - $options + 'MOVE', + $uri, + $options ); // Return the value of the location header diff --git a/src/IFedoraClient.php b/src/IFedoraClient.php index 9ce3c24..7ecf84b 100644 --- a/src/IFedoraClient.php +++ b/src/IFedoraClient.php @@ -150,7 +150,7 @@ public function deleteResource($uri, * @param array $destination Destination URI * @param string $transaction Transaction id * - * @return string + * @return string */ public function copyResource($uri, $destination, @@ -162,7 +162,7 @@ public function copyResource($uri, * @param array $destination Destination URI * @param string $transaction Transaction id * - * @return string + * @return string */ public function moveResource($uri, $destination, From eaa0701a14eb14098ca849d22dc5a1ea6b1fa493 Mon Sep 17 00:00:00 2001 From: nruest Date: Mon, 7 Dec 2015 11:14:00 -0500 Subject: [PATCH 6/7] whitespace --- src/Chullo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chullo.php b/src/Chullo.php index 874f79e..9b23718 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -348,7 +348,7 @@ public function copyResource($uri, $transaction = "") { // Ensure uri takes transaction into account. $uri = $this->prepareUri($uri, $transaction); - // Create destinsation URI + // Create destinsation URI $destination_uri = $this->prepareUri($destination, $transaction); // Create destination array $options = [ From 2c5ce7314034660f5987ad8cde4676ab4c05133b Mon Sep 17 00:00:00 2001 From: nruest Date: Mon, 7 Dec 2015 11:14:38 -0500 Subject: [PATCH 7/7] whitespace --- src/Chullo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chullo.php b/src/Chullo.php index 9b23718..7abaf6b 100644 --- a/src/Chullo.php +++ b/src/Chullo.php @@ -382,7 +382,7 @@ public function moveResource($uri, $transaction = "") { // Ensure uri takes transaction into account. $uri = $this->prepareUri($uri, $transaction); - // Create destinsation URI + // Create destinsation URI $destination_uri = $this->prepareUri($destination, $transaction); // Create destination array $options = [