Skip to content

Added Buzz client factory #69

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
Mar 6, 2016
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## UNRELEASED

### Added

- Client factories for Buzz.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two lines between releases


## 1.0.0 - 2016-03-04

### Added
Expand Down
71 changes: 71 additions & 0 deletions ClientFactory/BuzzFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Http\HttplugBundle\ClientFactory;

use Buzz\Client\FileGetContents;
use Http\Adapter\Buzz\Client as Adapter;
use Http\Message\MessageFactory;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class BuzzFactory implements ClientFactory
{
/**
* @var MessageFactory
*/
private $messageFactory;

/**
* @param MessageFactory $messageFactory
*/
public function __construct(MessageFactory $messageFactory)
{
$this->messageFactory = $messageFactory;
}

/**
* {@inheritdoc}
*/
public function createClient(array $config = [])
{
if (!class_exists('Http\Adapter\Buzz\Client')) {
throw new \LogicException('To use the Buzz adapter you need to install the "php-http/buzz-adapter" package.');
}

$client = new FileGetContents();
$options = $this->getOptions($config);

$client->setTimeout($options['timeout']);
$client->setVerifyPeer($options['verify_peer']);
$client->setVerifyHost($options['verify_host']);
$client->setProxy($options['proxy']);

return new Adapter($client, $this->messageFactory);
}

/**
* Get options to configure the Buzz client.
*
* @param array $config
*/
private function getOptions(array $config = [])
{
$resolver = new OptionsResolver();

$resolver->setDefaults([
'timeout' => 5,
'verify_peer' => true,
'verify_host' => 2,
'proxy' => null,
]);

$resolver->setAllowedTypes('timeout', 'int');
$resolver->setAllowedTypes('verify_peer', 'bool');
$resolver->setAllowedTypes('verify_host', 'int');
$resolver->setAllowedTypes('proxy', ['string', 'null']);

return $resolver->resolve($config);
}
}
3 changes: 3 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<services>

<!-- ClientFactories -->
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">
<argument type="service" id="httplug.message_factory"/>
</service>
<service id="httplug.factory.curl" class="Http\HttplugBundle\ClientFactory\CurlFactory" public="false">
<argument type="service" id="httplug.message_factory"/>
<argument type="service" id="httplug.stream_factory"/>
Expand Down
21 changes: 21 additions & 0 deletions Tests/Unit/ClientFactory/BuzzFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Http\HttplugBundle\Tests\Unit\ClientFactory;

use Http\Adapter\Buzz\Client;
use Http\HttplugBundle\ClientFactory\BuzzFactory;
use Http\Message\MessageFactory;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class BuzzFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateClient()
{
$factory = new BuzzFactory($this->getMock(MessageFactory::class));
$client = $factory->createClient();

$this->assertInstanceOf(Client::class, $client);
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"php-http/client-implementation": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/plugins": "^1.0",
"symfony/options-resolver": "^2.7|^3.0",
"symfony/framework-bundle": "^2.7|^3.0"
},
"require-dev": {
Expand All @@ -28,6 +29,7 @@
"php-http/socket-client": "^1.0",
"php-http/guzzle6-adapter": "^1.0",
"php-http/react-adapter": "^0.1",
"php-http/buzz-adapter": "^0.1",
"php-http/message": "^1.0",
"symfony/symfony": "^2.7|^3.0",
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
Expand Down