Skip to content

Commit 596ef53

Browse files
committed
Buzz: migrate client factory to use PSR-17
1 parent 143dba2 commit 596ef53

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"conflict": {
4545
"php-http/guzzle6-adapter": "<1.1",
4646
"php-http/curl-client": "<2.0",
47-
"php-http/socket-client": "<2.0"
47+
"php-http/socket-client": "<2.0",
48+
"kriswallsmith/buzz": "<0.17"
4849
},
4950
"require-dev": {
5051
"guzzlehttp/psr7": "^1.7 || ^2.0",

src/ClientFactory/BuzzFactory.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Buzz\Client\FileGetContents;
8-
use Http\Adapter\Buzz\Client as Adapter;
9-
use Http\Message\MessageFactory;
8+
use Psr\Http\Message\ResponseFactoryInterface;
109
use Symfony\Component\OptionsResolver\OptionsResolver;
1110

1211
/**
@@ -15,33 +14,25 @@
1514
class BuzzFactory implements ClientFactory
1615
{
1716
/**
18-
* @var MessageFactory
17+
* @var ResponseFactoryInterface
1918
*/
20-
private $messageFactory;
19+
private $responseFactory;
2120

22-
public function __construct(MessageFactory $messageFactory)
21+
public function __construct(ResponseFactoryInterface $responseFactory)
2322
{
24-
$this->messageFactory = $messageFactory;
23+
$this->responseFactory = $responseFactory;
2524
}
2625

2726
/**
2827
* {@inheritdoc}
2928
*/
3029
public function createClient(array $config = [])
3130
{
32-
if (!class_exists('Http\Adapter\Buzz\Client')) {
33-
throw new \LogicException('To use the Buzz adapter you need to install the "php-http/buzz-adapter" package.');
31+
if (!class_exists('Buzz\Client\FileGetContents')) {
32+
throw new \LogicException('To use the Buzz you need to install the "kriswallsmith/buzz" package.');
3433
}
3534

36-
$client = new FileGetContents();
37-
$options = $this->getOptions($config);
38-
39-
$client->setTimeout($options['timeout']);
40-
$client->setVerifyPeer($options['verify_peer']);
41-
$client->setVerifyHost($options['verify_host']);
42-
$client->setProxy($options['proxy']);
43-
44-
return new Adapter($client, $this->messageFactory);
35+
return new FileGetContents($this->responseFactory, $this->getOptions($config));
4536
}
4637

4738
/**
@@ -53,14 +44,12 @@ private function getOptions(array $config = [])
5344

5445
$resolver->setDefaults([
5546
'timeout' => 5,
56-
'verify_peer' => true,
57-
'verify_host' => 2,
47+
'verify' => true,
5848
'proxy' => null,
5949
]);
6050

6151
$resolver->setAllowedTypes('timeout', 'int');
62-
$resolver->setAllowedTypes('verify_peer', 'bool');
63-
$resolver->setAllowedTypes('verify_host', 'int');
52+
$resolver->setAllowedTypes('verify', 'bool');
6453
$resolver->setAllowedTypes('proxy', ['string', 'null']);
6554

6655
return $resolver->resolve($config);

src/Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<!-- ClientFactories -->
8181
<service id="httplug.factory.auto" class="Http\HttplugBundle\ClientFactory\AutoDiscoveryFactory" public="false" />
8282
<service id="httplug.factory.buzz" class="Http\HttplugBundle\ClientFactory\BuzzFactory" public="false">
83-
<argument type="service" id="httplug.message_factory"/>
83+
<argument type="service" id="httplug.psr17_response_factory"/>
8484
</service>
8585
<service id="httplug.factory.curl" class="Http\HttplugBundle\ClientFactory\CurlFactory" public="false">
8686
<argument type="service" id="httplug.psr17_response_factory"/>

tests/Unit/ClientFactory/BuzzFactoryTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Http\HttplugBundle\Tests\Unit\ClientFactory;
66

7-
use Http\Adapter\Buzz\Client;
7+
use Buzz\Client\FileGetContents;
88
use Http\HttplugBundle\ClientFactory\BuzzFactory;
9-
use Http\Message\MessageFactory;
109
use PHPUnit\Framework\TestCase;
10+
use Psr\Http\Message\ResponseFactoryInterface;
1111

1212
/**
1313
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -16,13 +16,13 @@ class BuzzFactoryTest extends TestCase
1616
{
1717
public function testCreateClient(): void
1818
{
19-
if (!class_exists(Client::class)) {
20-
$this->markTestSkipped('Buzz adapter is not installed');
19+
if (!class_exists(FileGetContents::class)) {
20+
$this->markTestSkipped('Buzz client is not installed');
2121
}
2222

23-
$factory = new BuzzFactory($this->getMockBuilder(MessageFactory::class)->getMock());
23+
$factory = new BuzzFactory($this->getMockBuilder(ResponseFactoryInterface::class)->getMock());
2424
$client = $factory->createClient();
2525

26-
$this->assertInstanceOf(Client::class, $client);
26+
$this->assertInstanceOf(FileGetContents::class, $client);
2727
}
2828
}

0 commit comments

Comments
 (0)