Skip to content

Commit c796537

Browse files
committed
Merge pull request #69 from php-http/buzz
Added Buzz client factory
2 parents 45dc1ec + edfca70 commit c796537

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## UNRELEASED
4+
5+
### Added
6+
7+
- Client factories for Buzz.
8+
9+
310
## 1.0.0 - 2016-03-04
411

512
### Added

ClientFactory/BuzzFactory.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Buzz\Client\FileGetContents;
6+
use Http\Adapter\Buzz\Client as Adapter;
7+
use Http\Message\MessageFactory;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
/**
11+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
12+
*/
13+
class BuzzFactory implements ClientFactory
14+
{
15+
/**
16+
* @var MessageFactory
17+
*/
18+
private $messageFactory;
19+
20+
/**
21+
* @param MessageFactory $messageFactory
22+
*/
23+
public function __construct(MessageFactory $messageFactory)
24+
{
25+
$this->messageFactory = $messageFactory;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function createClient(array $config = [])
32+
{
33+
if (!class_exists('Http\Adapter\Buzz\Client')) {
34+
throw new \LogicException('To use the Buzz adapter you need to install the "php-http/buzz-adapter" package.');
35+
}
36+
37+
$client = new FileGetContents();
38+
$options = $this->getOptions($config);
39+
40+
$client->setTimeout($options['timeout']);
41+
$client->setVerifyPeer($options['verify_peer']);
42+
$client->setVerifyHost($options['verify_host']);
43+
$client->setProxy($options['proxy']);
44+
45+
return new Adapter($client, $this->messageFactory);
46+
}
47+
48+
/**
49+
* Get options to configure the Buzz client.
50+
*
51+
* @param array $config
52+
*/
53+
private function getOptions(array $config = [])
54+
{
55+
$resolver = new OptionsResolver();
56+
57+
$resolver->setDefaults([
58+
'timeout' => 5,
59+
'verify_peer' => true,
60+
'verify_host' => 2,
61+
'proxy' => null,
62+
]);
63+
64+
$resolver->setAllowedTypes('timeout', 'int');
65+
$resolver->setAllowedTypes('verify_peer', 'bool');
66+
$resolver->setAllowedTypes('verify_host', 'int');
67+
$resolver->setAllowedTypes('proxy', ['string', 'null']);
68+
69+
return $resolver->resolve($config);
70+
}
71+
}

Resources/config/services.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<services>
77

88
<!-- ClientFactories -->
9+
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">
10+
<argument type="service" id="httplug.message_factory"/>
11+
</service>
912
<service id="httplug.factory.curl" class="Http\HttplugBundle\ClientFactory\CurlFactory" public="false">
1013
<argument type="service" id="httplug.message_factory"/>
1114
<argument type="service" id="httplug.stream_factory"/>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Unit\ClientFactory;
4+
5+
use Http\Adapter\Buzz\Client;
6+
use Http\HttplugBundle\ClientFactory\BuzzFactory;
7+
use Http\Message\MessageFactory;
8+
9+
/**
10+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
11+
*/
12+
class BuzzFactoryTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testCreateClient()
15+
{
16+
$factory = new BuzzFactory($this->getMock(MessageFactory::class));
17+
$client = $factory->createClient();
18+
19+
$this->assertInstanceOf(Client::class, $client);
20+
}
21+
}

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"php-http/client-implementation": "^1.0",
2121
"php-http/message-factory": "^1.0.2",
2222
"php-http/plugins": "^1.0",
23+
"symfony/options-resolver": "^2.7|^3.0",
2324
"symfony/framework-bundle": "^2.7|^3.0"
2425
},
2526
"require-dev": {
@@ -28,6 +29,7 @@
2829
"php-http/socket-client": "^1.0",
2930
"php-http/guzzle6-adapter": "^1.0",
3031
"php-http/react-adapter": "^0.1",
32+
"php-http/buzz-adapter": "^0.1",
3133
"php-http/message": "^1.0",
3234
"symfony/symfony": "^2.7|^3.0",
3335
"polishsymfonycommunity/symfony-mocker-container": "^1.0",

0 commit comments

Comments
 (0)