diff --git a/spec/BatchRequestSpec.php b/spec/BatchClientSpec.php similarity index 58% rename from spec/BatchRequestSpec.php rename to spec/BatchClientSpec.php index 4f11880..2dd9361 100644 --- a/spec/BatchRequestSpec.php +++ b/spec/BatchClientSpec.php @@ -3,16 +3,15 @@ namespace spec\Http\Client\Utils; use Http\Client\HttpClient; -use Http\Client\Utils\BatchRequest; use PhpSpec\ObjectBehavior; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -class BatchRequestSpec extends ObjectBehavior +class BatchClientSpec extends ObjectBehavior { function let(HttpClient $client) { - $this->beAnInstanceOf('spec\Http\Client\Utils\BatchRequestStub', [$client]); + $this->beAnInstanceOf('Http\Client\Utils\BatchClient', [$client]); } function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2) @@ -20,7 +19,7 @@ function it_send_multiple_request_using_send_request(HttpClient $client, Request $client->sendRequest($request1)->willReturn($response1); $client->sendRequest($request2)->willReturn($response2); - $this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\BatchResult'); + $this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\Utils\BatchResult'); } function it_throw_batch_exception_if_one_or_more_request_failed(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response) @@ -28,26 +27,6 @@ function it_throw_batch_exception_if_one_or_more_request_failed(HttpClient $clie $client->sendRequest($request1)->willReturn($response); $client->sendRequest($request2)->willThrow('Http\Client\Exception\HttpException'); - $this->shouldThrow('Http\Client\Exception\BatchException')->duringSendRequests([$request1, $request2]); - } -} - -class BatchRequestStub implements HttpClient -{ - use BatchRequest; - - protected $client; - - public function __construct(HttpClient $client) - { - $this->client = $client; - } - - /** - * {@inheritdoc} - */ - public function sendRequest(RequestInterface $request) - { - return $this->client->sendRequest($request); + $this->shouldThrow('Http\Client\Utils\Exception\BatchException')->duringSendRequests([$request1, $request2]); } } diff --git a/spec/BatchResultSpec.php b/spec/BatchResultSpec.php index 84df31d..01c553d 100644 --- a/spec/BatchResultSpec.php +++ b/spec/BatchResultSpec.php @@ -12,7 +12,6 @@ class BatchResultSpec extends ObjectBehavior function it_is_initializable() { $this->beAnInstanceOf('Http\Client\Utils\BatchResult'); - $this->shouldImplement('Http\Client\BatchResult'); } function it_is_immutable(RequestInterface $request, ResponseInterface $response) diff --git a/spec/Exception/BatchExceptionSpec.php b/spec/Exception/BatchExceptionSpec.php new file mode 100644 index 0000000..4b8e117 --- /dev/null +++ b/spec/Exception/BatchExceptionSpec.php @@ -0,0 +1,37 @@ +beConstructedWith($batchResult); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Client\Utils\Exception\BatchException'); + } + + function it_is_a_runtime_exception() + { + $this->shouldHaveType('RuntimeException'); + } + + function it_is_an_exception() + { + $this->shouldImplement('Http\Client\Exception'); + } + + function it_has_a_batch_result() + { + $this->getResult()->shouldHaveType('Http\Client\Utils\BatchResult'); + } +} + \ No newline at end of file diff --git a/spec/HttpMethodsClientSpec.php b/spec/HttpMethodsClientSpec.php index 1c9e059..ffb45ea 100644 --- a/spec/HttpMethodsClientSpec.php +++ b/spec/HttpMethodsClientSpec.php @@ -85,14 +85,6 @@ function it_sends_request_with_underlying_client(HttpClient $client, MessageFact $this->beConstructedWith($client, $messageFactory); $this->sendRequest($request)->shouldReturn($response); } - - function it_sends_requests_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request1, RequestInterface $request2, BatchResult $batchResult) - { - $client->sendRequests([$request1, $request2])->shouldBeCalled()->willReturn($batchResult); - - $this->beConstructedWith($client, $messageFactory); - $this->sendRequests([$request1, $request2])->shouldReturn($batchResult); - } } class HttpMethodsClientStub extends HttpMethodsClient diff --git a/src/BatchRequest.php b/src/BatchClient.php similarity index 67% rename from src/BatchRequest.php rename to src/BatchClient.php index e441aef..2cc98c3 100644 --- a/src/BatchRequest.php +++ b/src/BatchClient.php @@ -3,24 +3,33 @@ namespace Http\Client\Utils; use Http\Client\Exception; -use Http\Client\Exception\BatchException; +use Http\Client\HttpClient; +use Http\Client\Utils\Exception\BatchException; use Psr\Http\Message\RequestInterface; /** - * Implements sending multiple request for client not supporting parallel requests. + * BatchClient allow to sends multiple request and retrieve a Batch Result * * This implementation simply loops over the requests and uses sendRequest to send each of them. * - * Use when implementing Http\Client\HttpClient. - * * @author Joel Wurtz */ -trait BatchRequest +class BatchClient implements HttpClient { + private $client; + + public function __construct(HttpClient $client) + { + $this->client = $client; + } + /** * {@inheritdoc} */ - abstract public function sendRequest(RequestInterface $request); + public function sendRequest(RequestInterface $request) + { + return $this->client->sendRequest($request); + } /** * {@inheritdoc} diff --git a/src/BatchResult.php b/src/BatchResult.php index ac11663..fbdeadf 100644 --- a/src/BatchResult.php +++ b/src/BatchResult.php @@ -3,7 +3,6 @@ namespace Http\Client\Utils; use Http\Client\Exception; -use Http\Client\BatchResult as BatchResultInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -12,7 +11,7 @@ * * @author Márk Sági-Kazár */ -final class BatchResult implements BatchResultInterface +final class BatchResult { /** * @var \SplObjectStorage @@ -31,7 +30,9 @@ public function __construct() } /** - * {@inheritDoc} + * Checks if there are any successful responses at all. + * + * @return boolean */ public function hasResponses() { @@ -39,7 +40,9 @@ public function hasResponses() } /** - * {@inheritDoc} + * Returns all successful responses. + * + * @return ResponseInterface[] */ public function getResponses() { @@ -53,7 +56,11 @@ public function getResponses() } /** - * {@inheritDoc} + * Checks if there is a successful response for a request. + * + * @param RequestInterface $request + * + * @return boolean */ public function isSuccessful(RequestInterface $request) { @@ -61,7 +68,13 @@ public function isSuccessful(RequestInterface $request) } /** - * {@inheritDoc} + * Returns the response for a successful request. + * + * @param RequestInterface $request + * + * @return ResponseInterface + * + * @throws \UnexpectedValueException If request was not part of the batch or failed. */ public function getResponseFor(RequestInterface $request) { @@ -73,7 +86,12 @@ public function getResponseFor(RequestInterface $request) } /** - * {@inheritDoc} + * Adds a response in an immutable way. + * + * @param RequestInterface $request + * @param ResponseInterface $response + * + * @return BatchResult the new BatchResult with this request-response pair added to it. */ public function addResponse(RequestInterface $request, ResponseInterface $response) { @@ -84,7 +102,9 @@ public function addResponse(RequestInterface $request, ResponseInterface $respon } /** - * {@inheritDoc} + * Checks if there are any unsuccessful requests at all. + * + * @return boolean */ public function hasExceptions() { @@ -92,7 +112,9 @@ public function hasExceptions() } /** - * {@inheritDoc} + * Returns all exceptions for the unsuccessful requests. + * + * @return Exception[] */ public function getExceptions() { @@ -106,7 +128,11 @@ public function getExceptions() } /** - * {@inheritDoc} + * Checks if there is an exception for a request, meaning the request failed. + * + * @param RequestInterface $request + * + * @return boolean */ public function isFailed(RequestInterface $request) { @@ -114,7 +140,13 @@ public function isFailed(RequestInterface $request) } /** - * {@inheritDoc} + * Returns the exception for a failed request. + * + * @param RequestInterface $request + * + * @return Exception + * + * @throws \UnexpectedValueException If request was not part of the batch or was successful. */ public function getExceptionFor(RequestInterface $request) { @@ -126,7 +158,12 @@ public function getExceptionFor(RequestInterface $request) } /** - * {@inheritDoc} + * Adds an exception in an immutable way. + * + * @param RequestInterface $request + * @param Exception $exception + * + * @return BatchResult the new BatchResult with this request-exception pair added to it. */ public function addException(RequestInterface $request, Exception $exception) { diff --git a/src/Exception/BatchException.php b/src/Exception/BatchException.php new file mode 100644 index 0000000..8469c4a --- /dev/null +++ b/src/Exception/BatchException.php @@ -0,0 +1,39 @@ + + */ +final class BatchException extends TransferException implements Exception +{ + /** + * @var BatchResult + */ + private $result; + + /** + * @param BatchResult $result + */ + public function __construct(BatchResult $result) + { + $this->result = $result; + } + /** + * Returns the BatchResult that contains all responses and exceptions + * + * @return BatchResult + */ + public function getResult() + { + return $this->result; + } +} diff --git a/src/HttpMethodsClient.php b/src/HttpMethodsClient.php index 60ea580..87b04ff 100644 --- a/src/HttpMethodsClient.php +++ b/src/HttpMethodsClient.php @@ -205,14 +205,4 @@ public function sendRequest(RequestInterface $request) { return $this->httpClient->sendRequest($request); } - - /** - * Forward to the underlying HttpClient. - * - * {@inheritdoc} - */ - public function sendRequests(array $requests) - { - return $this->httpClient->sendRequests($requests); - } }