Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.

Commit a6dd2e6

Browse files
committed
Merge pull request #16 from joelwurtz/feature/batch-client
Add a concrete implementation for sending multiple requests
2 parents f3be834 + d2c85e8 commit a6dd2e6

File tree

8 files changed

+144
-62
lines changed

8 files changed

+144
-62
lines changed

spec/BatchRequestSpec.php renamed to spec/BatchClientSpec.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,30 @@
33
namespace spec\Http\Client\Utils;
44

55
use Http\Client\HttpClient;
6-
use Http\Client\Utils\BatchRequest;
76
use PhpSpec\ObjectBehavior;
87
use Psr\Http\Message\RequestInterface;
98
use Psr\Http\Message\ResponseInterface;
109

11-
class BatchRequestSpec extends ObjectBehavior
10+
class BatchClientSpec extends ObjectBehavior
1211
{
1312
function let(HttpClient $client)
1413
{
15-
$this->beAnInstanceOf('spec\Http\Client\Utils\BatchRequestStub', [$client]);
14+
$this->beAnInstanceOf('Http\Client\Utils\BatchClient', [$client]);
1615
}
1716

1817
function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2)
1918
{
2019
$client->sendRequest($request1)->willReturn($response1);
2120
$client->sendRequest($request2)->willReturn($response2);
2221

23-
$this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\BatchResult');
22+
$this->sendRequests([$request1, $request2])->shouldReturnAnInstanceOf('Http\Client\Utils\BatchResult');
2423
}
2524

2625
function it_throw_batch_exception_if_one_or_more_request_failed(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response)
2726
{
2827
$client->sendRequest($request1)->willReturn($response);
2928
$client->sendRequest($request2)->willThrow('Http\Client\Exception\HttpException');
3029

31-
$this->shouldThrow('Http\Client\Exception\BatchException')->duringSendRequests([$request1, $request2]);
32-
}
33-
}
34-
35-
class BatchRequestStub implements HttpClient
36-
{
37-
use BatchRequest;
38-
39-
protected $client;
40-
41-
public function __construct(HttpClient $client)
42-
{
43-
$this->client = $client;
44-
}
45-
46-
/**
47-
* {@inheritdoc}
48-
*/
49-
public function sendRequest(RequestInterface $request)
50-
{
51-
return $this->client->sendRequest($request);
30+
$this->shouldThrow('Http\Client\Utils\Exception\BatchException')->duringSendRequests([$request1, $request2]);
5231
}
5332
}

spec/BatchResultSpec.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class BatchResultSpec extends ObjectBehavior
1212
function it_is_initializable()
1313
{
1414
$this->beAnInstanceOf('Http\Client\Utils\BatchResult');
15-
$this->shouldImplement('Http\Client\BatchResult');
1615
}
1716

1817
function it_is_immutable(RequestInterface $request, ResponseInterface $response)

spec/Exception/BatchExceptionSpec.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Utils\Exception;
4+
5+
use Http\Client\Utils\BatchResult;
6+
use Http\Client\Exception;
7+
use PhpSpec\ObjectBehavior;
8+
9+
class BatchExceptionSpec extends ObjectBehavior
10+
{
11+
function let()
12+
{
13+
$batchResult = new BatchResult();
14+
$this->beConstructedWith($batchResult);
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Client\Utils\Exception\BatchException');
20+
}
21+
22+
function it_is_a_runtime_exception()
23+
{
24+
$this->shouldHaveType('RuntimeException');
25+
}
26+
27+
function it_is_an_exception()
28+
{
29+
$this->shouldImplement('Http\Client\Exception');
30+
}
31+
32+
function it_has_a_batch_result()
33+
{
34+
$this->getResult()->shouldHaveType('Http\Client\Utils\BatchResult');
35+
}
36+
}
37+

spec/HttpMethodsClientSpec.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@ function it_sends_request_with_underlying_client(HttpClient $client, MessageFact
8585
$this->beConstructedWith($client, $messageFactory);
8686
$this->sendRequest($request)->shouldReturn($response);
8787
}
88-
89-
function it_sends_requests_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request1, RequestInterface $request2, BatchResult $batchResult)
90-
{
91-
$client->sendRequests([$request1, $request2])->shouldBeCalled()->willReturn($batchResult);
92-
93-
$this->beConstructedWith($client, $messageFactory);
94-
$this->sendRequests([$request1, $request2])->shouldReturn($batchResult);
95-
}
9688
}
9789

9890
class HttpMethodsClientStub extends HttpMethodsClient

src/BatchRequest.php renamed to src/BatchClient.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@
33
namespace Http\Client\Utils;
44

55
use Http\Client\Exception;
6-
use Http\Client\Exception\BatchException;
6+
use Http\Client\HttpClient;
7+
use Http\Client\Utils\Exception\BatchException;
78
use Psr\Http\Message\RequestInterface;
89

