Skip to content

Commit 6a15990

Browse files
committed
mark classes as final and methods and properties as private
1 parent 14a2a39 commit 6a15990

16 files changed

+113
-140
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Changed
66
- Abstract method `HttpClientPool::chooseHttpClient()` has now an explicit return type (`Http\Client\Common\HttpClientPoolItem`)
77
- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`)
8+
- Made all classes final as they are not intended to be extended.
89

910
### Removed
1011
- Deprecated option `debug_plugins` has been removed from `PluginClient`

spec/HttpClientPoolItemSpec.php renamed to spec/HttpClientPool/HttpClientPoolItemImplSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace spec\Http\Client\Common;
3+
namespace spec\Http\Client\Common\HttpClientPool;
44

55
use Http\Client\Exception;
66
use Http\Client\Exception\TransferException;
@@ -14,7 +14,7 @@
1414
use Psr\Http\Message\ResponseInterface;
1515
use Http\Client\Exception\RequestException;
1616

17-
class HttpClientPoolItemSpec extends ObjectBehavior
17+
class HttpClientPoolItemImplSpec extends ObjectBehavior
1818
{
1919
public function let(HttpClient $httpClient)
2020
{

spec/HttpClientPool/LeastUsedClientPoolSpec.php

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

33
namespace spec\Http\Client\Common\HttpClientPool;
44

5-
use Http\Client\Common\HttpClientPoolItem;
5+
use Http\Client\Common\HttpClientPool\HttpClientPoolItem;
6+
use Http\Client\Common\HttpClientPool\HttpClientPoolItemImpl;
67
use Http\Client\HttpAsyncClient;
78
use Http\Client\HttpClient;
89
use Http\Promise\Promise;
@@ -65,7 +66,7 @@ public function it_throw_exception_if_no_more_enable_client(HttpClient $client,
6566

6667
public function it_reenable_client(HttpClient $client, RequestInterface $request)
6768
{
68-
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
69+
$this->addHttpClient(new HttpClientPoolItemImpl($client->getWrappedObject(), 0));
6970
$client->sendRequest($request)->willThrow(HttpException::class);
7071

7172
$this->shouldThrow(HttpException::class)->duringSendRequest($request);

spec/HttpClientPool/RandomClientPoolSpec.php

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

33
namespace spec\Http\Client\Common\HttpClientPool;
44

5-
use Http\Client\Common\HttpClientPoolItem;
5+
use Http\Client\Common\HttpClientPool\HttpClientPoolItemImpl;
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
88
use Http\Promise\Promise;
@@ -65,7 +65,7 @@ public function it_throw_exception_if_no_more_enable_client(HttpClient $client,
6565

6666
public function it_reenable_client(HttpClient $client, RequestInterface $request)
6767
{
68-
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
68+
$this->addHttpClient(new HttpClientPoolItemImpl($client->getWrappedObject(), 0));
6969
$client->sendRequest($request)->willThrow(HttpException::class);
7070

7171
$this->shouldThrow(HttpException::class)->duringSendRequest($request);

spec/HttpClientPool/RoundRobinClientPoolSpec.php

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

33
namespace spec\Http\Client\Common\HttpClientPool;
44

5-
use Http\Client\Common\HttpClientPoolItem;
5+
use Http\Client\Common\HttpClientPool\HttpClientPoolItemImpl;
66
use Http\Client\HttpAsyncClient;
77
use Http\Client\HttpClient;
88
use Http\Promise\Promise;
@@ -65,7 +65,7 @@ public function it_throw_exception_if_no_more_enable_client(HttpClient $client,
6565

6666
public function it_reenable_client(HttpClient $client, RequestInterface $request)
6767
{
68-
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
68+
$this->addHttpClient(new HttpClientPoolItemImpl($client->getWrappedObject(), 0));
6969
$client->sendRequest($request)->willThrow(HttpException::class);
7070

7171
$this->shouldThrow(HttpException::class)->duringSendRequest($request);

spec/HttpMethodsClientSpec.php

Lines changed: 40 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,88 @@
55
use GuzzleHttp\Psr7\Response;
66
use Http\Client\Common\HttpMethodsClient;
77
use Http\Client\HttpClient;
8-
use Http\Message\MessageFactory;
8+
use Http\Message\RequestFactory;
99
use PhpSpec\ObjectBehavior;
1010
use Psr\Http\Message\RequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
1212

1313
class HttpMethodsClientSpec extends ObjectBehavior
1414
{
15-
public function let(HttpClient $client, MessageFactory $messageFactory)
15+
private static $requestData = [
16+
'uri' => '/uri',
17+
'headers' => [
18+
'Content-Type' => 'text/plain',
19+
],
20+
'body' => 'body',
21+
];
22+
23+
public function let(HttpClient $client, RequestFactory $requestFactory)
1624
{
1725
$this->beAnInstanceOf(
18-
HttpMethodsClientStub::class, [
26+
HttpMethodsClient::class, [
1927
$client,
20-
$messageFactory,
28+
$requestFactory,
2129
]
2230
);
2331
}
2432

25-
public function it_sends_a_get_request()
33+
public function it_sends_a_get_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
2634
{
27-
$data = HttpMethodsClientStub::$requestData;
28-
29-
$this->get($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class);
35+
$this->assert($client, $requestFactory, $request, $response, 'get');
3036
}
3137

32-
public function it_sends_a_head_request()
33-
{
34-
$data = HttpMethodsClientStub::$requestData;
3538

36-
$this->head($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class);
39+
public function it_sends_a_head_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
40+
{
41+
$this->assert($client, $requestFactory, $request, $response, 'head');
3742
}
3843

39-
public function it_sends_a_trace_request()
44+
public function it_sends_a_trace_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
4045
{
41-
$data = HttpMethodsClientStub::$requestData;
42-
43-
$this->trace($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class);
46+
$this->assert($client, $requestFactory, $request, $response, 'trace');
4447
}
4548

46-
public function it_sends_a_post_request()
49+
public function it_sends_a_post_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
4750
{
48-
$data = HttpMethodsClientStub::$requestData;
49-
50-
$this->post($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
51+
$this->assert($client, $requestFactory, $request, $response, 'post', self::$requestData['body']);
5152
}
5253

53-
public function it_sends_a_put_request()
54+
public function it_sends_a_put_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
5455
{
55-
$data = HttpMethodsClientStub::$requestData;
56-
57-
$this->put($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
56+
$this->assert($client, $requestFactory, $request, $response, 'put', self::$requestData['body']);
5857
}
5958

60-
public function it_sends_a_patch_request()
59+
public function it_sends_a_patch_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
6160
{
62-
$data = HttpMethodsClientStub::$requestData;
63-
64-
$this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
61+
$this->assert($client, $requestFactory, $request, $response, 'patch', self::$requestData['body']);
6562
}
6663

67-
public function it_sends_a_delete_request()
64+
public function it_sends_a_delete_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
6865
{
69-
$data = HttpMethodsClientStub::$requestData;
70-
71-
$this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
66+
$this->assert($client, $requestFactory, $request, $response, 'delete', self::$requestData['body']);
7267
}
7368

74-
public function it_sends_a_options_request()
69+
public function it_sends_an_options_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
7570
{
76-
$data = HttpMethodsClientStub::$requestData;
77-
78-
$this->options($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
71+
$this->assert($client, $requestFactory, $request, $response, 'options', self::$requestData['body']);
7972
}
8073

81-
public function it_sends_request_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response)
74+
/**
75+
* Run the actual test.
76+
*
77+
* As there is no data provider in phpspec, we keep separate methods to get new mocks for each test.
78+
*/
79+
private function assert(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response, string $method, string $body = null)
8280
{
8381
$client->sendRequest($request)->shouldBeCalled()->willReturn($response);
82+
$this->mockFactory($requestFactory, $request, strtoupper($method), $body);
8483

85-
$this->beConstructedWith($client, $messageFactory);
86-
$this->sendRequest($request)->shouldReturn($response);
87-
}
88-
}
84+
$this->$method(self::$requestData['uri'], self::$requestData['headers'], self::$requestData['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
8985

90-
class HttpMethodsClientStub extends HttpMethodsClient
91-
{
92-
public static $requestData = [
93-
'uri' => '/uri',
94-
'headers' => [
95-
'Content-Type' => 'text/plain',
96-
],
97-
'body' => 'body',
98-
];
86+
}
9987

100-
/**
101-
* {@inheritdoc}
102-
*/
103-
public function send($method, $uri, array $headers = [], $body = null): ResponseInterface
88+
private function mockFactory(RequestFactory $requestFactory, RequestInterface $request, string $method, string $body = null)
10489
{
105-
if ($uri !== self::$requestData['uri']) {
106-
throw new \InvalidArgumentException('Invalid URI: '.$uri);
107-
}
108-
109-
if ($headers !== self::$requestData['headers']) {
110-
throw new \InvalidArgumentException('Invalid headers: '.print_r($headers, true));
111-
}
112-
113-
switch ($method) {
114-
case 'GET':
115-
case 'HEAD':
116-
case 'TRACE':
117-
if (null !== $body) {
118-
throw new \InvalidArgumentException('Non-empty body');
119-
}
120-
121-
return new Response();
122-
case 'POST':
123-
case 'PUT':
124-
case 'PATCH':
125-
case 'DELETE':
126-
case 'OPTIONS':
127-
if ($body !== self::$requestData['body']) {
128-
throw new \InvalidArgumentException('Invalid body: '.print_r($body, true));
129-
}
130-
131-
return new Response();
132-
default:
133-
throw new \InvalidArgumentException('Invalid method: '.$method);
134-
}
90+
$requestFactory->createRequest($method, self::$requestData['uri'], self::$requestData['headers'], $body)->willReturn($request);
13591
}
13692
}

src/BatchClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @author Joel Wurtz <jwurtz@jolicode.com>
1717
*/
18-
class BatchClient implements HttpClient
18+
final class BatchClient implements HttpClient
1919
{
2020
/**
2121
* @var HttpClient

src/Deferred.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* A deferred allow to return a promise which has not been resolved yet.
1111
*/
12-
class Deferred implements Promise
12+
final class Deferred implements Promise
1313
{
1414
private $value;
1515

src/HttpClientPool.php

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

55
use Http\Client\Common\Exception\HttpClientNotFoundException;
6+
use Http\Client\Common\HttpClientPool\HttpClientPoolItem;
7+
use Http\Client\Common\HttpClientPool\HttpClientPoolItemImpl;
68
use Http\Client\HttpAsyncClient;
79
use Http\Client\HttpClient;
810
use Psr\Http\Message\RequestInterface;
@@ -27,7 +29,7 @@ abstract class HttpClientPool implements HttpAsyncClient, HttpClient
2729
public function addHttpClient($client)
2830
{
2931
if (!$client instanceof HttpClientPoolItem) {
30-
$client = new HttpClientPoolItem($client);
32+
$client = new HttpClientPoolItemImpl($client);
3133
}
3234

3335
$this->clientPool[] = $client;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Http\Client\Common\HttpClientPool;
4+
5+
use Http\Client\HttpAsyncClient;
6+
use Http\Client\HttpClient;
7+
8+
/**
9+
* A HttpClientPoolItem represent a HttpClient inside a Pool.
10+
*
11+
* It is disabled when a request failed and can be reenabled after a certain number of seconds.
12+
* It also keep tracks of the current number of open requests the client is currently being sending
13+
* (only usable for async method).
14+
*
15+
* @author Joel Wurtz <joel.wurtz@gmail.com>
16+
*/
17+
interface HttpClientPoolItem extends HttpClient, HttpAsyncClient
18+
{
19+
/**
20+
* Whether this client is disabled or not.
21+
*
22+
* Will also reactivate this client if possible
23+
*
24+
* @internal
25+
*
26+
* @return bool
27+
*/
28+
public function isDisabled();
29+
30+
/**
31+
* Get current number of request that are currently being sent by the underlying HTTP client.
32+
*
33+
* @internal
34+
*
35+
* @return int
36+
*/
37+
public function getSendingRequestCount();
38+
}

0 commit comments

Comments
 (0)