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

HttpClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 40
rs 10
c 0
b 0
f 0
ccs 4
cts 12
cp 0.3333

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseContent() 0 6 2
A response() 0 8 1
A __construct() 0 4 1
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