Skip to content

Add a Symfony client factory #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ matrix:
- php: 7.3
env: SYMFONY_REQUIRE=4.3.*
- php: 7.3
env: SYMFONY_REQUIRE=4.4.*
env: SYMFONY_REQUIRE=4.4.* DEPENDENCIES="symfony/http-client:^4.4"
- php: 7.3
env: SYMFONY_REQUIRE=5.0.*
env: SYMFONY_REQUIRE=5.0.* DEPENDENCIES="symfony/http-client:^5.0"

# Test with httplug 1.x clients
- php: 7.2
Expand Down
46 changes: 46 additions & 0 deletions src/ClientFactory/SymfonyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Http\HttplugBundle\ClientFactory;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\HttplugClient;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class SymfonyFactory implements ClientFactory
{
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;

/**
* @var StreamFactoryInterface
*/
private $streamFactory;

/**
* @param ResponseFactoryInterface $responseFactory
* @param StreamFactoryInterface $streamFactory
*/
public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
{
$this->responseFactory = $responseFactory;
$this->streamFactory = $streamFactory;
}

/**
* {@inheritdoc}
*/
public function createClient(array $config = [])
{
if (!class_exists(HttplugClient::class)) {
throw new \LogicException('To use the Symfony client you need to install the "symfony/http-client" package.');
}

return new HttplugClient(HttpClient::create($config), $this->responseFactory, $this->streamFactory);
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,9 @@
<service id="httplug.factory.socket" class="Http\HttplugBundle\ClientFactory\SocketFactory" public="false">
<argument type="service" id="httplug.message_factory"/>
</service>
<service id="httplug.factory.symfony" class="Http\HttplugBundle\ClientFactory\SymfonyFactory" public="false">
<argument type="service" id="httplug.psr17_response_factory"/>
<argument type="service" id="httplug.psr17_stream_factory"/>
</service>
</services>
</container>
30 changes: 30 additions & 0 deletions tests/Unit/ClientFactory/SymfonyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Http\HttplugBundle\Tests\Unit\ClientFactory;

use Http\HttplugBundle\ClientFactory\SymfonyFactory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Symfony\Component\HttpClient\HttplugClient;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class SymfonyFactoryTest extends TestCase
{
public function testCreateClient(): void
{
if (!class_exists(HttplugClient::class)) {
$this->markTestSkipped('Symfony Http client is not installed');
}

$factory = new SymfonyFactory(
$this->getMockBuilder(ResponseFactoryInterface::class)->getMock(),
$this->getMockBuilder(StreamFactoryInterface::class)->getMock()
);
$client = $factory->createClient();

$this->assertInstanceOf(HttplugClient::class, $client);
}
}