Completed
Pull Request — master (#86)
by Márk
06:48
created

CurlFactory::createClient()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 9.9614

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 5
cts 12
cp 0.4167
rs 8.7624
cc 5
eloc 10
nc 5
nop 1
crap 9.9614
1
<?php
2
3
namespace Http\HttplugBundle\ClientFactory;
4
5
use Http\Client\Curl\Client;
6
use Http\Message\MessageFactory;
7
use Http\Message\StreamFactory;
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class CurlFactory implements ClientFactory
13
{
14
    /**
15
     * @var MessageFactory
16
     */
17
    private $messageFactory;
18
19
    /**
20
     * @var StreamFactory
21
     */
22
    private $streamFactory;
23
24
    /**
25
     * @param MessageFactory $messageFactory
26
     * @param StreamFactory  $streamFactory
27
     */
28 1
    public function __construct(MessageFactory $messageFactory, StreamFactory $streamFactory)
29
    {
30 1
        $this->messageFactory = $messageFactory;
31 1
        $this->streamFactory = $streamFactory;
32 1
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function createClient(array $config = [])
38
    {
39 1
        if (!class_exists('Http\Client\Curl\Client')) {
40
            throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
41
        }
42
43
        // Try to resolve curl constant names
44 1
        foreach ($config as $key => $value) {
45
            // If the $key is a string we assume it is a constant
46
            if (is_string($key)) {
47
                if (null === ($constantValue = constant($key))) {
48
                    throw new \LogicException(sprintf('Key %s is not an int nor a CURL constant', $key));
49
                }
50
51
                unset($config[$key]);
52
                $config[$constantValue] = $value;
53
            }
54 1
        }
55
56 1
        return new Client($this->messageFactory, $this->streamFactory, $config);
57
    }
58
}
59