Failed Conditions
Pull Request — develop (#21)
by Johan
02:56 queued 01:07
created

HttpClient::buildUri()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8.9038

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 19
ccs 6
cts 13
cp 0.4615
rs 8.8571
cc 5
eloc 10
nc 8
nop 1
crap 8.9038
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
     * Adds an option to config options array
33
     *
34
     * @param  string $option
35
     *
36
     * @return void
37
     */
38
    public function addConfigOption($option)
39
    {
40
        $this->config['options'][] = $option;
41
    }
42
43
    /**
44
     * Make a POST request.
45
     *
46
     * @param  string $path
47
     * @param  mixed  $body
48
     * @param  array  $headers
49
     *
50
     * @return Request
51
     */
52 8
    public function post($path, $body = null, array $headers = [])
53
    {
54 8
        return $this->request($path, $body, 'POST', $headers);
55
    }
56
57
    /**
58
     * Send request with HTTP client.
59
     *
60
     * @param  string $path
61
     * @param  mixed  $body
62
     * @param  string $method
63
     * @param  array  $headers
64
     *
65
     * @return Response
66
     */
67 8
    protected function request($path, $body = null, $method = 'GET', array $headers = [])
68
    {
69 8
        $request = $this->createRequest($method, $path, $body, $headers);
70 8
        $response = $this->send($request);
71
72 8
        return $response;
73
    }
74
75
    /**
76
     * Create request with HTTP client.
77
     *
78
     * @param  string $method
79
     * @param  string $path
80
     * @param  mixed  $body
81
     * @param  array  $headers
82
     *
83
     * @return Request
84
     */
85 8
    protected function createRequest($method, $path, $body = null, array $headers = [])
86
    {
87 8
        $defaultHeaders = isset($this->config['headers']) ? $this->config['headers'] : [];
88 8
        return new Request(
89 8
            $method,
90 8
            $path,
91 8
            array_merge($defaultHeaders, $headers),
92
            $body
93 8
        );
94
    }
95
96
    /**
97
     * Send the request.
98
     *
99
     * @param  Request $request
100
     *
101
     * @return Response
102
     */
103 4
    public function send(Request $request)
104
    {
105 4
        $client = new CurlClient;
106 4
        $client->setOption(CURLOPT_URL, $this->buildUri($request->getPath()));
107 4
        $client->setOption(CURLOPT_RETURNTRANSFER, true);
108 4
        $client->setOption(CURLOPT_HTTPHEADER, $this->getRequestHeaders($request));
109 4
        $client->setOption(CURLOPT_POST, true);
110 4
        $client->setOption(CURLOPT_POSTFIELDS, $request->getBody());
111 4
        $client->setOption(CURLOPT_HEADERFUNCTION, [&$this, 'headerCallback']);
112
113 4
        if (!$result = $client->execute()) {
114 2
            $result = 'cURL Error: '.$client->error();
115 2
        }
116
117 4
        $client->close();
118
119 4
        return new Response(200, $this->responseHeaders, $result);
120
    }
121
122
    /**
123
     * Callback to store response headers.
124
     *
125
     * @param  resource $curl
126
     * @param  string   $header
127
     *
128
     * @return int
129
     */
130 2
    public function headerCallback($curl, $header)
131
    {
132 2
        $pair = explode(': ', $header);
133
        // We're only interested in the headers that forms a pair
134 2
        if (count($pair) == 2) {
135 2
            $this->responseHeaders[reset($pair)] = end($pair);
136 2
        }
137
138 2
        return strlen($header);
139
    }
140
141
    /**
142
     * Prepare the request headers to be sent.
143
     *
144
     * @param  Request $request
145
     * @param  array   $headers
146
     *
147
     * @return array
148
     */
149 4
    protected function getRequestHeaders(Request $request, array $headers = [])
150
    {
151 4
        foreach ($request->getHeaders() as $key => $value) {
152 2
            array_push($headers, sprintf('%s: %s', $key, $value));
153 4
        }
154
155 4
        return $headers;
156
    }
157
158
    /**
159
     * Build uri with possible base uri.
160
     *
161
     * @param  string $uri
162
     *
163
     * @return string
164
     */
165 4
    private function buildUri($uri)
166
    {
167
        // Append possible options to the uri
168 4
        if (array_key_exists('options', $this->config)) {
169
            foreach ($this->config['options'] as $option) {
170
                $options[$option] = '1';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
171
            }
172
        }
173 4
        if (isset($options)) {
174
            $query = http_build_query($options);
175
            $uri = $uri.'?'.$query;
176
        }
177
178 4
        if (isset($this->config['base_uri'])) {
179 2
            return $this->config['base_uri'].$uri;
180
        }
181
182 2
        return $uri;
183
    }
184
}
185