Completed
Pull Request — master (#546)
by thomas
76:04 queued 06:03
created

CurlDriver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 7 2
B execute() 0 40 4
A addCurlOption() 0 6 1
A getDefaultCurlOptions() 0 9 1
1
<?php
2
/**
3
 * @author Joshua Estes
4
 * @copyright 2012-2015 Joshua Estes
5
 * @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
6
 */
7
8
namespace BitWasp\Bitcoin\RpcTest;
9
10
use Nbobtc\Http\Driver\DriverInterface;
11
use Psr\Http\Message\RequestInterface;
12
use Nbobtc\Http\Message\Response;
13
14
/**
15
 * Uses cURL to send Requests
16
 *
17
 * @since 2.0.0
18
 */
19
class CurlDriver implements DriverInterface
20
{
21
    /**
22
     * @var resource
23
     */
24
    protected $ch;
25
26
    /**
27
     * @var array
28
     */
29
    protected $curlOptions = array();
30
31
    /**
32
     * @since 2.0.0
33
     */
34
    public function __destruct()
35
    {
36
        if (null !== $this->ch) {
37
            curl_close($this->ch);
38
            $this->ch=null;
39
        }
40
    }
41
42
    /**
43
     * @since 2.0.0
44
     * {@inheritDoc}
45
     */
46
    public function execute(RequestInterface $request)
47
    {
48
        $uri = $request->getUri();
49
50
        if (null === $this->ch) {
51
            $this->ch = curl_init();
52
        }
53
54
        curl_setopt_array($this->ch, $this->getDefaultCurlOptions());
55
56
        curl_setopt($this->ch, CURLOPT_URL, sprintf('%s://%s@%s', $uri->getScheme(), $uri->getUserInfo(), $uri->getHost()));
57
        curl_setopt($this->ch, CURLOPT_PORT, $uri->getPort());
58
59
        $headers = array();
60
        foreach ($request->getHeaders() as $header => $values) {
61
            $headers[] = $header.': '.implode(', ', $values);
62
        }
63
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
64
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request->getBody()->getContents());
65
66
        // Allows user to override any option, may cause errors
67
        curl_setopt_array($this->ch, $this->curlOptions);
68
69
        /** @var string|false */
70
        $result = curl_exec($this->ch);
71
        /** @var array|false */
72
        $info = curl_getinfo($this->ch);
73
        /** @var string */
74
        $error = curl_error($this->ch);
75
76
        if (!empty($error)) {
77
            throw new \Exception($error);
78
        }
79
80
        $response = new Response();
81
        $response->withStatus($info['http_code']);
82
        $response->getBody()->write($result);
83
84
        return $response;
85
    }
86
87
    /**
88
     * Add options to use for cURL requests
89
     *
90
     * @since 2.0.0
91
     * @param integer $option
92
     * @param mixed   $value
93
     */
94
    public function addCurlOption($option, $value)
95
    {
96
        $this->curlOptions[$option] = $value;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Returns an array of cURL options
103
     *
104
     * @since 2.0.0
105
     * @return array
106
     */
107
    protected function getDefaultCurlOptions()
108
    {
109
        return array(
110
            CURLOPT_POST           => true,
111
            CURLOPT_RETURNTRANSFER => true,
112
            CURLOPT_CONNECTTIMEOUT => 5,
113
            CURLOPT_TIMEOUT        => 10,
114
        );
115
    }
116
}
117