Completed
Push — master ( baadcd...d3edb4 )
by Tobias
09:03
created

BuzzFactory::createClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2.004
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 <[email protected]>
12
 */
13
class BuzzFactory implements ClientFactory
14
{
15
    /**
16
     * @var MessageFactory
17
     */
18
    private $messageFactory;
19
20
    /**
21
     * @param MessageFactory $messageFactory
22
     */
23 1
    public function __construct(MessageFactory $messageFactory)
24
    {
25 1
        $this->messageFactory = $messageFactory;
26 1
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function createClient(array $config = [])
32
    {
33 1
        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 1
        $client = new FileGetContents();
38 1
        $options = $this->getOptions($config);
39
40 1
        $client->setTimeout($options['timeout']);
41 1
        $client->setVerifyPeer($options['verify_peer']);
42 1
        $client->setVerifyHost($options['verify_host']);
43 1
        $client->setProxy($options['proxy']);
44
45 1
        return new Adapter($client, $this->messageFactory);
46
    }
47
48
    /**
49
     * Get options to configure the Buzz client.
50
     *
51
     * @param array $config
52
     */
53 1
    private function getOptions(array $config = [])
54
    {
55 1
        $resolver = new OptionsResolver();
56
57 1
        $resolver->setDefaults([
58 1
          'timeout' => 5,
59
          'verify_peer' => true,
60
          'verify_host' => 2,
61
          'proxy' => null,
62
        ]);
63
64 1
        $resolver->setAllowedTypes('timeout', 'int');
65 1
        $resolver->setAllowedTypes('verify_peer', 'bool');
66 1
        $resolver->setAllowedTypes('verify_host', 'int');
67 1
        $resolver->setAllowedTypes('proxy', ['string', 'null']);
68
69 1
        return $resolver->resolve($config);
70
    }
71
}
72