Skip to content

Commit 157f595

Browse files
committed
Add a symfony factory
1 parent 060b9b1 commit 157f595

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ matrix:
3939
- php: 7.3
4040
env: SYMFONY_REQUIRE=4.3.*
4141
- php: 7.3
42-
env: SYMFONY_REQUIRE=4.4.*
42+
env: SYMFONY_REQUIRE=4.4.* DEPENDENCIES="symfony/http-client:^4.4"
4343
- php: 7.3
44-
env: SYMFONY_REQUIRE=5.0.*
44+
env: SYMFONY_REQUIRE=5.0.* DEPENDENCIES="symfony/http-client:^5.0"
4545

4646
# Test with httplug 1.x clients
4747
- php: 7.2

src/ClientFactory/SymfonyFactory.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Psr\Http\Message\ResponseFactoryInterface;
6+
use Psr\Http\Message\StreamFactoryInterface;
7+
use Symfony\Component\HttpClient\HttpClient;
8+
use Symfony\Component\HttpClient\HttplugClient;
9+
10+
/**
11+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
12+
*/
13+
class SymfonyFactory implements ClientFactory
14+
{
15+
/**
16+
* @var ResponseFactoryInterface
17+
*/
18+
private $responseFactory;
19+
20+
/**
21+
* @var StreamFactoryInterface
22+
*/
23+
private $streamFactory;
24+
25+
/**
26+
* @param ResponseFactoryInterface $responseFactory
27+
* @param StreamFactoryInterface $streamFactory
28+
*/
29+
public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
30+
{
31+
$this->responseFactory = $responseFactory;
32+
$this->streamFactory = $streamFactory;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function createClient(array $config = [])
39+
{
40+
if (!class_exists(HttplugClient::class)) {
41+
throw new \LogicException('To use the Symfony client you need to install the "symfony/http-client" package.');
42+
}
43+
44+
return new HttplugClient(HttpClient::create($config), $this->responseFactory, $this->streamFactory);
45+
}
46+
}

src/Resources/config/services.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,9 @@
9393
<service id="httplug.factory.socket" class="Http\HttplugBundle\ClientFactory\SocketFactory" public="false">
9494
<argument type="service" id="httplug.message_factory"/>
9595
</service>
96+
<service id="httplug.factory.symfony" class="Http\HttplugBundle\ClientFactory\SymfonyFactory" public="false">
97+
<argument type="service" id="httplug.psr17_response_factory"/>
98+
<argument type="service" id="httplug.psr17_stream_factory"/>
99+
</service>
96100
</services>
97101
</container>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Unit\ClientFactory;
4+
5+
use Http\HttplugBundle\ClientFactory\SymfonyFactory;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Http\Message\ResponseFactoryInterface;
8+
use Psr\Http\Message\StreamFactoryInterface;
9+
use Symfony\Component\HttpClient\HttplugClient;
10+
11+
/**
12+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
13+
*/
14+
class SymfonyFactoryTest extends TestCase
15+
{
16+
public function testCreateClient(): void
17+
{
18+
if (!class_exists(HttplugClient::class)) {
19+
$this->markTestSkipped('Symfony Http client is not installed');
20+
}
21+
22+
$factory = new SymfonyFactory(
23+
$this->getMockBuilder(ResponseFactoryInterface::class)->getMock(),
24+
$this->getMockBuilder(StreamFactoryInterface::class)->getMock()
25+
);
26+
$client = $factory->createClient();
27+
28+
$this->assertInstanceOf(HttplugClient::class, $client);
29+
}
30+
}

0 commit comments

Comments
 (0)