Completed
Push — ci/full-coverage ( 2bad7d...c60b3b )
by Johan
03:51
created

HttpClient::send()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
rs 8.8571
cc 3
eloc 15
nc 4
nop 1
crap 3.0021
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
     * Assign dependencies.
16
     *
17
     * @param array $config
18
     */
19 16
    public function __construct(array $config = [])
20
    {
21 16
        $this->config = $config;
22
23 16
    }
24
25
    /**
26
     * Make a POST request.
27
     *
28
     * @param  string $path
29
     * @param  mixed  $body
30
     * @param  array  $headers
31
     *
32
     * @return Request
33
     */
34 8
    public function post($path, $body = null, array $headers = [])
35
    {
36 8
        return $this->request($path, $body, 'POST', $headers);
37
    }
38
39
    /**
40
     * Send request with HTTP client.
41
     *
42
     * @param  string $path
43
     * @param  mixed  $body
44
     * @param  string $method
45
     * @param  array  $headers
46
     *
47
     * @return Response
48
     */
49 8
    protected function request($path, $body = null, $method = 'GET', array $headers = [])
50
    {
51 8
        $request = $this->createRequest($method, $path, $body, $headers);
52 8
        $response = $this->send($request);
53
54 8
        return $response;
55
    }
56
57
    /**
58
     * Create request with HTTP client.
59
     *
60
     * @param  string $method
61
     * @param  string $path
62
     * @param  mixed  $body
63
     * @param  array  $headers
64
     *
65
     * @return Request
66
     */
67 8
    protected function createRequest($method, $path, $body = null, array $headers = [])
68
    {
69 8
        $defaultHeaders = isset($this->config['headers']) ? $this->config['headers'] : [];
70 8
        return new Request(
71 8
            $method,
72 8
            $path,
73 8
            array_merge($defaultHeaders, $headers),
74
            $body
75 8
        );
76
    }
77
78
    /**
79
     * Send the request.
80
     *
81
     * @param  Request $request
82
     *
83
     * @return Response
84
     */
85 4
    public function send(Request $request)
86
    {
87 4
        $client = new CurlClient;
88 4
        $client->setOption(CURLOPT_URL, $this->buildUri($request->getPath()));
89 4
        $client->setOption(CURLOPT_RETURNTRANSFER, true);
90
91 4
        $headers = [];
92 4
        foreach ($request->getHeaders() as $key => $value) {
93
            array_push($headers, sprintf('%s: %s', $key, $value));
94 4
        }
95 4
        $client->setOption(CURLOPT_HTTPHEADER, $headers);
96
97 4
        $client->setOption(CURLOPT_POST, true);
98 4
        $client->setOption(CURLOPT_POSTFIELDS, $request->getBody());
99
100 4
        $result = $client->execute();
101
102 4
        if ($result === false) {
103 2
            return new Response(500, [], 'cURL Error: '.$client->error());
104
        }
105
106 2
        $client->close();
107
108 2
        return new Response(200, [], $result);
109
    }
110
111
    /**
112
     * Build uri with possible base uri.
113
     *
114
     * @param  string $uri
115
     *
116
     * @return string
117
     */
118 4
    private function buildUri($uri)
119
    {
120 4
        if (isset($this->config['base_uri'])) {
121
            return $this->config['base_uri'].$uri;
122
        }
123
124 4
        return $uri;
125
    }
126
}
127