Completed
Pull Request — master (#1)
by Sergey
05:27
created

GuzzleHttpAdater::parseResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\Adapters;
4
5
use Guzzle\Http\Client;
6
use Guzzle\Http\Message\Response;
7
use seregazhuk\HeadHunterApi\Contracts\HttpInterface;
8
9
class GuzzleHttpAdater implements HttpInterface
10
{
11
    /**
12
     * @var Client
13
     */
14
    private $client;
15
16
    public function __construct()
17
    {
18
        $this->client = new Client();
19
    }
20
21
    /**
22
     * @param string $url
23
     */
24
    public function setBaseUrl($url)
25
    {
26
        $this->client->setBaseUrl($url);
27
    }
28
29
    /**
30
     * @param string $uri
31
     * @param array $params
32
     * @param null $headers
33
     * @return array|null
34
     */
35
    public function get($uri, $params = [], $headers = null)
36
    {
37
        if(!empty($params)){
38
            $uri .= '?'. http_build_query($params);
39
        }
40
41
        $response = $this->client->get($uri, $headers)
42
            ->send();
43
        return $this->parseResponse($response);
44
    }
45
46
    /**
47
     * @param string $uri
48
     * @param array $params
49
     * @param null $headers
50
     * @return array|null
51
     */
52
    public function post($uri, $params = [], $headers = null)
53
    {
54
        $response = $this->client->post($uri, $headers, $params)
55
            ->send();
56
        return $this->parseResponse($response);
57
    }
58
59
    /**
60
     * @param Response $response
61
     * @return array|null
62
     */
63
    private function parseResponse(Response $response)
64
    {
65
        return json_decode($response->getBody(), true);
66
    }
67
68
}