Completed
Push — master ( 6aab68...44a125 )
by Leo
02:05
created

HttpClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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