Skip to content

Commit 0d626da

Browse files
committed
Merge pull request #73 from php-http/phpdoc
cleanup phpdoc and BatchResult interface
2 parents 0d3bad2 + ff0a895 commit 0d626da

10 files changed

+54
-83
lines changed

spec/Exception/RequestExceptionSpec.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,4 @@ function it_has_a_request(RequestInterface $request)
2727
{
2828
$this->getRequest()->shouldReturn($request);
2929
}
30-
31-
function it_wraps_an_exception(RequestInterface $request)
32-
{
33-
$e = new \Exception('message');
34-
35-
$requestException = $this->wrapException($request, $e);
36-
37-
$requestException->getMessage()->shouldReturn('message');
38-
}
39-
40-
function it_does_not_wrap_if_request_exception(RequestInterface $request, RequestException $requestException)
41-
{
42-
$this->wrapException($request, $requestException)->shouldReturn($requestException);
43-
}
4430
}

spec/Exception/TransferExceptionSpec.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace spec\Http\Client\Exception;
44

5+
use Http\Client\Exception\TransferException;
56
use PhpSpec\ObjectBehavior;
6-
use Prophecy\Argument;
77

88
class TransferExceptionSpec extends ObjectBehavior
99
{
10-
function it_is_initializable()
10+
function let()
1111
{
12-
$this->shouldHaveType('Http\Client\Exception\TransferException');
12+
$this->beAnInstanceOf('spec\Http\Client\Exception\TransferExceptionStub');
1313
}
1414

1515
function it_is_a_runtime_exception()
@@ -22,3 +22,7 @@ function it_is_an_exception()
2222
$this->shouldImplement('Http\Client\Exception');
2323
}
2424
}
25+
26+
class TransferExceptionStub extends TransferException
27+
{
28+
}