910
/**
10-
* Implements sending multiple request for client not supporting parallel requests.
11+
* BatchClient allow to sends multiple request and retrieve a Batch Result
1112
*
1213
* This implementation simply loops over the requests and uses sendRequest to send each of them.
1314
*
14-
* Use when implementing Http\Client\HttpClient.
15-
*
1615
* @author Joel Wurtz <[email protected]>
1716
*/
18-
trait BatchRequest
17+
class BatchClient implements HttpClient
1918
{
19+
private $client;
20+
21+
public function __construct(HttpClient $client)
22+
{
23+
$this->client = $client;
24+
}
25+
2026
/**
2127
* {@inheritdoc}
2228
*/
23-
abstract public function sendRequest(RequestInterface $request);
29+
public function sendRequest(RequestInterface $request)
30+
{
31+
return $this->client->sendRequest($request);
32+
}
2433

2534
/**
2635
* {@inheritdoc}

src/BatchResult.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Http\Client\Utils;
44

55
use Http\Client\Exception;
6-
use Http\Client\BatchResult as BatchResultInterface;
76
use Psr\Http\Message\RequestInterface;
87
use Psr\Http\Message\ResponseInterface;
98

@@ -12,7 +11,7 @@
1211
*
1312
* @author Márk Sági-Kazár <[email protected]>
1413
*/
15-
final class BatchResult implements BatchResultInterface
14+
final class BatchResult
1615
{
1716
/**
1817
* @var \SplObjectStorage
@@ -31,15 +30,19 @@ public function __construct()
3130
}
3231

3332
/**
34-
* {@inheritDoc}
33+
* Checks if there are any successful responses at all.
34+
*
35+
* @return boolean
3536
*/
3637
public function hasResponses()
3738
{
3839
return $this->responses->count() > 0;
3940
}
4041

4142
/**
42-
* {@inheritDoc}
43+
* Returns all successful responses.
44+
*
45+
* @return ResponseInterface[]
4346
*/
4447
public function getResponses()
4548
{
@@ -53,15 +56,25 @@ public function getResponses()
5356
}
5457

5558
/**
56-
* {@inheritDoc}
59+
* Checks if there is a successful response for a request.
60+
*
61+
* @param RequestInterface $request
62+
*
63+
* @return boolean
5764
*/
5865
public function isSuccessful(RequestInterface $request)
5966
{
6067
return $this->responses->contains($request);
6168
}
6269

6370
/**
64-
* {@inheritDoc}
71+
* Returns the response for a successful request.
72+
*
73+
* @param RequestInterface $request
74+
*
75+
* @return ResponseInterface
76+
*
77+
* @throws \UnexpectedValueException If request was not part of the batch or failed.
6578
*/
6679
public function getResponseFor(RequestInterface $request)
6780
{
@@ -73,7 +86,12 @@ public function getResponseFor(RequestInterface $request)
7386
}
7487

7588
/**
76-
* {@inheritDoc}
89+
* Adds a response in an immutable way.
90+
*
91+
* @param RequestInterface $request
92+
* @param ResponseInterface $response
93+
*
94+
* @return BatchResult the new BatchResult with this request-response pair added to it.
7795
*/
7896
public function addResponse(RequestInterface $request, ResponseInterface $response)
7997
{
@@ -84,15 +102,19 @@ public function addResponse(RequestInterface $request, ResponseInterface $respon
84102
}
85103

86104
/**
87-
* {@inheritDoc}
105+
* Checks if there are any unsuccessful requests at all.
106+
*
107+
* @return boolean
88108
*/
89109
public function hasExceptions()
90110
{
91111
return $this->exceptions->count() > 0;
92112
}
93113

94114
/**
95-
* {@inheritDoc}
115+
* Returns all exceptions for the unsuccessful requests.
116+
*
117+
* @return Exception[]
96118
*/
97119
public function getExceptions()
98120
{
@@ -106,15 +128,25 @@ public function getExceptions()
106128
}
107129

108130
/**
109-
* {@inheritDoc}
131+
* Checks if there is an exception for a request, meaning the request failed.
132+
*
133+
* @param RequestInterface $request
134+
*
135+
* @return boolean
110136
*/
111137
public function isFailed(RequestInterface $request)
112138
{
113139
return $this->exceptions->contains($request);
114140
}
115141

116142
/**
117-
* {@inheritDoc}
143+
* Returns the exception for a failed request.
144+
*
145+
* @param RequestInterface $request
146+
*
147+
* @return Exception
148+
*
149+
* @throws \UnexpectedValueException If request was not part of the batch or was successful.
118150
*/
119151
public function getExceptionFor(RequestInterface $request)
120152
{
@@ -126,7 +158,12 @@ public function getExceptionFor(RequestInterface $request)
126158
}
127159

128160
/**
129-
* {@inheritDoc}
161+
* Adds an exception in an immutable way.
162+
*
163+
* @param RequestInterface $request
164+
* @param Exception $exception
165+
*
166+
* @return BatchResult the new BatchResult with this request-exception pair added to it.
130167
*/
131168
public function addException(RequestInterface $request, Exception $exception)
132169
{

src/Exception/BatchException.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Http\Client\Utils\Exception;
4+
5+
use Http\Client\Exception;
6+
use Http\Client\Exception\TransferException;
7+
use Http\Client\Utils\BatchResult;
8+
9+
/**
10+
* This exception is thrown when HttpClient::sendRequests led to at least one failure.
11+
*
12+
* It gives access to a BatchResult with the request-exception and request-response pairs.
13+
*
14+
* @author Márk Sági-Kazár <[email protected]>
15+
*/
16+
final class BatchException extends TransferException implements Exception
17+
{
18+
/**
19+
* @var BatchResult
20+
*/
21+
private $result;
22+
23+
/**
24+
* @param BatchResult $result
25+
*/
26+
public function __construct(BatchResult $result)
27+
{
28+
$this->result = $result;
29+
}
30+
/**
31+
* Returns the BatchResult that contains all responses and exceptions
32+
*
33+
* @return BatchResult
34+
*/
35+
public function getResult()
36+
{
37+
return $this->result;
38+
}
39+
}

src/HttpMethodsClient.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,4 @@ public function sendRequest(RequestInterface $request)
205205
{
206206
return $this->httpClient->sendRequest($request);
207207
}
208-
209-
/**
210-
* Forward to the underlying HttpClient.
211-
*
212-
* {@inheritdoc}
213-
*/
214-
public function sendRequests(array $requests)
215-
{
216-
return $this->httpClient->sendRequests($requests);
217-
}
218208
}

0 commit comments

Comments
 (0)