Completed
Pull Request — develop (#20)
by Johan
03:38 queued 01:50
created

HttpClient::send()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 8.9713
cc 3
eloc 15
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Artstorm\MonkeyLearn\HttpClient;
4
5
class HttpClient implements HttpClientInterface
6
{
7
    /**
8
     * Client config.
9
     *
10
     * @var array
11
     */
12
    protected $config;
13
14
    /**
15
     * Headers returned in the response.
16
     *
17
     * @var array
18
     */
19
    protected $headers = [];
20
21
    /**
22
     * Assign dependencies.
23
     *
24
     * @param array $config
25
     */
26 16
    public function __construct(array $config = [])
27
    {
28 16
        $this->config = $config;
29 16
    }
30
31
    /**
32
     * Make a POST request.
33
     *
34
     * @param  string $path
35
     * @param  mixed  $body
36
     * @param  array  $headers
37
     *
38
     * @return Request
39
     */
40 8
    public function post($path, $body = null, array $headers = [])
41
    {
42 8
        return $this->request($path, $body, 'POST', $headers);
43
    }
44
45
    /**
46
     * Send request with HTTP client.
47
     *
48
     * @param  string $path
49
     * @param  mixed  $body
50
     * @param  string $method
51
     * @param  array  $headers
52
     *
53
     * @return Response
54
     */
55 8
    protected function request($path, $body = null, $method = 'GET', array $headers = [])
56
    {
57 8
        $request = $this->createRequest($method, $path, $body, $headers);
58 8
        $response = $this->send($request);
59
60 8
        return $response;
61
    }
62
63
    /**
64
     * Create request with HTTP client.
65
     *
66
     * @param  string $method
67
     * @param  string $path
68
     * @param  mixed  $body
69
     * @param  array  $headers
70
     *
71
     * @return Request
72
     */
73 8
    protected function createRequest($method, $path, $body = null, array $headers = [])
74
    {
75 8
        $defaultHeaders = isset($this->config['headers']) ? $this->config['headers'] : [];
76 8
        return new Request(
77 8
            $method,
78 8
            $path,
79 8
            array_merge($defaultHeaders, $headers),
80
            $body
81 8
        );
82
    }
83
84
    /**
85
     * Send the request.
86
     *
87
     * @param  Request $request
88
     *
89
     * @return Response
90
     */
91 4
    public function send(Request $request)
92
    {
93 4
        $client = new CurlClient;
94 4
        $client->setOption(CURLOPT_URL, $this->buildUri($request->getPath()));
95 4
        $client->setOption(CURLOPT_RETURNTRANSFER, true);
96
97 4
        $headers = [];
98 4
        foreach ($request->getHeaders() as $key => $value) {
99 2
            array_push($headers, sprintf('%s: %s', $key, $value));
100 4
        }
101 4
        $client->setOption(CURLOPT_HTTPHEADER, $headers);
102
103 4
        $client->setOption(CURLOPT_POST, true);
104 4
        $client->setOption(CURLOPT_POSTFIELDS, $request->getBody());
105 4
        $client->setOption(CURLOPT_HEADERFUNCTION, [&$this, 'headerCallback']);
106
107 4
        if (!$result = $client->execute()) {
108 2
            $result = 'cURL Error: '.$client->error();
109 2
        }
110
111 4
        $client->close();
112
113 4
        return new Response(200, $this->headers, $result);
114
    }
115
116
    /**
117
     * Callback to store response headers.
118
     *
119
     * @param  resource $curl
120
     * @param  string   $header
121
     *
122
     * @return int
123
     */
124 2
    public function headerCallback($curl, $header)
125
    {
126 2
        $pair = explode(': ', $header);
127
        // We're only interested in the headers that forms a pair
128 2
        if (count($pair) == 2) {
129 2
            array_push($this->headers, [reset($pair) => end($pair)]);
130 2
        }
131
132 2
        return strlen($header);
133
    }
134
135
    /**
136
     * Build uri with possible base uri.
137
     *
138
     * @param  string $uri
139
     *
140
     * @return string
141
     */
142 4
    private function buildUri($uri)
143
    {
144 4
        if (isset($this->config['base_uri'])) {
145 2
            return $this->config['base_uri'].$uri;
146
        }
147
148 2
        return $uri;
149
    }
150
}
151