src/BatchResult.php

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
*/
1414
interface BatchResult
1515
{
16+
/**
17+
* Checks if there are any successful responses at all.
18+
*
19+
* @return boolean
20+
*/
21+
public function hasResponses();
22+
1623
/**
1724
* Returns all successful responses.
1825
*
@@ -21,31 +28,24 @@ interface BatchResult
2128
public function getResponses();
2229

2330
/**
24-
* Returns the response for a successful request.
31+
* Checks if there is a successful response for a request.
2532
*
2633
* @param RequestInterface $request
2734
*
28-
* @return ResponseInterface
29-
*
30-
* @throws \UnexpectedValueException If request was not part of the batch or failed.
31-
*/
32-
public function getResponseFor(RequestInterface $request);
33-
34-
/**
35-
* Checks if there are any successful responses at all
36-
*
3735
* @return boolean
3836
*/
39-
public function hasResponses();
37+
public function isSuccessful(RequestInterface $request);
4038

4139
/**
42-
* Checks if there is a successful response for a request.
40+
* Returns the response for a successful request.
4341
*
4442
* @param RequestInterface $request
4543
*
4644
* @return ResponseInterface
45+
*
46+
* @throws \UnexpectedValueException If request was not part of the batch or failed.
4747
*/
48-
public function hasResponseFor(RequestInterface $request);
48+
public function getResponseFor(RequestInterface $request);
4949

5050
/**
5151
* Adds a response in an immutable way.
@@ -58,22 +58,11 @@ public function hasResponseFor(RequestInterface $request);
5858
public function addResponse(RequestInterface $request, ResponseInterface $response);
5959

6060
/**
61-
* Checks if a request was successful.
62-
*
63-
* @param RequestInterface $request
64-
*
65-
* @return boolean
66-
*/
67-
public function isSuccessful(RequestInterface $request);
68-
69-
/**
70-
* Checks if a request has failed.
71-
*
72-
* @param RequestInterface $request
61+
* Checks if there are any unsuccessful requests at all.
7362
*
7463
* @return boolean
7564
*/
76-
public function isFailed(RequestInterface $request);
65+
public function hasExceptions();
7766

7867
/**
7968
* Returns all exceptions for the unsuccessful requests.
@@ -83,24 +72,24 @@ public function isFailed(RequestInterface $request);
8372
public function getExceptions();
8473

8574
/**
86-
* Returns the exception for a failed request.
75+
* Checks if there is an exception for a request, meaning the request failed.
8776
*
8877
* @param RequestInterface $request
8978
*
90-
* @return Exception
91-
*
92-
* @throws \UnexpectedValueException If request was not part of the batch or was successful.
79+
* @return boolean
9380
*/
94-
public function getExceptionFor(RequestInterface $request);
81+
public function isFailed(RequestInterface $request);
9582

9683
/**
97-
* Checks if there is an exception for a request.
84+
* Returns the exception for a failed request.
9885
*
9986
* @param RequestInterface $request
10087
*
101-
* @return boolean
88+
* @return Exception
89+
*
90+
* @throws \UnexpectedValueException If request was not part of the batch or was successful.
10291
*/
103-
public function hasExceptionFor(RequestInterface $request);
92+
public function getExceptionFor(RequestInterface $request);
10493

10594
/**
10695
* Adds an exception in an immutable way.

src/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Http\Client;
44

55
/**
6-
* Every HTTP Client related Exception should implement this interface
6+
* Every HTTP Client related Exception must implement this interface
77
*
88
* @author Márk Sági-Kazár <[email protected]>
99
*/

src/Exception/BatchException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
use Http\Client\Exception;
77

88
/**
9-
* This exception is thrown when a batch of requests led to at least one failure.
9+
* This exception is thrown when HttpClient::sendRequests led to at least one failure.
1010
*
11-
* It holds the response/exception pairs and gives access to a BatchResult with the successful responses.
11+
* It gives access to a BatchResult with the request-exception and request-response pairs.
1212
*
1313
* @author Márk Sági-Kazár <[email protected]>
1414
*/
15-
final class BatchException extends \RuntimeException implements Exception
15+
final class BatchException extends TransferException implements Exception
1616
{
1717
/**
1818
* @var BatchResult

src/Exception/HttpException.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Thrown when a response was received but has an error status code.
1010
*
11-
* This exception always provides the request and response objects.
11+
* In addition to the request, this exception always provides access to the response object.
1212
*
1313
* @author Márk Sági-Kazár <[email protected]>
1414
*/
@@ -31,11 +31,10 @@ public function __construct(
3131
ResponseInterface $response,
3232
\Exception $previous = null
3333
) {
34+
parent::__construct($message, $request, $previous);
35+
3436
$this->response = $response;
3537
$this->code = $response->getStatusCode();
36-
37-
38-
parent::__construct($message, $request, $previous);
3938
}
4039

4140
/**

src/Exception/NetworkException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Http\Client\Exception;
44

55
/**
6-
* Thrown when the request cannot be completed caused by network issues
6+
* Thrown when the request cannot be completed because of network issues.
7+
*
8+
* There is no response object as this exception is thrown when no response has been received.
79
*
810
* @author Márk Sági-Kazár <[email protected]>
911
*/

src/Exception/RequestException.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Psr\Http\Message\RequestInterface;
66

77
/**
8-
* Base exception for when a request failed.
8+
* Exception for when a request failed, providing access to the failed request.
9+
*
10+
* This could be due to an invalid request, or one of the extending exceptions
11+
* for network errors or HTTP error responses.
912
*
1013
* @author Márk Sági-Kazár <[email protected]>
1114
*/
@@ -37,19 +40,4 @@ public function getRequest()
3740
{
3841
return $this->request;
3942
}
40-
41-
/**
42-
* @param RequestInterface $request
43-
* @param \Exception $e
44-
*
45-
* @return RequestException
46-
*/
47-
public static function wrapException(RequestInterface $request, \Exception $e)
48-
{
49-
if (!$e instanceof RequestException) {
50-
$e = new RequestException($e->getMessage(), $request, $e);
51-
}
52-
53-
return $e;
54-
}
5543
}

src/Exception/TransferException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
*
1010
* @author Márk Sági-Kazár <[email protected]>
1111
*/
12-
class TransferException extends \RuntimeException implements Exception
12+
abstract class TransferException extends \RuntimeException implements Exception
1313
{
1414
}

src/HttpClient.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
interface HttpClient
1717
{
1818
/**
19-
* Sends a PSR request
19+
* Sends a PSR-7 request.
2020
*
2121
* @param RequestInterface $request
2222
*
@@ -27,17 +27,20 @@ interface HttpClient
2727
public function sendRequest(RequestInterface $request);
2828

2929
/**
30-
* Sends PSR requests
30+
* Sends several PSR-7 requests.
3131
*
32-
* If one or more requests led to an exception, the BatchException is thrown.
33-
* The BatchException also gives access to the BatchResult for the successful responses.
32+
* If the client is able to, these requests should be sent in parallel. Otherwise they will be sent sequentially.
33+
* Either way, the caller may not rely on them being executed in any particular order.
34+
*
35+
* If one or more requests led to an exception, the BatchException is thrown. The BatchException gives access to the
36+
* BatchResult that contains responses for successful calls and exceptions for unsuccessful calls.
3437
*
3538
* @param RequestInterface[] $requests
3639
*
3740
* @return BatchResult If all requests where successful.
3841
*
39-
* @throws Exception
40-
* @throws BatchException
42+
* @throws Exception On general setup problems.
43+
* @throws BatchException If one or more requests led to exceptions.
4144
*/
4245
public function sendRequests(array $requests);
4346
}

0 commit comments

Comments
 (0)