Completed
Pull Request — master (#69)
by Tobias
08:18
created

BuzzFactory::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 1
eloc 12
nc 1
nop 1
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
    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
}
72