Skip to content

Commit ace8b53

Browse files
committed
use interface suffice to avoid name collisions without BC break
1 parent f6224a9 commit ace8b53

13 files changed

+330
-330
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`)
99
- Made classes final that are not intended to be extended.
1010
Added interfaces for BatchClient, HttpClientRouter and HttpMethodsClient.
11-
The implementations of those utilities have been renamed with an `Impl` suffix.
11+
(These interfaces use the `Interface` suffix to avoid name collisions.)
1212
- Added an interface for HttpClientPool and moved the abstract class to the HttpClientPool sub namespace.
1313

1414
### Removed

spec/BatchClientImplSpec.php renamed to spec/BatchClientSpec.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use PhpSpec\ObjectBehavior;
9-
use Http\Client\Common\BatchClientImpl;
9+
use Http\Client\Common\BatchClient;
1010
use Http\Client\Common\BatchResult;
1111
use Http\Client\Exception\HttpException;
1212
use Http\Client\Common\Exception\BatchException;
1313

14-
class BatchClientImplSpec extends ObjectBehavior
14+
class BatchClientSpec extends ObjectBehavior
1515
{
1616
public function let(HttpClient $client)
1717
{
18-
$this->beAnInstanceOf(BatchClientImpl::class, [$client]);
18+
$this->beAnInstanceOf(BatchClient::class, [$client]);
1919
}
2020

2121
public function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2)

spec/HttpClientRouterImplSpec.php renamed to spec/HttpClientRouterSpec.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
namespace spec\Http\Client\Common;
44

5-
use Http\Client\Common\HttpClientRouterImpl;
5+
use Http\Client\Common\HttpClientRouter;
66
use Http\Message\RequestMatcher;
77
use Http\Client\HttpAsyncClient;
88
use Http\Client\HttpClient;
99
use Http\Promise\Promise;
1010
use Psr\Http\Message\RequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
1212
use PhpSpec\ObjectBehavior;
13-
use Http\Client\Common\HttpClientRouter;
13+
use Http\Client\Common\HttpClientRouterInterface;
1414
use Http\Client\Exception\RequestException;
1515

16-
class HttpClientRouterImplSpec extends ObjectBehavior
16+
class HttpClientRouterSpec extends ObjectBehavior
1717
{
1818
public function it_is_initializable()
1919
{
20-
$this->shouldHaveType(HttpClientRouterImpl::class);
20+
$this->shouldHaveType(HttpClientRouter::class);
2121
}
2222

2323
public function it_is_an_http_client_router()
2424
{
25-
$this->shouldImplement(HttpClientRouter::class);
25+
$this->shouldImplement(HttpClientRouterInterface::class);
2626
}
2727

2828
public function it_is_an_http_client()

spec/HttpMethodsClientImplSpec.php renamed to spec/HttpMethodsClientSpec.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace spec\Http\Client\Common;
44

5-
use Http\Client\Common\HttpMethodsClientImpl;
5+
use Http\Client\Common\HttpMethodsClient;
66
use Http\Client\HttpClient;
77
use Http\Message\RequestFactory;
88
use PhpSpec\ObjectBehavior;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
1111

12-
class HttpMethodsClientImplSpec extends ObjectBehavior
12+
class HttpMethodsClientSpec extends ObjectBehavior
1313
{
1414
private static $requestData = [
1515
'uri' => '/uri',
@@ -22,7 +22,7 @@ class HttpMethodsClientImplSpec extends ObjectBehavior
2222
public function let(HttpClient $client, RequestFactory $requestFactory)
2323
{
2424
$this->beAnInstanceOf(
25-
HttpMethodsClientImpl::class, [
25+
HttpMethodsClient::class, [
2626
$client,
2727
$requestFactory,
2828
]

src/BatchClient.php

+34-21
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,42 @@
66
use Http\Client\HttpClient;
77
use Http\Client\Common\Exception\BatchException;
88
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
910

10-
/**
11-
* BatchClient allow to sends multiple request and retrieve a Batch Result.
12-
*
13-
* This implementation simply loops over the requests and uses sendRequest with each of them.
14-
*
15-
* @author Joel Wurtz <jwurtz@jolicode.com>
16-
*/
17-
interface BatchClient extends HttpClient
11+
final class BatchClient implements BatchClientInterface
1812
{
1913
/**
20-
* Send several requests.
21-
*
22-
* You may not assume that the requests are executed in a particular order. If the order matters
23-
* for your application, use sendRequest sequentially.
24-
*
25-
* @param RequestInterface[] The requests to send
26-
*
27-
* @return BatchResult Containing one result per request
28-
*
29-
* @throws BatchException If one or more requests fails. The exception gives access to the
30-
* BatchResult with a map of request to result for success, request to
31-
* exception for failures
14+
* @var HttpClient
3215
*/
33-
public function sendRequests(array $requests): BatchResult;
16+
private $client;
17+
18+
public function __construct(HttpClient $client)
19+
{
20+
$this->client = $client;
21+
}
22+
23+
public function sendRequest(RequestInterface $request): ResponseInterface
24+
{
25+
return $this->client->sendRequest($request);
26+
}
27+
28+
public function sendRequests(array $requests): BatchResult
29+
{
30+
$batchResult = new BatchResult();
31+
32+
foreach ($requests as $request) {
33+
try {
34+
$response = $this->sendRequest($request);
35+
$batchResult = $batchResult->addResponse($request, $response);
36+
} catch (Exception $e) {
37+
$batchResult = $batchResult->addException($request, $e);
38+
}
39+
}
40+
41+
if ($batchResult->hasExceptions()) {
42+
throw new BatchException($batchResult);
43+
}
44+
45+
return $batchResult;
46+
}
3447
}

src/BatchClientImpl.php

-47
This file was deleted.

src/BatchClientInterface.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Http\Client\Common;
4+
5+
use Http\Client\Exception;
6+
use Http\Client\HttpClient;
7+
use Http\Client\Common\Exception\BatchException;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
/**
11+
* BatchClient allow to sends multiple request and retrieve a Batch Result.
12+
*
13+
* This implementation simply loops over the requests and uses sendRequest with each of them.
14+
*
15+
* @author Joel Wurtz <jwurtz@jolicode.com>
16+
*/
17+
interface BatchClientInterface extends HttpClient
18+
{
19+
/**
20+
* Send several requests.
21+
*
22+
* You may not assume that the requests are executed in a particular order. If the order matters
23+
* for your application, use sendRequest sequentially.
24+
*
25+
* @param RequestInterface[] The requests to send
26+
*
27+
* @return BatchResult Containing one result per request
28+
*
29+
* @throws BatchException If one or more requests fails. The exception gives access to the
30+
* BatchResult with a map of request to result for success, request to
31+
* exception for failures
32+
*/
33+
public function sendRequests(array $requests): BatchResult;
34+
}

src/HttpClientRouter.php

+49-5
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,67 @@
22

33
namespace Http\Client\Common;
44

5+
use Http\Client\Exception\RequestException;
56
use Http\Client\HttpAsyncClient;
67
use Http\Client\HttpClient;
78
use Http\Message\RequestMatcher;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
811

912
/**
10-
* Route a request to a specific client in the stack based using a RequestMatcher.
11-
*
12-
* This is not a HttpClientPool client because it uses a matcher to select the client.
13+
* {@inheritdoc}
1314
*
1415
* @author Joel Wurtz <joel.wurtz@gmail.com>
1516
*/
16-
interface HttpClientRouter extends HttpClient, HttpAsyncClient
17+
final class HttpClientRouter implements HttpClientRouterInterface
1718
{
19+
/**
20+
* @var array
21+
*/
22+
private $clients = [];
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function sendRequest(RequestInterface $request): ResponseInterface
28+
{
29+
return $this->chooseHttpClient($request)->sendRequest($request);
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function sendAsyncRequest(RequestInterface $request)
36+
{
37+
return $this->chooseHttpClient($request)->sendAsyncRequest($request);
38+
}
39+
1840
/**
1941
* Add a client to the router.
2042
*
2143
* @param HttpClient|HttpAsyncClient $client
2244
*/
23-
public function addClient($client, RequestMatcher $requestMatcher);
45+
public function addClient($client, RequestMatcher $requestMatcher)
46+
{
47+
$this->clients[] = [
48+
'matcher' => $requestMatcher,
49+
'client' => new FlexibleHttpClient($client),
50+
];
51+
}
52+
53+
/**
54+
* Choose an HTTP client given a specific request.
55+
*
56+
* @return HttpClient|HttpAsyncClient
57+
*/
58+
private function chooseHttpClient(RequestInterface $request)
59+
{
60+
foreach ($this->clients as $client) {
61+
if ($client['matcher']->matches($request)) {
62+
return $client['client'];
63+
}
64+
}
65+
66+
throw new RequestException('No client found for the specified request', $request);
67+
}
2468
}

src/HttpClientRouterImpl.php

-68
This file was deleted.

0 commit comments

Comments
 (0)