Skip to content

Commit ce5e183

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

16 files changed

+111
-141
lines changed

Diff for: CHANGELOG.md

+1
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`

Diff for: spec/HttpClientPoolItemSpec.php renamed to spec/HttpClientPool/HttpClientPoolItemImplSpec.php

+2-2
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
{

Diff for: spec/HttpClientPool/LeastUsedClientPoolSpec.php

+3-2
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);

Diff for: spec/HttpClientPool/RandomClientPoolSpec.php

+2-2
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);

Diff for: spec/HttpClientPool/RoundRobinClientPoolSpec.php

+2-2
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);

Diff for: spec/HttpMethodsClientSpec.php

+38-85
Original file line numberDiff line numberDiff line change
@@ -2,135 +2,88 @@
22

33
namespace spec\Http\Client\Common;
44

5-
use GuzzleHttp\Psr7\Response;
65
use Http\Client\Common\HttpMethodsClient;
76
use Http\Client\HttpClient;
8-
use Http\Message\MessageFactory;
7+
use Http\Message\RequestFactory;
98
use PhpSpec\ObjectBehavior;
109
use Psr\Http\Message\RequestInterface;
1110
use Psr\Http\Message\ResponseInterface;
1211

1312
class HttpMethodsClientSpec extends ObjectBehavior
1413
{
15-
public function let(HttpClient $client, MessageFactory $messageFactory)
14+
private static $requestData = [
15+
'uri' => '/uri',
16+
'headers' => [
17+
'Content-Type' => 'text/plain',
18+
],
19+
'body' => 'body',
20+
];
21+
22+
public function let(HttpClient $client, RequestFactory $requestFactory)
1623
{
1724
$this->beAnInstanceOf(
18-
HttpMethodsClientStub::class, [
25+
HttpMethodsClient::class, [
1926
$client,
20-
$messageFactory,
27+
$requestFactory,
2128
]
2229
);
2330
}
2431

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

32-
public function it_sends_a_head_request()
37+
public function it_sends_a_head_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response)
3338
{
34-
$data = HttpMethodsClientStub::$requestData;
35-
36-
$this->head($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class);
39+
$this->assert($client, $requestFactory, $request, $response, 'head');
3740
}
3841

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

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

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

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

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

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

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

85-
$this->beConstructedWith($client, $messageFactory);
86-
$this->sendRequest($request)->shouldReturn($response);
82+
$this->$method(self::$requestData['uri'], self::$requestData['headers'], self::$requestData['body'])->shouldReturnAnInstanceOf(ResponseInterface::class);
8783
}
88-
}
89-
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-
];
9984

100-
/**
101-
* {@inheritdoc}
102-
*/
103-
public function send($method, $uri, array $headers = [], $body = null): ResponseInterface
85+
private function mockFactory(RequestFactory $requestFactory, RequestInterface $request, string $method, string $body = null)
10486
{
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-
}
87+
$requestFactory->createRequest($method, self::$requestData['uri'], self::$requestData['headers'], $body)->willReturn($request);
13588
}
13689
}

Diff for: src/BatchClient.php

+1-1
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

Diff for: src/Deferred.php

+1-1
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

Diff for: src/HttpClientPool.php

+3-1
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;

Diff for: src/HttpClientPool/HttpClientPoolItem.php

+38
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)