diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c10c4c..5791a289 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## UNRELEASED + +### Added + +- Client factories for Buzz. + + ## 1.0.0 - 2016-03-04 ### Added diff --git a/ClientFactory/BuzzFactory.php b/ClientFactory/BuzzFactory.php new file mode 100644 index 00000000..84f1e9d7 --- /dev/null +++ b/ClientFactory/BuzzFactory.php @@ -0,0 +1,71 @@ + + */ +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); + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 450ccac3..77e78b6e 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -6,6 +6,9 @@ + + + diff --git a/Tests/Unit/ClientFactory/BuzzFactoryTest.php b/Tests/Unit/ClientFactory/BuzzFactoryTest.php new file mode 100644 index 00000000..d312d6dc --- /dev/null +++ b/Tests/Unit/ClientFactory/BuzzFactoryTest.php @@ -0,0 +1,21 @@ + + */ +class BuzzFactoryTest extends \PHPUnit_Framework_TestCase +{ + public function testCreateClient() + { + $factory = new BuzzFactory($this->getMock(MessageFactory::class)); + $client = $factory->createClient(); + + $this->assertInstanceOf(Client::class, $client); + } +} diff --git a/composer.json b/composer.json index 56b98b7b..b92ceda2 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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",