-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathBuzzFactory.php
70 lines (57 loc) · 1.79 KB
/
BuzzFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
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 <[email protected]>
*
* @final
*/
class BuzzFactory implements ClientFactory
{
/**
* @var MessageFactory
*/
private $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.
*/
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);
}
}