Completed
Push — master ( 048bf8...346c21 )
by Leo
03:28
created

HttpClient::getResponseContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
namespace leocata\M1;
4
5
use GuzzleHttp\Client;
6
7
class HttpClient
8
{
9
10
    private $client;
11
    private $auth;
12
    private $request;
13
14
    /**
15
     * HttpClient constructor.
16
     * @param HttpClientAuthorization $auth
17
     */
18 2
    public function __construct(HttpClientAuthorization $auth)
19
    {
20 2
        $this->client = new Client(['base_uri' => 'https://m1online.net']);
21 2
        $this->auth = $auth;
22 2
    }
23
24
    /**
25
     * @param $request
26
     * @return mixed|null
27
     */
28
    public function getResponseContent($request)
29
    {
30
        $this->request = $request;
31
        $response = $this->response()->getBody()->getContents();
32
33
        return !empty($response) ? \GuzzleHttp\json_decode($response) : null;
34
    }
35
36
    /**
37
     * @return mixed|\Psr\Http\Message\ResponseInterface
38
     */
39
    private function response()
40
    {
41
        return $this->client->request('POST', '/', [
42
            'headers' => [
43
                    'Content-Type' => 'application/json'
44
                ] +
45
                $this->auth->getBasicAuth(),
46
            'body' => $this->request
47
        ]);
48
    }
49
}
50