Completed
Push — feature/get-x-query-limit-head... ( 261464...7df971 )
by Johan
02:07
created

HttpClient::getRequestHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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 $responseHeaders = [];
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 4
        $client->setOption(CURLOPT_HTTPHEADER, $this->getRequestHeaders($request));
97 4
        $client->setOption(CURLOPT_POST, true);
98 4
        $client->setOption(CURLOPT_POSTFIELDS, $request->getBody());
99 4
        $client->setOption(CURLOPT_HEADERFUNCTION, [&$this, 'headerCallback']);
100
101 4
        if (!$result = $client->execute()) {
102 2
            $result = 'cURL Error: '.$client->error();
103 2
        }
104
105 4
        $client->close();
106
107 4
        return new Response(200, $this->responseHeaders, $result);
108
    }
109
110
    /**
111
     * Callback to store response headers.
112
     *
113
     * @param  resource $curl
114
     * @param  string   $header
115
     *
116
     * @return int
117
     */
118 2
    public function headerCallback($curl, $header)
119
    {
120 2
        $pair = explode(': ', $header);
121
        // We're only interested in the headers that forms a pair
122 2
        if (count($pair) == 2) {
123 2
            array_push($this->responseHeaders, [reset($pair) => end($pair)]);
124 2
        }
125
126 2
        return strlen($header);
127
    }
128
129
    /**
130
     * Prepare the request headers to be sent.
131
     *
132
     * @param  Request $request
133
     * @param  array   $headers
134
     *
135
     * @return array
136
     */
137 4
    protected function getRequestHeaders(Request $request, $headers = [])
138
    {
139 4
        foreach ($request->getHeaders() as $key => $value) {
140 2
            array_push($headers, sprintf('%s: %s', $key, $value));
141 4
        }
142
143 4
        return $headers;
144
    }
145
146
    /**
147
     * Build uri with possible base uri.
148
     *
149
     * @param  string $uri
150
     *
151
     * @return string
152
     */
153 4
    private function buildUri($uri)
154
    {
155 4
        if (isset($this->config['base_uri'])) {
156 2
            return $this->config['base_uri'].$uri;
157
        }
158
159 2
        return $uri;
160
    }
161
}
162