Skip to content

Commit

Permalink
use interface suffice to avoid name collisions without BC break
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Dec 20, 2018
1 parent f6224a9 commit ace8b53
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 330 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -8,7 +8,7 @@
- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`)
- Made classes final that are not intended to be extended.
Added interfaces for BatchClient, HttpClientRouter and HttpMethodsClient.
The implementations of those utilities have been renamed with an `Impl` suffix.
(These interfaces use the `Interface` suffix to avoid name collisions.)
- Added an interface for HttpClientPool and moved the abstract class to the HttpClientPool sub namespace.

### Removed
Expand Down
6 changes: 3 additions & 3 deletions spec/BatchClientImplSpec.php → spec/BatchClientSpec.php
Expand Up @@ -6,16 +6,16 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
use Http\Client\Common\BatchClientImpl;
use Http\Client\Common\BatchClient;
use Http\Client\Common\BatchResult;
use Http\Client\Exception\HttpException;
use Http\Client\Common\Exception\BatchException;

class BatchClientImplSpec extends ObjectBehavior
class BatchClientSpec extends ObjectBehavior
{
public function let(HttpClient $client)
{
$this->beAnInstanceOf(BatchClientImpl::class, [$client]);
$this->beAnInstanceOf(BatchClient::class, [$client]);
}

public function it_send_multiple_request_using_send_request(HttpClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2)
Expand Down
Expand Up @@ -2,27 +2,27 @@

namespace spec\Http\Client\Common;

use Http\Client\Common\HttpClientRouterImpl;
use Http\Client\Common\HttpClientRouter;
use Http\Message\RequestMatcher;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
use Http\Client\Common\HttpClientRouter;
use Http\Client\Common\HttpClientRouterInterface;
use Http\Client\Exception\RequestException;

class HttpClientRouterImplSpec extends ObjectBehavior
class HttpClientRouterSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(HttpClientRouterImpl::class);
$this->shouldHaveType(HttpClientRouter::class);
}

public function it_is_an_http_client_router()
{
$this->shouldImplement(HttpClientRouter::class);
$this->shouldImplement(HttpClientRouterInterface::class);
}

public function it_is_an_http_client()
Expand Down
Expand Up @@ -2,14 +2,14 @@

namespace spec\Http\Client\Common;

use Http\Client\Common\HttpMethodsClientImpl;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class HttpMethodsClientImplSpec extends ObjectBehavior
class HttpMethodsClientSpec extends ObjectBehavior
{
private static $requestData = [
'uri' => '/uri',
Expand All @@ -22,7 +22,7 @@ class HttpMethodsClientImplSpec extends ObjectBehavior
public function let(HttpClient $client, RequestFactory $requestFactory)
{
$this->beAnInstanceOf(
HttpMethodsClientImpl::class, [
HttpMethodsClient::class, [
$client,
$requestFactory,
]
Expand Down
55 changes: 34 additions & 21 deletions src/BatchClient.php
Expand Up @@ -6,29 +6,42 @@
use Http\Client\HttpClient;
use Http\Client\Common\Exception\BatchException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* BatchClient allow to sends multiple request and retrieve a Batch Result.
*
* This implementation simply loops over the requests and uses sendRequest with each of them.
*
* @author Joel Wurtz <jwurtz@jolicode.com>
*/
interface BatchClient extends HttpClient
final class BatchClient implements BatchClientInterface
{
/**
* Send several requests.
*
* You may not assume that the requests are executed in a particular order. If the order matters
* for your application, use sendRequest sequentially.
*
* @param RequestInterface[] The requests to send
*
* @return BatchResult Containing one result per request
*
* @throws BatchException If one or more requests fails. The exception gives access to the
* BatchResult with a map of request to result for success, request to
* exception for failures
* @var HttpClient
*/
public function sendRequests(array $requests): BatchResult;
private $client;

public function __construct(HttpClient $client)
{
$this->client = $client;
}

public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->client->sendRequest($request);
}

public function sendRequests(array $requests): BatchResult
{
$batchResult = new BatchResult();

foreach ($requests as $request) {
try {
$response = $this->sendRequest($request);
$batchResult = $batchResult->addResponse($request, $response);
} catch (Exception $e) {
$batchResult = $batchResult->addException($request, $e);
}
}

if ($batchResult->hasExceptions()) {
throw new BatchException($batchResult);
}

return $batchResult;
}
}
47 changes: 0 additions & 47 deletions src/BatchClientImpl.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/BatchClientInterface.php
@@ -0,0 +1,34 @@
<?php

namespace Http\Client\Common;

use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Client\Common\Exception\BatchException;
use Psr\Http\Message\RequestInterface;

/**
* BatchClient allow to sends multiple request and retrieve a Batch Result.
*
* This implementation simply loops over the requests and uses sendRequest with each of them.
*
* @author Joel Wurtz <jwurtz@jolicode.com>
*/
interface BatchClientInterface extends HttpClient
{
/**
* Send several requests.
*
* You may not assume that the requests are executed in a particular order. If the order matters
* for your application, use sendRequest sequentially.
*
* @param RequestInterface[] The requests to send
*
* @return BatchResult Containing one result per request
*
* @throws BatchException If one or more requests fails. The exception gives access to the
* BatchResult with a map of request to result for success, request to
* exception for failures
*/
public function sendRequests(array $requests): BatchResult;
}
54 changes: 49 additions & 5 deletions src/HttpClientRouter.php
Expand Up @@ -2,23 +2,67 @@

namespace Http\Client\Common;

use Http\Client\Exception\RequestException;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Message\RequestMatcher;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* Route a request to a specific client in the stack based using a RequestMatcher.
*
* This is not a HttpClientPool client because it uses a matcher to select the client.
* {@inheritdoc}
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
interface HttpClientRouter extends HttpClient, HttpAsyncClient
final class HttpClientRouter implements HttpClientRouterInterface
{
/**
* @var array
*/
private $clients = [];

/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->chooseHttpClient($request)->sendRequest($request);
}

/**
* {@inheritdoc}
*/
public function sendAsyncRequest(RequestInterface $request)
{
return $this->chooseHttpClient($request)->sendAsyncRequest($request);
}

/**
* Add a client to the router.
*
* @param HttpClient|HttpAsyncClient $client
*/
public function addClient($client, RequestMatcher $requestMatcher);
public function addClient($client, RequestMatcher $requestMatcher)
{
$this->clients[] = [
'matcher' => $requestMatcher,
'client' => new FlexibleHttpClient($client),
];
}

/**
* Choose an HTTP client given a specific request.
*
* @return HttpClient|HttpAsyncClient
*/
private function chooseHttpClient(RequestInterface $request)
{
foreach ($this->clients as $client) {
if ($client['matcher']->matches($request)) {
return $client['client'];
}
}

throw new RequestException('No client found for the specified request', $request);
}
}
68 changes: 0 additions & 68 deletions src/HttpClientRouterImpl.php

This file was deleted.

0 comments on commit ace8b53

Please sign in to comment.