Completed
Push — master ( fb9cc0...a12844 )
by Johan
02:05
created

HttpClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 10
c 4
b 1
f 2
lcom 1
cbo 3
dl 0
loc 121
ccs 36
cts 36
cp 1
rs 10

6 Methods

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