Skip to content

Commit c245496

Browse files
committed
Add flexible http client
1 parent b61b641 commit c245496

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

spec/HttpClientFlexibleSpec.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common;
4+
5+
use Http\Client\Common\HttpAsyncClientDecorator;
6+
use Http\Client\Common\HttpClientDecorator;
7+
use Http\Client\Common\HttpClientFlexible;
8+
use Http\Client\HttpAsyncClient;
9+
use Http\Client\HttpClient;
10+
use PhpSpec\ObjectBehavior;
11+
12+
class HttpClientFlexibleSpec extends ObjectBehavior
13+
{
14+
function let(HttpClient $httpClient)
15+
{
16+
$this->beAnInstanceOf(
17+
'spec\Http\Client\Common\HttpClientFlexibleStub', [
18+
$httpClient
19+
]
20+
);
21+
}
22+
23+
function it_is_initializable()
24+
{
25+
$this->shouldHaveType('Http\Client\Common\HttpClientFlexible');
26+
}
27+
28+
function it_is_an_http_client()
29+
{
30+
$this->shouldImplement('Http\Client\HttpClient');
31+
}
32+
33+
function it_is_an_async_http_client()
34+
{
35+
$this->shouldImplement('Http\Client\HttpAsyncClient');
36+
}
37+
38+
function it_throw_exception_if_invalid_client()
39+
{
40+
$httpClient = null;
41+
42+
$this->shouldThrow('\LogicException')->during('__construct', [$httpClient]);
43+
}
44+
45+
function it_emulates_an_async_client(HttpClient $httpClient)
46+
{
47+
$this->beConstructedWith($httpClient);
48+
49+
$this->getClient()->shouldImplement('Http\Client\HttpClient');
50+
$this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient');
51+
$this->getAsyncClient()->shouldImplement('Http\Client\Common\EmulatedHttpAsyncClient');
52+
}
53+
54+
function it_emulates_a_client(HttpAsyncClient $httpAsyncClient)
55+
{
56+
$this->beConstructedWith($httpAsyncClient);
57+
58+
$this->getClient()->shouldImplement('Http\Client\HttpClient');
59+
$this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient');
60+
$this->getClient()->shouldImplement('Http\Client\Common\EmulatedHttpClient');
61+
}
62+
63+
function it_does_not_emulates_a_client(HttpClientFull $httpClient)
64+
{
65+
$this->beConstructedWith($httpClient);
66+
67+
$this->getClient()->shouldImplement('Http\Client\HttpClient');
68+
$this->getAsyncClient()->shouldImplement('Http\Client\HttpAsyncClient');
69+
$this->getClient()->shouldImplement('spec\Http\Client\Common\HttpClientFull');
70+
$this->getAsyncClient()->shouldImplement('spec\Http\Client\Common\HttpClientFull');
71+
$this->getClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpClient');
72+
$this->getAsyncClient()->shouldNotImplement('Http\Client\Common\EmulatedHttpAsyncClient');
73+
}
74+
}
75+
76+
class HttpClientFull implements HttpClient, HttpAsyncClient
77+
{
78+
use HttpClientDecorator;
79+
use HttpAsyncClientDecorator;
80+
}
81+
82+
class HttpClientFlexibleStub extends HttpClientFlexible
83+
{
84+
public function getClient()
85+
{
86+
return $this->httpClient;
87+
}
88+
89+
public function getAsyncClient()
90+
{
91+
return $this->httpAsyncClient;
92+
}
93+
}

src/HttpClientFlexible.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Http\Client\Common;
4+
5+
use Http\Client\HttpAsyncClient;
6+
use Http\Client\HttpClient;
7+
8+
/**
9+
* A flexible http client, which implements both interface and will emulate
10+
* one contract, the other, or none at all depending on the injected client contract.
11+
*
12+
* @author Joel Wurtz <[email protected]>
13+
*/
14+
class HttpClientFlexible implements HttpClient, HttpAsyncClient
15+
{
16+
use HttpClientDecorator;
17+
use HttpAsyncClientDecorator;
18+
19+
/**
20+
* @param HttpClient|HttpAsyncClient $client
21+
*/
22+
public function __construct($client)
23+
{
24+
if (!($client instanceof HttpClient) && !($client instanceof HttpAsyncClient)) {
25+
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient');
26+
}
27+
28+
$this->httpAsyncClient = $client;
29+
$this->httpClient = $client;
30+
31+
if (!($this->httpAsyncClient instanceof HttpAsyncClient)) {
32+
$this->httpAsyncClient = new EmulatedHttpAsyncClient($this->httpAsyncClient);
33+
}
34+
35+
if (!($this->httpClient instanceof HttpClient)) {
36+
$this->httpClient = new EmulatedHttpClient($this->httpClient);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)